1

I have a query as:

select id 
from events 
where description is not null 
  and (name ilike any (query_arr) 
       or description ilike any (query_arr) 
       or additional_info ilike any (query_arr) 
       or venue_name ilike any (query_arr) 
       or other_category ilike any (query_arr) 
       or eventful_category ilike any (query_arr) )

And query_arr is:

{'%Tomato%','%Potato%','%Pines%'}

But now I need to match the complete word instead of ilike % sign, since it fails for a case where if the description is 'Porcupines are rodentian mammals' then the query_arr word 'pines' gets matched which is incorrect.

So I need to match the complete word itself any where in the table columns being queried.

4
  • It's looks like you need FTS. Commented Nov 30, 2016 at 5:53
  • @SergeyGornostaev can u provide an example for same Commented Nov 30, 2016 at 5:55
  • you can just add spaces around searchwords, like ` select 'Porcupines are rodentian mammals' ~* ' pines ';` instead of ` select 'Porcupines are rodentian mammals' ~* 'pines';` Commented Nov 30, 2016 at 8:33
  • field ~* any(array['\mTomato\M','\mPotato\M','\mPines\M']). But FTS looks promising. Commented Nov 30, 2016 at 9:24

3 Answers 3

1

FTS - is a large and complex subject. Don't use my example as is.

select id from events where to_tsquery('Tomato Potato Pines') @@ to_tsvector(events::text);
Sign up to request clarification or add additional context in comments.

Comments

0

Probably you can use this syntax in PostgreSql

~*ANY('{query_arr}'::text[])

Your Query Should be like this

select id from events where description is not null and (name ~*ANY('{query_arr}'::text[]) or description ~*ANY('{query_arr}'::text[]) or additional_info ~*ANY('{query_arr}'::text[]) or venue_name ~*ANY('{query_arr}'::text[]) or other_category ~*ANY('{query_arr}'::text[]) or eventful_category ~*ANY('{query_arr}'::text[])  

Comments

0

In this case changing

{'%Tomato%','%Potato%','%Pines%'}

to

{'% Tomato %','% Potato %','% Pines %'}

should be enough?..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.