With PgSearch, I'd like to make use of the stemming dictionaries for full text search in multisearch (database is Postgres 15).
On my model, I have:
class MyModel < ApplicationRecord
include PgSearch::Model
PgSearch.multisearch_options = {
ignoring: :accents,
using: {
tsearch: { dictionary: 'english' }
}
}
multisearchable against: %i[my_column my_other_column]
end
Unfortunately, the text I want to run the multisearch on can be in a variety of languages. Each model has a language column that specifies what language it's in, but I don't know how to use it with .multiserach_options because at the time that code runs, I only have the class and not an instance of it.
So I can't do:
# ...
tsearch: { dictionary: language } #=> NoMethodError `language'
# ...
I tried this:
PgSearch.multisearch_options = ->(record) {
{
ignoring: :accents,
using: {
tsearch: { dictionary: record.language }
}
}
but that didn't work, as it doesn't seem to accept/run the lambda.
How can I dynamically define the dictionary's language for each record?
Ok so I figured it out, kinda.
First of all, the
PgSearch.multisearch_options = ...needs to go in an initializer. I put it inconfig/initializers/pg_search.rb.Second, it doesn't really make sense to set the dictionary per model instance because the multisearch is executed across all of them. So since I have documents in multiple languages, I guess I can only use the
simpledictionary.