I using textsearch-controls for fulltext search in postgres. And everything works correct. I created index and used in query. But when I have been needed join another table I faced with error.
> ERROR: invalid reference to FROM-clause entry for table "p"
LINE 10: ON cp.product_id = p.id
^
HINT: There is an entry for table "p", but it cannot be referenced from this part of the query.
my query
SELECT
p.id AS id,
p.sku As sku,
cp .category_id,
ts_rank_cd(to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')), query) AS rank
FROM products p, to_tsquery('Urbanears:*') query
LEFT JOIN category_product cp
ON cp.product_id = p.id
WHERE to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')) @@ query
ORDER BY rank DESC
without left join query with @@ query works correct without problem and without @@ query query with left join works correct. What I missed ? How to use left join with to_tsquery and @@ query ?
when I move left jpin after query I faced with another silent error
query
SELECT
p.id AS id,
p.sku As sku,
cp .category_id,
ts_rank_cd(to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')), query) AS rank
FROM products p, to_tsquery('Urbanears:*') query
WHERE to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')) @@ query
LEFT JOIN category_product cp
ON cp.product_id = p.id
ORDER BY rank DESC
error:
> ERROR: syntax error at or near "LEFT"
LINE 11: LEFT JOIN category_product cp
^
> Time: 0.001s
UPDATE:
SELECT
b.category_id, a.id, a.rank
FROM (SELECT
p.id AS id,
ts_rank_cd(to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')), query) AS rank
FROM products p, to_tsquery('Urbanears:*') query
WHERE to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')) @@ query
ORDER BY rank DESC) as a
LEFT JOIN category_product b on b.product_id=a.id
WHERE b.category_id = 181
I did it when used subquery. Filter by full text search by some word and by category. But it is correct approach ?