Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres full-text search with synonyms

I have a database of restaurants which I do a full-text search on. The code looks something like this:

SELECT * FROM restaurant WHERE restaurant.search_vector @@ plainto_tsquery(:terms);

And search_vector is defined like this:

alter table restaurant add column search_vector tsvector;
create index restaurant_search_index on restaurant using gin(search_vector);
create trigger restaurant_search_update before update or insert on restaurant
    for each row execute procedure
    tsvector_update_trigger('search_vector',
    'pg_catalog.english','title');

Now, a notable problem with this search is the word barbecue. It can be spelled many different ways: barbecue, barbeque, BBQ, B.B.Q., B-B-Q, etc. When somebody searches any of these, I need to search restaurants for all of these terms.

From what I've read online, it seems I need to modify the dictionary (That would be pg_catalog.english, right?), but I'm not sure how to go about this.

like image 850
Theron Luhn Avatar asked Aug 07 '12 19:08

Theron Luhn


People also ask

How do I do a full text search in PostgreSQL?

When performing a full-text searches on a PostgreSQL database, you must implement the to_tsvector() and to_tsquery() functions together with a match operator @@. The match operator returns a boolean value (either t or f) when you run a keyword search (tsquery) against a tsvector document.

Are there synonyms in Postgres?

Introduction to PostgreSQL Synonyms. PostgreSQL provides synonyms functionality to the user. A synonym is an identifier and it is used to reference other database objects we can also call alternate names of objects. A synonym is useful in a fully qualified schema name and referenced by PostgreSQL statement.

What is FTS in PostgreSQL?

To summarize, we learnt how to perform full-text search operation in PostgreSQL. If you liked our article, check out the book Mastering PostgreSQL 10 to understand how to perform operations such as indexing, query optimization, concurrent transactions, table partitioning, server tuning, and more.

Is Elasticsearch faster than Postgres?

Elasticsearch is faster than Postgres when it comes to searching for data. Elasticsearch is a powerful search engine that is often faster than Postgres when it comes to searching for data. Elasticsearch can be used to search for documents, images, and other data stored in a database.


1 Answers

Sounds like what you want to do is add a synonym dictionary in front of your english one. This will only work on single words though, so you might have problems with B.B.Q. if it gets parsed as three separate tokens.

Synonym dictionaries in postgresql.org docs

like image 64
Richard Huxton Avatar answered Sep 18 '22 15:09

Richard Huxton