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

后端 未结 2 1865
有刺的猬
有刺的猬 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条回答
  •  -上瘾入骨i
    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)
    

提交回复
热议问题