Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make postgres full text search (tsvector) act like ILIKE to search inside words?

So let's say I search for 'Blerg'. And I have a item with the name SomethingblergSomething.

If I do an ILIKE search in postgres (and rails) like this:

where("name ILIKE ?", "%#{ 'Blerg' }%")

It will return the result 'SomethingBlergSomething' because it contains Blerg.

Is there a way to make the faster tsvector do a similar style of searching inside a word:

where("(to_tsvector('english', name) @@ to_tsquery(?))", ('Blerg' + ':*'))

The above query will not return 'SomethingBlergSomething'.

So how do I make tsvector act like ILIKE when searching inside words.

like image 387
MintDeparture Avatar asked Dec 12 '14 12:12

MintDeparture


People also ask

Is PostgreSQL good for full text search?

Yes, You Can Keep Full-Text Search in Postgres You can get even deeper and make your Postgres full-text search even more robust, by implementing features such as highlighting results, or writing your own custom dictionaries or functions.

How do I find text in PostgreSQL function?

Text search by using to_tsvector function and operator We have search text by using the to_tsvector function in PostgreSQL. In to_tsvector, “ts” is defined as text search. In to_tsvector, the tsvector is the data type of to_tsvector function. This function will return the lexeme tokens with pointers in PostgreSQL.

What is FTS in PostgreSQL?

Introduction. Full-text search (FTS) is a technique used by search engines to find results in a database. It can be used to power search results on websites like shops, search engines, newspapers, and more.

What is Tsvector in PostgreSQL?

A tsvector value is a sorted list of distinct lexemes, which are words that have been normalized to merge different variants of the same word (see Chapter 12 for details).


1 Answers

Are you aware of trigram search, provided by the additional module pg_trgm? That seems more appropriate for your use case than text search.

With a trigram index in place (GIN or GiST) you can use your original ILIKE predicate and get index support for it. You need Postgres 9.1+ for that.

Details:

  • PostgreSQL LIKE query performance variations
  • Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
like image 196
Erwin Brandstetter Avatar answered Oct 17 '22 06:10

Erwin Brandstetter