[英]Postgres full text search: how to search multiple words in multiple fields?
[英]Postgres text search on multiple rows
我有一個名為'exclude'的表,其中包含#標簽:
-------------
id | tag
-------------
1 #oxford
2 #uk
3 #england
-------------
我還有另一個表叫做“ post”:
-----------------------------------------------
id | tags | text
1 #oxford #funtimes Sitting in the sun
2 #oz Beach fun
3 #england Milk was a bad choice
-----------------------------------------------
為了對posts標簽進行文本搜索,我一直在運行如下查詢:
SELECT * FROM post WHERE to_tsvector(tags) @@ plainto_tsquery('mysearchterm')
但是,我現在希望能夠排除某些或所有標簽在我的排除表中的所有帖子。 有什么簡單的方法可以在SQL / Postgres中做到這一點?
我嘗試將標簽行轉換為一列,並在plainto_tsquery函數中使用此術語,但是它不起作用(我不知道如何進行“不等於”的文本搜索,因此邏輯實際上是錯誤的,盡管我的想法正確):
select * from post where to_tsvector(tags) @@ plainto_tsquery(
select array_to_string(array(select RTRIM(value) from exclude where key = 'tag'), ' ')
)
您正在使用哪個版本的PostgreSQL? 架構設計的靈活性如何? 換句話說,您可以隨意更改它嗎? 還是這超出了您的控制范圍?
當我閱讀您的問題時,有兩件事立即浮現在腦海。 一種是您應該能夠使用數組,並且使用@>(包含)或<@(包含)運算符。
這是文檔
其次,您可能能夠使用hstore並執行類似的操作。 至:
hstore @> hstore
這不是真正的hstore,因為您沒有使用真正的key => value對。 但是,我想您可以做到{tagname} = True或{tagname} = NULL。 可能會有點駭人聽聞。
您可以在此處查看 hstore文檔(針對PostgreSQL 9.1)以及如何使用它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.