Question

I have a Rails app that uses the pg_search gem to leverage the Full Text Search capabilities of PostgreSQL.

The manual says about searching a model that has (for instance) a has_many relation:

Searching through associations

It is possible to search columns on associated models. Note that if you do this, it will be impossible to speed up searches with database indexes. However, it is supported as a quick way to try out cross-model searching.

The last lines seems to suggest that this is a good way to prototype searching through associations. And yes, it works indeed. But I typically have a 10 second latency for returning results. Is there a way to to have cross-model searching that is also fast?

Is it: search both models separately, then merge the results?

Was it helpful?

Solution

What I did as a workaround, added a str_* column to my main table, and update this column when an element is saved:

  before_validation(on: :save) do
    self.str_abbreviations = join_abbreviations()
    # ... etc, append all columns I'd like to search through ...
    true
  end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top