简体   繁体   English

使用pg_search gem for substring在轨道上进行PG全文搜索

[英]PG full text search on rails using pg_search gem for substring

I am using Pg full text search for my search . 我正在使用Pg全文搜索我的搜索。 As i am using Ruby on rails, I am using pg_search gem. 由于我在rails上使用Ruby,我使用的是pg_search gem。 How do i configure it to give a hit for substring as well. 我如何配置它以给出子串的命中。

pg_search_scope :search_by_detail, 
              :against => [
                   [:first_name,'A'],
                   [:last_name,'B'],
                   [:email,'C']
              ],                  
              :using => {
                :tsearch => {:prefix => true}
              }

Right now it gives a hit if the substring is in the start but it wont give a hit if the substring in the middle 现在,如果子串在开始时它给出一个命中,但如果中间的子串,它不会给出命中

example It gives a hit for sdate@example.com but not for example.com 示例它为sdate@example.com命中,但不是example.com

I'm the author and maintainer of pg_search. 我是pg_search的作者和维护者。

Unfortunately, PostgreSQL's tsearch by default doesn't split up email addresses and allow you to match against parts. 不幸的是,默认情况下PostgreSQL的tsearch不会拆分电子邮件地址,并允许您匹配部分。 It might work if you turned on :trigram search, though, since it matches arbitrary sub-strings that appear anywhere in the searchable text. 如果你打开它可能会有效:trigram但是, :trigram搜索,因为它匹配出现在可搜索文本中任意位置的任意子字符串。

pg_search_scope :search_by_detail,
                :against => [
                  [:first_name,'A'],
                  [:last_name,'B'],
                  [:email,'C']
                ],
                :using => {
                  :tsearch => {:prefix => true},
                  :trigram => {}
                }

I confirmed this by running the following command in psql: 我通过在psql中运行以下命令来确认这一点:

grant=# SELECT plainto_tsquery('example.com') @@ to_tsvector('english', 'name@example.com');
 ?column? 
----------
 f
(1 row)

I know that the parser does detect email addresses, so I think it must be possible. 我知道解析器确实检测到了电子邮件地址,所以我认为它必须是可能的。 But it would involve building a text search dictionary in PostgreSQL that would properly split the email address up into tokens. 但它将涉及在PostgreSQL中构建一个文本搜索字典 ,将电子邮件地址正确地拆分为令牌。

Here is evidence that the text search parser knows that it is an email address: 以下是文本搜索解析器知道它是电子邮件地址的证据:

grant=# SELECT ts_debug('english', 'name@example.com');
                                  ts_debug                                   
-----------------------------------------------------------------------------
 (email,"Email address",name@example.com,{simple},simple,{name@example.com})
(1 row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM