postgresql Postgresql全文搜索部分单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4539930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:27:27  来源:igfitidea点击:

Postgresql full text search part of words

postgresqlfull-text-search

提问by Grezly

Is postresql capable of doing a full text search, based on 'half' a word? For example I'm trying to seach for "tree", but I tell postgres to search for "tr".

postresql 是否能够基于“半”字进行全文搜索?例如,我正在尝试"tree"搜索 ,但我告诉 postgres 搜索"tr".

I can't find such a solution that is capable of doing this.

我找不到能够做到这一点的解决方案。

Currently I'm using

目前我正在使用

 select * from test, to_tsquery('tree') as q where vectors @@ q ;

But I'd like to do something like this:

但我想做这样的事情:

 select * from test, to_tsquery('tr%') as q where vectors @@ q ;

采纳答案by catchdave

Sounds like you simply want wildcard matching.

听起来您只是想要通配符匹配。

  • One option, as previously mentioned is trigrams. My (very) limited experience with it was that it was too slow on massive tables for my liking (some cases slower than a LIKE). As I said, my experience with trigrams is limited, so I might have just been using it wrong.

  • A second option you could use is the wildspeedmodule: http://www.sai.msu.su/~megera/wiki/wildspeed(you'll have to build & install this tho).

  • 如前所述,一种选择是trigrams。我对它的(非常)有限的经验是它在我喜欢的大表上太慢了(在某些情况下比 LIKE 慢)。正如我所说,我对三元组的经验有限,所以我可能只是用错了它。

  • 您可以使用的第二个选项是wildspeed模块:http: //www.sai.msu.su/~megera/wiki/wildspeed(您必须构建和安装此 tho)。

The 2nd option will work for suffix/middle matching as well. Which may or may not be more than you're looking for.

第二个选项也适用于后缀/中间匹配。这可能会或可能不会超过您正在寻找的。

There are a couple of caveats (like size of the index), so read through that page thoroughly.

有几个注意事项(例如索引的大小),因此请仔细阅读该页面。

回答by Magnus Hagander

You can use tsearch prefix matching, see http://www.postgresql.org/docs/9.0/interactive/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES

您可以使用 tsearch 前缀匹配,参见http://www.postgresql.org/docs/9.0/interactive/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES

postgres=# select to_tsvector('tree') @@ to_tsquery('tr:*');
 ?column? 
----------
 t
(1 row)

It will only work for prefix search though, not if you want partial match at any position in the word.

不过,它仅适用于前缀搜索,如果您想在单词中的任何位置进行部分匹配,则无效。

回答by Lecky Lao

select * from test, to_tsquery('tree') as q where vectors @@ q OR xxxx LIKE ('%tree%')

select * from test, to_tsquery('tree') as q where 向量@@ q OR xxxx LIKE ('%tree%')

':*' is to specify prefix matching.

':*' 是指定前缀匹配。

回答by Wolph

It can be done with trigrams but it's not part of tsearch2.

它可以用三元组来完成,但它不是 tsearch2 的一部分。

You can view the manual here: http://www.postgresql.org/docs/9.0/interactive/pgtrgm.html

您可以在此处查看手册:http: //www.postgresql.org/docs/9.0/interactive/pgtrgm.html

Basically what the pg_tgrmmodule does is split a word in all it's parts so it can search for those separate parts.

基本上,该pg_tgrm模块所做的就是将一个单词拆分成它的所有部分,以便它可以搜索这些单独的部分。