我已经成功地将单个单词的数组连接成 to_tsquery 的字符串,但 postgres 9.6 中的phraseto_tsquery 只允许一个关键字短语。有谁知道以这样一种方式查询 tsvector(无论是在 Sql 还是全文搜索功能中)的解决方案,我可以(或/与)将动态数量的短语放入查询中。选择块都是文本数组。
第一次尝试:
SELECT to_tsvector('english','Try not to become a man of successful companies, but rather try to become a man of value')
@@ (to_tsquery('english','man & become')
&& phraseto_tsquery('english','man of value')
&& phraseto_tsquery('english','company')
|| phraseto_tsquery('english', 'company | man of value')
);
搜索动物的现实世界问题示例:
-- with statements here of opp_tsv and tp
SELECT
tp.id,
tp.keywords, --['giraffes','lions', 'monkeys']
tp.phrase_keywords, --['pygmy marmocet','African Lion']
tp.neg_keywords, --['aliens', 'spaceships', 'space']
tp.neg_phrase_keywords --['Andromedan Alien', 'Nibiru Reptilian']
FROM tp, opp_tsv,
-- string logic for ts_query
concat(array_to_string(tp.keywords, ' | ')) AS kws_concat,
concat(array_to_string(tp.neg_keywords, ' | ')) AS neg_kws_concat,
to_tsquery('english', kws_concat) query,
to_tsquery('english', concat(neg_kws_concat)) neg_query
-- Case logic for phrase queries
-- .... -> phrase_query,
phraseto_tsquery('phrase to search | Need this phrase too')
-- .... -> phrase_neg_query,
WHERE
(
opp_tsv.doc @@ query --pos
OR
opp_tsv.doc @@ phrase_query --pos
)
AND NOT (
opp_tsv.doc @@ neg_query --neg
OR
opp_tsv.doc @@ phrase_neg_query --neg
)
ORDER BY rank_cd DESC;
思考:根据数组长度动态生成
opp_tsv.doc @@ (phrase_query || phrase_query2)
或以某种方式实现这一目标
opp_tsv.doc @@ phraseto_tsquery('big messy phrase | more messy wordphrases')
编辑:
SELECT phraseto_tsquery('phrase to search | Need this phrase too')
结果='phrase' <-> 'to' <-> 'search' <-> 'need' <-> 'this' <-> 'phrase' <-> 'too'
我正在寻找的是结果'phrase<->to<->search' | 'need<->this<->phrase<->too'