Skip to content
Advertisement

Full text search for compound words

I am looking into PostgreSQL full text search and would like to know if it is possible to search for the second part of compound words.

It there a way to get ‘Cheesecake’ as a result when I search for ‘cake’?

If I search for ‘cake’ or ‘list’ I get no results.

Check it with ts_lexize:

The output:

Works as designed but is there a way to get the Cheesecake just from searching for cake? (And I do not mean)

When I select the whole table the Cheesecake is also cut into Cheesecak.

Advertisement

Answer

Full text search is capable of prefix matching. See:

But that only works for left-anchored searches. Your pattern is right-anchored.

You could build an index on reversed strings and search with reversed patterns:

Then this query with prefix search can use the new index:

But I would consider a trigram index instead. See:

Query:

Notably, the pattern is '%cake', not '%cake%', if “cake” shall be at the end of the string. But the trigram index supports this just as well:

db<>fiddle here

Text search indexes are typically substantially smaller than trigram indexes – and therefore a bit faster. There are lots of other subtle differences …

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement