Skip to content

Instantly share code, notes, and snippets.

@nerboda
Last active October 28, 2020 03:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerboda/1f580680f36f2fba3a9ece0c3f2fa990 to your computer and use it in GitHub Desktop.
Save nerboda/1f580680f36f2fba3a9ece0c3f2fa990 to your computer and use it in GitHub Desktop.
Elastic Search Query
class << self
def query(params)
listings = self.active_record_search(params) # filter by other parameters first
# return right there if search is blank
return listings.page(params[:page]) if params[:search].blank?
# otherwise pass already filtered set to elastic search for further filtering
listing_ids = listings.pluck(:id)
self.elastic_search(params, listing_ids)
end
def active_record_search(params)
# ...some code
end
def elastic_search(params, listing_ids)
elastic_query = {
fields: [:title, :description, :tagged, :artist_name],
# include a where clause so I'm only searching records returned by the other filters
where: { id: listings_ids },
order: { _score: :desc },
page: params[:page],
per_page: 15
}
self.search(params[:search], elastic_query)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment