Let me try to formulate an answer according to the comments given on the question (if I understand your request correctly).
Problem
You are trying to do a full text search on the table tableA, column indexed_text_field (a tsvector type) based on words that are stored as text in another table tableB in a column called words.
Solution
First, if you wish to feed PostgreSQL multiple tokens (individual words) during a full text search you have two functions at your disposal:
- to_tsquery()
- plainto_tsquery()
In the first function you need to split each given token with an ampersand (&). The second function can be fed any string of text and it will chop it into tokens for you. More info here.
Your challenge is that you wish to select matches based on words present in another table. This can be done in different ways, for example via a simple (INNER) JOIN:
SELECT a.* FROM tableA a, tableB b WHERE a.indexed_text_field @@ to_tsquery(b.words);
Or if you have multiple words in the words column you should most likely be using the plainto_tsquery() function to keep things simple:
SELECT a.* FROM tableA a, tableB b WHERE a.indexed_text_field @@ plainto_tsquery(b.words);
Yet, if you must use the more low-level to_tsquery() version:
SELECT a.* FROM tableA a, tableB b WHERE a.indexed_text_field @@ to_tsquery(replace(b.words, ' ', '&'));
In the latter you replace all spaces between the words with an ampersand, thus making them separate tokens. Mind the index usage on the last one though, as you might need to create an expression index on the usage of the replace() function.
to_tsquerycan search for multiple tokens by splitting the tokens with a &:to_tsquery('foo & bar & baz'). So you could retrieve your list of words from your table and feed them (as tokens) to the `to_tsquery' function, separated by ampersands. Yet ... a few code examples could help to better illustrate what you are trying to accomplish.SELECT * FROM table , to_tsquery(SELECT words FROM another_table) query WHERE Table.indexed_text_field @@ query;I think your solution would be great, but I don't know how to feedto_tsquery.