I have been successfully able to concat arrays of single words into strings for to_tsquery but phraseto_tsquery in postgres 9.6 only allows one keyword phrase. Does anyone know of a solution to query a tsvector (whether in Sql or full-text-search features) in such a way where I can (OR/AND) a dynamic amount of phrases into a query. The select blocks are all arrays of text.
First tries:
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')
);
Example of the real world problem searching for animals:
-- 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;
Thoughts: generate dynamically according to array length
opp_tsv.doc @@ (phrase_query || phrase_query2)
or achieve this somehow
opp_tsv.doc @@ phraseto_tsquery('big messy phrase | more messy wordphrases')
EDIT:
SELECT phraseto_tsquery('phrase to search | Need this phrase too')
result = 'phrase' <-> 'to' <-> 'search' <-> 'need' <-> 'this' <-> 'phrase' <-> 'too'
What I am looking for is the result of 'phrase<->to<->search' | 'need<->this<->phrase<->too'
CREATE AGGRETAGEfunctions around thetsqueryoperators&&and||, or b) just simply use the string representation of outputs of thesephraseto_tsquerycalls, using correct parentheses, e.g.concat('(', array_to_string(tp.phrase_keywords, ') | ('), ')')... - pozsphraseto_tsqueryfunction: Likeplainto_tsquery, thephraseto_tsqueryfunction will not recognizetsqueryoperators, weight labels, or prefix-match labels in its input, sophraseto_tsquery('phrase to search | Need this phrase too')is equivalent tophraseto_tsquery('phrase to search need this phrase too')- pozsWHEREclause:opp_tsv.doc @@ ((query || phrase_query) && !!(neg_query || phrase_neg_query))-- And, if you extract the construction of thistsqueryinto a CTE, you'll more likely use an index for that search. - pozsphraseto_tsquery('phrase to search | Need this phrase too')but this treats the entire input as one phrase. phraseto_query can only take 1 phrase. Thanks for the side note! That can significantly clean up my query. But I still need a programmatic way to add multiple phraseto_query in my query depending on the amount of phrases. @pozs - sizzle