def self.search(params) __elasticsearch__.search(:page => params[:page], :per_page => params[:per_page], load: true) do query { string "name:#{params[:query]}*" } if params[:query].present? filter :exists , :field => :tutors end end
To search by passed parameters
# File app/models/concerns/category_search.rb, line 50 def self.search(params) @search_definition = { query: { filtered: { filter: { exists: { field: "tutors" } } } } } unless params[:query].blank? @search_definition[:query][:filtered][:query] = { multi_match: { query: params[:query], fields: [ "name" ], type: "phrase_prefix" } } end __elasticsearch__.search(@search_definition) end
This will return all the relevant rows from the database.
# File app/models/concerns/category_search.rb, line 21 def as_indexed_json(options={}) { id: id, name: name, tutors: tutors.map { |t| { _type: 'tutor', _id: t.id, name: t.name } } } end
Calls Superclass Method
# File app/models/concerns/category_search.rb, line 37 def as_json(option={}) json = super(option) json[:_type] = 'category' json end