module UsersSearch extend ActiveSupport::Concern included do include ApplicationSearch mappings do indexes :id, type: :integer indexes :email, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, index_analyzer: :index_analyzer indexes :name, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, index_analyzer: :index_analyzer indexes :username, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, index_analyzer: :index_analyzer indexes :bio, type: :string indexes :skype, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, index_analyzer: :index_analyzer indexes :linkedin, type: :string indexes :twitter, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, index_analyzer: :index_analyzer indexes :state, type: :string indexes :website_url, type: :string indexes :created_at, type: :date indexes :admin, type: :boolean indexes :name_sort, type: :string, index: 'not_analyzed' indexes :created_at_sort, type: :string, index: 'not_analyzed' indexes :updated_at_sort, type: :string, index: 'not_analyzed' end def as_indexed_json(options = {}) as_json.merge({ name_sort: name.downcase, updated_at_sort: updated_at, created_at_sort: created_at }) end def self.elastic_search(query, page: 1, per: 20, options: {}) page ||= 1 per ||= 20 if options[:in].blank? options[:in] = %w(name^3 username^2 email) else options[:in].push(%w(name^3 username^2 email) - options[:in]) end query_hash = { query: { filtered: { query: { multi_match: { fields: options[:in], query: "#{query}", operator: :and } }, }, }, size: per, from: per * (page.to_i - 1) } if query.blank? query_hash[:query][:filtered][:query] = { match_all: {}} query_hash[:track_scores] = true end if options[:uids] query_hash[:query][:filtered][:filter] ||= { and: [] } query_hash[:query][:filtered][:filter][:and] << { ids: { values: options[:uids] } } end if options[:active] query_hash[:query][:filtered][:filter] ||= { and: [] } query_hash[:query][:filtered][:filter][:and] << { terms: { state: ["active"] } } end options[:order] = :default if options[:order].blank? order = case options[:order].to_sym when :newest { created_at_sort: { order: :asc, mode: :min } } when :oldest { created_at_sort: { order: :desc, mode: :min } } when :recently_updated { updated_at_sort: { order: :asc, mode: :min } } when :last_updated { updated_at_sort: { order: :desc, mode: :min } } else { name_sort: { order: :asc, mode: :min } } end query_hash[:sort] = [ order, :_score ] if options[:highlight] query_hash[:highlight] = highlight_options(options[:in]) end self.__elasticsearch__.search(query_hash) end end end