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?