From 7b7c6b1f3f3d4ae4af72b1dc55e3054b3a92cea4 Mon Sep 17 00:00:00 2001 From: Macartur Sousa Date: Thu, 7 Jul 2016 22:00:33 -0300 Subject: [PATCH] Elasticsearch: Altering filter structure --- plugins/elasticsearch/helpers/elasticsearch_helper.rb | 2 +- plugins/elasticsearch/helpers/nested_helper/environment.rb | 25 +++++++++++++++++++++++++ plugins/elasticsearch/helpers/nested_helper/profile.rb | 29 +++++++++++++++++++++++++++++ plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/elasticsearch/helpers/searchable_model/filter.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ plugins/elasticsearch/helpers/searchable_model_helper.rb | 9 +++++++++ plugins/elasticsearch/lib/elasticsearch_indexed_model.rb | 125 ----------------------------------------------------------------------------------------------------------------------------- plugins/elasticsearch/lib/ext/community.rb | 21 +++++++++++---------- plugins/elasticsearch/lib/ext/event.rb | 30 +++++++++++++++++++++++++----- plugins/elasticsearch/lib/ext/person.rb | 21 +++++++++++++++++---- plugins/elasticsearch/lib/ext/text_article.rb | 34 +++++++++++++++++++++------------- plugins/elasticsearch/lib/ext/uploaded_file.rb | 29 ++++++++++++++++++++++++----- plugins/elasticsearch/lib/nested_environment.rb | 8 -------- 13 files changed, 304 insertions(+), 171 deletions(-) create mode 100644 plugins/elasticsearch/helpers/nested_helper/environment.rb create mode 100644 plugins/elasticsearch/helpers/nested_helper/profile.rb create mode 100644 plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb create mode 100644 plugins/elasticsearch/helpers/searchable_model/filter.rb create mode 100644 plugins/elasticsearch/helpers/searchable_model_helper.rb delete mode 100644 plugins/elasticsearch/lib/elasticsearch_indexed_model.rb delete mode 100644 plugins/elasticsearch/lib/nested_environment.rb diff --git a/plugins/elasticsearch/helpers/elasticsearch_helper.rb b/plugins/elasticsearch/helpers/elasticsearch_helper.rb index 5450bfa..32ea7df 100644 --- a/plugins/elasticsearch/helpers/elasticsearch_helper.rb +++ b/plugins/elasticsearch/helpers/elasticsearch_helper.rb @@ -84,7 +84,7 @@ module ElasticsearchHelper query: query_string(expression,models), filter: { bool: { - should: models.map {|model| model.filter(environment: @environment.id)} + should: models.map {|model| model.filter(environment: @environment.id) } } } } diff --git a/plugins/elasticsearch/helpers/nested_helper/environment.rb b/plugins/elasticsearch/helpers/nested_helper/environment.rb new file mode 100644 index 0000000..f923e25 --- /dev/null +++ b/plugins/elasticsearch/helpers/nested_helper/environment.rb @@ -0,0 +1,25 @@ +module NestedEnvironment + + def self.environment_hash + { + :id => { type: :integer }, + :is_default => {type: :boolean } + } + end + + def self.environment_filter environment=1 + { + query: { + nested: { + path: "environment", + query: { + bool: { + must: { term: { "environment.id" => environment } }, + } + } + } + } + } + end + +end diff --git a/plugins/elasticsearch/helpers/nested_helper/profile.rb b/plugins/elasticsearch/helpers/nested_helper/profile.rb new file mode 100644 index 0000000..d3bd6f0 --- /dev/null +++ b/plugins/elasticsearch/helpers/nested_helper/profile.rb @@ -0,0 +1,29 @@ +module NestedProfile + + def self.hash + { + :id => { type: :integer }, + :visible => { type: :boolean }, + :public_profile => { type: :boolean } + } + end + + def self.filter + { + query: { + nested: { + path: "profile", + query: { + bool: { + must:[ + { term: { "profile.visible" => true } }, + { term: { "profile.public_profile" => true } } + ], + } + } + } + } + } + end + +end diff --git a/plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb b/plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb new file mode 100644 index 0000000..42f40df --- /dev/null +++ b/plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb @@ -0,0 +1,99 @@ +require_relative '../nested_helper/environment' + +module ElasticsearchIndexedModel + + def self.included base + base.send :include, Elasticsearch::Model + base.send :include, Elasticsearch::Model::Callbacks + + base.send :index_name, "#{Rails.env}_#{base.index_name}" + + base.extend ClassMethods + base.send :include, InstanceMethods + + base.class_eval do + settings index: { number_of_shards: 1 } do + mappings dynamic: 'false' do + base.indexed_fields.each do |field, value| + type = value[:type].presence + + if type == :nested + indexes(field, type: type) do + value[:hash].each do |hash_field, hash_value| + indexes(hash_field, base.indexes_as_hash(hash_field,hash_value[:type].presence)) + end + end + else + indexes(field, base.indexes_as_hash(field,type)) + end + print '.' + end + end + + base.__elasticsearch__.client.indices.delete \ + index: base.index_name rescue nil + base.__elasticsearch__.client.indices.create \ + index: base.index_name, + body: { + settings: base.settings.to_hash, + mappings: base.mappings.to_hash + } + end + end + base.send :import + end + + module ClassMethods + + def indexes_as_hash(name, type) + hash = {} + if type.nil? + hash[:fields] = raw_field(name, type) + else + hash[:type] = type if not type.nil? + end + hash + end + + def raw_field name, type + { + raw: { + type: "string", + index: "not_analyzed" + } + } + end + + def indexed_fields + fields = { + :environment => {type: :nested, hash: NestedEnvironment::environment_hash }, + :created_at => {type: :date } + } + fields.update(self::SEARCHABLE_FIELDS) + fields.update(self.control_fields) + fields + end + + end + + module InstanceMethods + def as_indexed_json options={} + attrs = {} + + self.class.indexed_fields.each do |field, value| + type = value[:type].presence + + if type == :nested + attrs[field] = {} + value[:hash].each do |hash_field, hash_value| + attrs[field][hash_field] = self.send(field).send(hash_field) + end + else + attrs[field] = self.send(field) + end + end + attrs.as_json + end + end + +end diff --git a/plugins/elasticsearch/helpers/searchable_model/filter.rb b/plugins/elasticsearch/helpers/searchable_model/filter.rb new file mode 100644 index 0000000..8d1117c --- /dev/null +++ b/plugins/elasticsearch/helpers/searchable_model/filter.rb @@ -0,0 +1,43 @@ +require_relative '../nested_helper/environment' + +module Filter + + def self.included base + base.extend ClassMethods + base.send :include, InstanceMethods + end + + module ClassMethods + + def filter options={} + environment = options[:environment].presence + + result_filter = {} + result_filter[:indices] = {:index => self.index_name, :no_match_filter => "none" } + result_filter[:indices][:filter] = { :bool => self.filter_bool(environment) } + + result_filter + end + + def filter_bool environment + result_filter = {} + + result_filter[:must] = [ NestedEnvironment::environment_filter(environment) ] + + self.nested_filter.each {|filter| result_filter[:must].append(filter)} if self.respond_to? :nested_filter + self.must.each {|filter| result_filter[:must].append(filter) } if self.respond_to? :must + + result_filter[:should] = self.should if self.respond_to? :should + result_filter[:must_not] = self.must_not if self.respond_to? :must_not + + result_filter + end + + + end + + module InstanceMethods + + end + +end diff --git a/plugins/elasticsearch/helpers/searchable_model_helper.rb b/plugins/elasticsearch/helpers/searchable_model_helper.rb new file mode 100644 index 0000000..5e81d85 --- /dev/null +++ b/plugins/elasticsearch/helpers/searchable_model_helper.rb @@ -0,0 +1,9 @@ +require_relative './searchable_model/elasticsearch_indexed_model' +require_relative './searchable_model/filter' + +module SearchableModelHelper + def self.included base + base.send :include, ElasticsearchIndexedModel + base.send :include, Filter + end +end diff --git a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb b/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb deleted file mode 100644 index aa774f4..0000000 --- a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb +++ /dev/null @@ -1,125 +0,0 @@ -require_relative 'nested_environment' - -module ElasticsearchIndexedModel - - def self.included base - base.send :include, Elasticsearch::Model - base.send :include, Elasticsearch::Model::Callbacks - - base.send :index_name, "#{Rails.env}_#{base.index_name}" - - base.extend ClassMethods - base.send :include, InstanceMethods - - base.class_eval do - settings index: { number_of_shards: 1 } do - mappings dynamic: 'false' do - base.indexed_fields.each do |field, value| - type = value[:type].presence - - if type == :nested - indexes(field, type: type) do - value[:hash].each do |hash_field, hash_value| - indexes(hash_field, base.indexes_as_hash(hash_field,hash_value[:type].presence)) - end - end - else - indexes(field, base.indexes_as_hash(field,type)) - end - print '.' - end - end - - base.__elasticsearch__.client.indices.delete \ - index: base.index_name rescue nil - base.__elasticsearch__.client.indices.create \ - index: base.index_name, - body: { - settings: base.settings.to_hash, - mappings: base.mappings.to_hash - } - end - end - base.send :import - end - - module ClassMethods - - def indexes_as_hash(name, type) - hash = {} - if type.nil? - hash[:fields] = raw_field(name, type) - else - hash[:type] = type if not type.nil? - end - hash - end - - def raw_field name, type - { - raw: { - type: "string", - index: "not_analyzed" - } - } - end - - def indexed_fields - fields = { - :environment => {type: :nested, hash: NestedEnvironment.environment_hash }, - :created_at => {type: :date } - } - fields.update(self::SEARCHABLE_FIELDS) - fields.update(self.control_fields) - fields - end - - def environment_filter environment=1 - { - query: { - nested: { - path: "environment", - query: { - bool: { - must: { term: { "environment.id" => environment } }, - } - } - } - } - } - end - - def filter options={} - environment = options[:environment].presence - - filter = {} - filter[:indices] = {:index => self.index_name, :no_match_filter => "none" } - filter[:indices][:filter] = { :bool => {} } - filter[:indices][:filter][:bool][:must] = [ environment_filter(environment) ] - filter[:indices][:filter][:bool][:should] = [ { :and => self.should_and } ] if self.respond_to? :should_and - filter - end - - end - - module InstanceMethods - def as_indexed_json options={} - attrs = {} - - self.class.indexed_fields.each do |field, value| - type = value[:type].presence - - if type == :nested - attrs[field] = {} - value[:hash].each do |hash_field, hash_value| - attrs[field][hash_field] = self.send(field).send(hash_field) - end - else - attrs[field] = self.send(field) - end - end - attrs.as_json - end - end - -end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb index 7079efb..e4c9010 100644 --- a/plugins/elasticsearch/lib/ext/community.rb +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -1,24 +1,25 @@ require_dependency 'community' -require_relative '../elasticsearch_indexed_model' +require_relative '../../helpers/searchable_model_helper' class Community def self.control_fields { - :secret => { type: :boolean }, - :visible => { type: :boolean }, + :secret => { type: :boolean }, + :visible => { type: :boolean }, } end - # community visible - def self.should_and + def self.should [ - {term: { :secret => false }}, - {term: { :visible => true }} + { and: + [ + {term: { :secret => false }}, + {term: { :visible => true }} + ] + } ] end - - include ElasticsearchIndexedModel - + include SearchableModelHelper end diff --git a/plugins/elasticsearch/lib/ext/event.rb b/plugins/elasticsearch/lib/ext/event.rb index acd90b2..7e74fa4 100644 --- a/plugins/elasticsearch/lib/ext/event.rb +++ b/plugins/elasticsearch/lib/ext/event.rb @@ -1,13 +1,33 @@ require_dependency 'event' -require_relative '../elasticsearch_indexed_model' + +require_relative '../../helpers/searchable_model_helper' +require_relative '../../helpers/nested_helper/profile' class Event + def self.control_fields { - :advertise => {}, - :published => {}, - :created_at => {type: 'date'} + :advertise => {type: :boolean}, + :published => {type: :boolean}, + :profile => { type: :nested , hash: NestedProfile.hash } } end - include ElasticsearchIndexedModel + + def self.should + [ + { and: [ + { term: { advertise: true }}, + { term: { published: true }} + ] + } + ] + end + + def self.nested_filter + [ + NestedProfile::filter + ] + end + + include SearchableModelHelper end diff --git a/plugins/elasticsearch/lib/ext/person.rb b/plugins/elasticsearch/lib/ext/person.rb index 1ce563d..2691975 100644 --- a/plugins/elasticsearch/lib/ext/person.rb +++ b/plugins/elasticsearch/lib/ext/person.rb @@ -1,13 +1,26 @@ require_dependency 'person' -require_relative '../elasticsearch_indexed_model' + +require_relative '../../helpers/searchable_model_helper' class Person + def self.control_fields { :visible => {type: 'boolean'}, - :public_profile => {type: 'boolean'}, - :created_at => {type: 'date'} + :secret => { type: :boolean }, } end - include ElasticsearchIndexedModel + + def self.should + [ + { and: + [ + {term: { :secret => false }}, + {term: { :visible => true }} + ] + } + ] + end + + include SearchableModelHelper end diff --git a/plugins/elasticsearch/lib/ext/text_article.rb b/plugins/elasticsearch/lib/ext/text_article.rb index 4eda103..d400048 100644 --- a/plugins/elasticsearch/lib/ext/text_article.rb +++ b/plugins/elasticsearch/lib/ext/text_article.rb @@ -1,28 +1,36 @@ # REQUIRE TO LOAD DESCENDANTS FROM TEXT_ARTICLE require_dependency 'raw_html_article' require_dependency 'tiny_mce_article' - require_dependency 'text_article' -require_relative '../elasticsearch_indexed_model' - -class TextArticle - def self.profile_hash - { - :id => { type: :integer }, - :visible => { type: :boolean }, - :public_profile => { type: :boolean } - } - end +require_relative '../../helpers/searchable_model_helper' +require_relative '../../helpers/nested_helper/profile' +class TextArticle def self.control_fields { :advertise => { type: :boolean }, :published => { type: 'boolean'}, - :profile => { type: :nested , hash: self.profile_hash } + :profile => { type: :nested , hash: NestedProfile.hash } } end - include ElasticsearchIndexedModel + def self.should + [ + { and: [ + { term: { advertise: true }}, + { term: { published: true }} + ] + } + ] + end + + def self.nested_filter + [ + NestedProfile::filter + ] + end + + include SearchableModelHelper end diff --git a/plugins/elasticsearch/lib/ext/uploaded_file.rb b/plugins/elasticsearch/lib/ext/uploaded_file.rb index 17b974d..a31aeca 100644 --- a/plugins/elasticsearch/lib/ext/uploaded_file.rb +++ b/plugins/elasticsearch/lib/ext/uploaded_file.rb @@ -1,13 +1,32 @@ require_dependency 'uploaded_file' -require_relative '../elasticsearch_indexed_model' + +require_relative '../../helpers/searchable_model_helper' +require_relative '../../helpers/nested_helper/profile' class UploadedFile def self.control_fields { - :advertise => {}, - :published => {}, - :created_at => {type: 'date'} + :advertise => {type: :boolean}, + :published => {type: :boolean}, + :profile => { type: :nested , hash: NestedProfile.hash } } end - include ElasticsearchIndexedModel + + def self.should + [ + { and: [ + { term: { advertise: true }}, + { term: { published: true }} + ] + } + ] + end + + def self.nested_filter + [ + NestedProfile::filter + ] + end + + include SearchableModelHelper end diff --git a/plugins/elasticsearch/lib/nested_environment.rb b/plugins/elasticsearch/lib/nested_environment.rb deleted file mode 100644 index 7f5fb64..0000000 --- a/plugins/elasticsearch/lib/nested_environment.rb +++ /dev/null @@ -1,8 +0,0 @@ -module NestedEnvironment - def self.environment_hash - { - :id => { type: :integer }, - :is_default => {type: :boolean } - } - end -end -- libgit2 0.21.2