Postgres全文搜索:如何搜索多个字段中的多个单词? [英] Postgres full text search: how to search multiple words in multiple fields?

查看:119
本文介绍了Postgres全文搜索:如何搜索多个字段中的多个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用Postgresql,并试图在我的网站中创建一个搜索引擎。我有这张表:

  CREATE TABLE shop(
id序列主键,
名称TEXT NOT NULL ,
description TEXT,
address TEXT NOT NULL,
city TEXT NOT NULL
);

然后我为表的每个字段创建一个索引(这是正确的方式吗?或者,也许我):



pre $ CREATE INDEX shop_name_fts ON shop使用杜松子酒(to_tsvector('italian',name) );
CREATE INDEX shop_desc_fts ON shop使用杜松子酒(to_tsvector('italian',description));
CREATE INDEX shop_addr_fts使用杜松子酒(to_tsvector('italian',address));
CREATE INDEX shop_city_fts ON shop使用杜松子酒(to_tsvector('italian',city));

现在,如果我想在每个索引中搜索一个单词,那么SQL查询是什么?



我试过这个,它可以工作:

  SELECT id FROM shop WHERE to_tsvector( ('$ word')OR 
to_tsvector(描述)@@ to_tsquery $ b to_tsvector(city)@@ to_tsquery('$ word')

照着做?
我可以搜索 to_tsquery 为多个 to_tsvector
我的一位朋友提出了一个解决方案,但它是针对MySQL数据库的:

$ p $ SELECT * FROM shop WHERE MATCH名称,描述,地址,城市)AGAINST('$ word')

Postgresql的解决方案是什么?



另外,我可以将多个 to_tsquery 搜索到多个 to_tsvector ?如果我想搜索两个单词或多个单词,SQL查询是什么?我能否将两个单词从PHP中传递给$单词?如果可以,它是如何工作的?它是否搜索第一个字和第二个字或第一个字或第二个字?

解决方案

它看起来像你想要的是事实上可以搜索所有这些字段的连接。



你可以建立一个查询来完成这个操作

  ...其中to_tsvector('italian',name ||''|| coalesce(decription,'')...)@@ to_tsquery('$ word')

并在完全相同的计算上构建一个索引:

 使用GIN(to_tsvector('italian',name ||''|| coalesce(decription,'')...))在shop 
上创建索引your_index

不要忘记在接受NULL值的列上使用 coalesce


i'm using for the first time Postgresql and i'm trying to create a search engine in my website. i have this table:

CREATE TABLE shop (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  description TEXT,
  address TEXT NOT NULL,
  city TEXT NOT NULL
);

Then i created an index for every field of the table (is this the right way? Or maybe i can create one index for all fields?):

CREATE INDEX shop_name_fts ON shop USING gin(to_tsvector('italian', name));
CREATE INDEX shop_desc_fts ON shop USING gin(to_tsvector('italian', description));
CREATE INDEX shop_addr_fts ON shop USING gin(to_tsvector('italian', address));
CREATE INDEX shop_city_fts ON shop USING gin(to_tsvector('italian', city));

Now, what is the SQL query if i want to search one word in every index?

I tried this and it works:

SELECT id FROM shop WHERE to_tsvector(name) @@ to_tsquery('$word') OR
                          to_tsvector(description) @@ to_tsquery('$word') OR 
                          to_tsvector(address) @@ to_tsquery('$word') OR 
                          to_tsvector(city) @@ to_tsquery('$word')

Does exist a better way to do the same? Can i search to_tsquery into multiple to_tsvector? A friend of mine suggests a solution, but it is for MySQL database:

SELECT * FROM shop WHERE MATCH(name, description, address, city) AGAINST('$word')

What is the solution for Postgresql?

In addition, can i search multiple to_tsquery into multiple to_tsvector? what is the SQL query if i want to search two words or more than one word? Can i just pass "two words" to $word from PHP? If i can, how does it work? Does it search for first word AND second one or first word OR second one?

解决方案

It looks like what you want is, in fact to search the concatenation of all those fields.

You could build a query doing exactly this

... where to_tsvector('italian', name||' '||coalesce(decription,'')...) @@ to_tsquery('$word')

and build an index on the exact same computation:

create index your_index on shop
using GIN(to_tsvector('italian',name||' '||coalesce(decription,'')...))

Don't forget to use coalesce on columns accepting NULL values.

这篇关于Postgres全文搜索:如何搜索多个字段中的多个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆