To search according to passed parameters
# File app/models/concerns/customer_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[:trade].blank? @search_definition[:query][:filtered][:query] = { multi_match: { query: params[:trade], type: "phrase_prefix" } } @search_definition[:query][:filtered][:query][:multi_match][:fields] = [ "subtrades_studying.name", "subtrades_studying.alternate_name", "subtrades_studying.trade_name" ] end __elasticsearch__.search(@search_definition) end
This will return all the relevant rows from the database in json format
# File app/models/concerns/customer_search.rb, line 31 def as_indexed_json(options={}) { id: id, name: name, full_name: full_name, email: email, searchable: searchable?, subtrades_studying: subtrades_studying.includes(:trade).map { |c| { _type: 'subtrade', _id: c.id, name: c.name, alternate_name: c.alternate_name, trade_name: c.trade_name } }, trades_studying: trades_studying.map { |s| { _type: 'trade', _id: s.id, name: s.name } } } end
Calls Superclass Method
# File app/models/concerns/customer_search.rb, line 60 def as_json(options={}) json = super(options) json[:_type] = "customer" json end