in reply to Faster of two options for finding keywords in a database
The built-in postgres keyword search isn't as good as ElasticSearch, but it's right at your fingertips when your database is already postgres. It works like this:
Here's an example of the DDL:
CREATE TABLE sometable ( ... keyword_search text not null ); CREATE INDEX sometable_fulltext ON sometable USING GIN (to_tsvector('english', keyword_search));
obviously change 'english' if your users have a different primary language.
If you have multiple columns to search, you can make a generated column that combines them into the keywords:
You select from this table likekeyword_search text not null GENERATED ALWAYS AS ( name || ' ' || COALESCE(description, '') || ' ' || COALESCE(tags, '')) STORED,
And then substitute your search for both '?' in that query.SELECT * FROM sometable WHERE to_tsvecor('english',keyword_search) @@ plainto_tsquery('english +', ?) ORSER BY ts_rank(to_tsvecor('english',keyword_search), plainto_tsquery +('english', ?))
If you're interested in integrating this with DBIx::Class, I can show some neat code for that.
The ElasticSearch solution is more involved. You would set up a separate ElasticSsearch database and then every time your application modifies content you also push a summary of that content into the elasticsearch database, then query that database instead of postgres to get your list of entity IDs to show to the user, then query postgres for the entities using the list of IDs, then render them for the user.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Faster of two options for finding keywords in a database
by Danny (Chaplain) on Feb 18, 2024 at 02:46 UTC | |
by NERDVANA (Priest) on Feb 18, 2024 at 03:46 UTC |