To search according to passed parameters
# File app/models/concerns/student_search.rb, line 67 def self.search(params) @search_definition = { query: {filtered: {filter: {bool: {must: {and: [{term: {searchable: true}}]}}}}}, } unless params[:query].blank? @search_definition[:query][:filtered][:query] ||= {} @search_definition[:query][:filtered][:query][:multi_match] = { query: params[:query], fields: ['full_name'], type: "phrase_prefix" } end unless params[:subject].blank? @search_definition[:query][:filtered][:query] = { multi_match: { query: params[:subject], type: "phrase_prefix" } } @search_definition[:query][:filtered][:query][:multi_match][:fields] = [ "courses_studying.name", "courses_studying.alternate_name", "courses_studying.subject_name" ] end __elasticsearch__.search(@search_definition) end
This will return all the relevant rows from the database in json format
# File app/models/concerns/student_search.rb, line 31 def as_indexed_json(options={}) { id: id, name: name, full_name: full_name, email: email, searchable: searchable?, courses_studying: courses_studying.includes(:subject).map { |c| { _type: 'course', _id: c.id, name: c.name, alternate_name: c.alternate_name, subject_name: c.subject_name } }, subjects_studying: subjects_studying.map { |s| { _type: 'subject', _id: s.id, name: s.name } } } end
Calls Superclass Method
# File app/models/concerns/student_search.rb, line 60 def as_json(options={}) json = super(options) json[:_type] = "student" json end