How to use PgSearch's multisearch_options to set the stemming dictionary language from the model instance's language?

65 Views Asked by At

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?

1

There are 1 best solutions below

0
Pierre On

Ok so I figured it out, kinda.

First of all, the PgSearch.multisearch_options = ... needs to go in an initializer. I put it in config/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 simple dictionary.