diff --git a/plugins/elasticsearch/helpers/elasticsearch_helper.rb b/plugins/elasticsearch/helpers/elasticsearch_helper.rb index 05168f7..3b65121 100644 --- a/plugins/elasticsearch/helpers/elasticsearch_helper.rb +++ b/plugins/elasticsearch/helpers/elasticsearch_helper.rb @@ -19,44 +19,18 @@ module ElasticsearchHelper } end - def fields_from_model - klass::SEARCHABLE_FIELDS.map do |key, value| - if value[:weight] - "#{key}^#{value[:weight]}" - else - "#{key}" + def fields_from_models klasses + fields = Set.new + klasses.each do |klass| + klass::SEARCHABLE_FIELDS.map do |key, value| + if value and value[:weight] + fields.add "#{key}^#{value[:weight]}" + else + fields.add "#{key}" + end end end - end - - def get_query text, klass=nil - query = {} - unless text.blank? - text = text.downcase - query = { - query: { - match_all: { - } - }, - filter: { - regexp: { - name: { - value: ".*" + text + ".*" } - } - }, - suggest: { - autocomplete: { - text: text, - term: { - field: "name", - suggest_mode: "always" - } - } - } - - } - end - query + fields.to_a end def process_results @@ -69,10 +43,8 @@ module ElasticsearchHelper end def search_from_all_models - models = [] query = get_query params[:query] - - ElasticsearchHelper::searchable_types.keys.each {| model | models.append( model.to_s.classify.constantize) if model != :all } + models = searchable_models Elasticsearch::Model.search(query, models, size: default_per_page(params[:per_page])).page(params[:page]).records end @@ -90,4 +62,41 @@ module ElasticsearchHelper per_page ||= 10 end + private + + def get_query text, klass=nil + fields = klass.nil? ? (fields_from_models searchable_models) : (fields_from_models [klass]) + query = {} + unless text.blank? + text = text.downcase + query = { + query: { + multi_match: { + query: text, + type: "phrase", + fields: fields, + zero_terms_query: "none" + }, + }, + sort: [ + {"name.raw" => {"order" => "asc"}} + ], + suggest: { + autocomplete: { + text: text, + term: { + field: "name", + suggest_mode: "always" + } + } + } + } + end + query + end + + def searchable_models + SEARCHABLE_TYPES.except(:all).keys.map { | model | model.to_s.classify.constantize } + end + end diff --git a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb b/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb index c3c7ab3..b7dcfee 100644 --- a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb +++ b/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb @@ -9,7 +9,10 @@ module ElasticsearchIndexedModel mappings dynamic: 'false' do base.indexable_fields.each do |field, value| value = {} if value.nil? - indexes field, type: value[:type].presence + indexes field, type: value[:type].presence, fields: {"raw" => + {"index" => "not_analyzed", + "type" => "String" + } } print '.' end end @@ -29,7 +32,7 @@ module ElasticsearchIndexedModel module ClassMethods def indexable_fields - self::SEARCHABLE_FIELDS.update self.control_fields + self::SEARCHABLE_FIELDS.merge self.control_fields end end -- libgit2 0.21.2