0

I use PostgreSQL's full text search capabilities, which works fine. All the relevant columns are indexed so it's nice and speedy:

  def self.text_search(query)
    if (query.present?)
      # search(query)
      where(
          "to_tsvector('english', title) @@ plainto_tsquery(:q)",
          q: query
      )
    else
      scoped
    end
  end

But now I also want to search through related abbreviations:

def self.text_search(query)
    if (query.present?)
      # search(query)
      includes(:abbreviations).where(
          "to_tsvector('english', articles.title) @@ plainto_tsquery(:q)"+
          " or to_tsvector('english', abbreviations.abbreviation) @@ plainto_tsquery(:q)",
          q: query
      )
    else
      scoped
    end
  end

This works, but now my queries take 2.5+ seconds! How do I remedy this? I was thinking that maybe this is a Rails inefficiency, so I could best perform raw SQL. But how do I do that and still get back a ActiveRecord relation?

1 Answer 1

1

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

  before_validation(on: :create) do
    self.str_abbreviations = join_abbreviations()
    ... etc ...
    true
  end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.