Last active
October 28, 2020 03:46
-
-
Save nerboda/1f580680f36f2fba3a9ece0c3f2fa990 to your computer and use it in GitHub Desktop.
Elastic Search Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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