How do you do phrase-based full text search in postgres that takes advantage of the full-text index?

后端 未结 2 1864
有刺的猬
有刺的猬 2020-12-08 17:01

Let\'s say you have a postgres 8.3 table as follows:

CREATE TABLE t1 (body text, body_vector tsvector);

I want to be able to search it for phras

相关标签:
2条回答
  • 2020-12-08 17:21

    If you want exact phrase matching, that's the way to do it. You can also try WHERE body_vector @@ plainto_tsquery('w1 w2'), and then order it by ranking. (the point being that the hits where the words are right next to each other should end up on top)

    0 讨论(0)
  • 2020-12-08 17:24

    Update: PostgreSQL 9.6 text search supports phrases

    select
      *
    from (values
      ('i heart new york'),
      ('i hate york new')
    ) docs(body)
    where
      to_tsvector(body) @@ phraseto_tsquery('new york')
    
    (1 row retrieved)
    

    or by distance between words:

    -- a distance of exactly 2 "hops" between "quick" and "fox"
    select
      *
    from (values
      ('the quick brown fox'),
      ('quick brown cute fox')
    ) docs(body)
    where
      to_tsvector(body) @@ to_tsquery('quick <2> fox') 
    
    (1 row retrieved)
    
    0 讨论(0)
提交回复
热议问题