I use postgresql 11
I want to make and advanced search .
my goal is to check if all the words of my text exist in original text
this is the original text
"dumas, franshesco robert Ferrer Lombardy alive"
this is the possibility of search :
"franshesco robert alive" return ok
"franshesco robert Ferrer Viran" not ok because Viran not extist in the original text
"dumas franshesco , robert" ok
"alive dumas ! franshesco" ok
"alive dumas ! suzan franshesco " not ok because suzan not extist in the original text
so meaning when make the search first split the text using space and exclude special character like (,!?.)
then check if all of this words exist or not in the original text "dumas, franshesco robert Ferrer Lombardy alive"
for example to search this "alive dumas ! franshesco"
in the original text "dumas, franshesco robert Ferrer Lombardy alive"
fist split the text and exclude special character
so to have this words alive and dumas and franshesco
then check if all of this words exist or not in the original text .
I use this code :
select string_to_array(regexp_replace('alive dumas ! franshesco', '[^\w]+',' ','g'), ' ') as val_arr
in order to split string into words and remove special character
the result is "{alive,dumas,franshesco}"
now I want to find the way to check if all of this words exist or not in this string
"dumas, franshesco robert Ferrer Lombardy alive"
can someone help me to find the correct query
>Solution :
You want all rows in the table whose text contains only words from your parameter string. You already have the logic to turn a string to an array of words, so we can build upon it ; array containment operator <@ comes handy here.
Assuming that phrases are stored under column txt in mytable:
select *
from mytable
where string_to_array(regexp_replace(txt, '[^\w]+',' ','g'), ' ') )
<@ string_to_array(regexp_replace('alive dumas ! franshesco', '[^\w]+',' ','g'), ' ')