From 60d1ce19f032ce3f481dd00c3ed2fcef1dffb14c Mon Sep 17 00:00:00 2001 From: Danil Tashkinov Date: Thu, 14 Aug 2014 17:15:09 +0400 Subject: [PATCH] fixed search on wiki pages; closes #10 --- app/elastic/application_search.rb | 4 ++++ app/serializers/base_serializer.rb | 6 +++++- app/serializers/wiki_page_serializer.rb | 4 ++++ init.rb | 2 +- lib/redmine_elasticsearch.rb | 8 ++++++++ lib/redmine_elasticsearch/indexer_service.rb | 4 ++-- lib/redmine_elasticsearch/patches/redmine_search.rb | 4 ++-- .../patches/search_controller_patch.rb | 2 +- lib/redmine_elasticsearch/serializer_service.rb | 2 +- lib/workers/indexer.rb | 9 ++++----- 10 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/elastic/application_search.rb b/app/elastic/application_search.rb index 613f7f6..4fabe02 100644 --- a/app/elastic/application_search.rb +++ b/app/elastic/application_search.rb @@ -19,6 +19,10 @@ def async_update_index module ClassMethods + def index_document_type + self.name.underscore + end + def index_mappings { document_type => { diff --git a/app/serializers/base_serializer.rb b/app/serializers/base_serializer.rb index af1d02e..fc758ac 100644 --- a/app/serializers/base_serializer.rb +++ b/app/serializers/base_serializer.rb @@ -16,10 +16,14 @@ def _routing route_key end - %w(datetime title description type).each do |attr| + %w(datetime title description).each do |attr| class_eval "def #{attr}() object.event_#{attr} end" end + def type + object.class.index_document_type + end + def author object.event_author && object.event_author.to_s end diff --git a/app/serializers/wiki_page_serializer.rb b/app/serializers/wiki_page_serializer.rb index d1c8bbe..136f8f1 100644 --- a/app/serializers/wiki_page_serializer.rb +++ b/app/serializers/wiki_page_serializer.rb @@ -9,6 +9,10 @@ def project_id object.wiki.try(:project_id) end + def _parent + object.wiki.try(:project_id) + end + def author nil end diff --git a/init.rb b/init.rb index 4e5b357..82d0e1f 100644 --- a/init.rb +++ b/init.rb @@ -8,7 +8,7 @@ name 'Full text searching plugin for Redmine' description 'This plugin integrates elasticsearch into Redmine' author 'Undev' - version '0.1.9' + version '0.1.10' url 'https://github.com/Undev/redmine_elasticsearch' requires_redmine :version_or_higher => '2.1' diff --git a/lib/redmine_elasticsearch.rb b/lib/redmine_elasticsearch.rb index 6780bad..13f384b 100644 --- a/lib/redmine_elasticsearch.rb +++ b/lib/redmine_elasticsearch.rb @@ -1,5 +1,13 @@ module RedmineElasticsearch INDEX_NAME = "#{Rails.application.class.parent_name.downcase}_#{Rails.env}" + + def self.type2class_name(type) + type.to_s.underscore.classify + end + + def self.type2class(type) + self.type2class_name(type).constantize + end end Tire::Configuration.url(Redmine::Configuration['elasticsearch_url']) diff --git a/lib/redmine_elasticsearch/indexer_service.rb b/lib/redmine_elasticsearch/indexer_service.rb index 6f6baca..bbc744a 100644 --- a/lib/redmine_elasticsearch/indexer_service.rb +++ b/lib/redmine_elasticsearch/indexer_service.rb @@ -73,12 +73,12 @@ def delete_index end def search_klasses - Redmine::Search.available_search_types.map { |type| type.to_s.classify.constantize } + Redmine::Search.available_search_types.map { |type| RedmineElasticsearch.type2class(type) } end def find_search_klass(search_type) validate_search_type(search_type) - search_type.to_s.classify.constantize + RedmineElasticsearch.type2class(search_type) end def validate_search_type(search_type) diff --git a/lib/redmine_elasticsearch/patches/redmine_search.rb b/lib/redmine_elasticsearch/patches/redmine_search.rb index 16d1e46..dd2032d 100644 --- a/lib/redmine_elasticsearch/patches/redmine_search.rb +++ b/lib/redmine_elasticsearch/patches/redmine_search.rb @@ -25,14 +25,14 @@ def update_search_methods private def include_search_methods(search_type) - search_klass = search_type.to_s.classify.constantize + search_klass = RedmineElasticsearch.type2class(search_type) include_methods(search_klass, ::ApplicationSearch) explicit_search_methods = detect_search_methods(search_type) include_methods(search_klass, explicit_search_methods) if explicit_search_methods end def detect_search_methods(search_type) - "::#{search_type.to_s.classify}Search".safe_constantize + "::#{RedmineElasticsearch.type2class_name(search_type)}Search".safe_constantize end def include_methods(klass, methods) diff --git a/lib/redmine_elasticsearch/patches/search_controller_patch.rb b/lib/redmine_elasticsearch/patches/search_controller_patch.rb index e4d23f7..7b25294 100644 --- a/lib/redmine_elasticsearch/patches/search_controller_patch.rb +++ b/lib/redmine_elasticsearch/patches/search_controller_patch.rb @@ -162,7 +162,7 @@ def perform_search(options = {}) common_should = [] document_types.each do |search_type| - search_klass = search_type.to_s.classify.constantize + search_klass = RedmineElasticsearch.type2class(search_type) type_query = search_klass.allowed_to_search_query(User.current) common_should << type_query if type_query end diff --git a/lib/redmine_elasticsearch/serializer_service.rb b/lib/redmine_elasticsearch/serializer_service.rb index 67c6ef2..546978e 100644 --- a/lib/redmine_elasticsearch/serializer_service.rb +++ b/lib/redmine_elasticsearch/serializer_service.rb @@ -18,7 +18,7 @@ def serializer_klass(object_type) end def build_serializer_klass(object_type) - parent = "#{object_type.to_s.classify}Serializer".safe_constantize || BaseSerializer + parent = "#{RedmineElasticsearch.type2class_name(object_type)}Serializer".safe_constantize || BaseSerializer serializer_klass = Class.new(parent) additional_props = additional_index_properties(object_type) add_additional_properties(serializer_klass, additional_props) if additional_props diff --git a/lib/workers/indexer.rb b/lib/workers/indexer.rb index 8d22c40..8ca9c56 100644 --- a/lib/workers/indexer.rb +++ b/lib/workers/indexer.rb @@ -10,12 +10,12 @@ class << self def defer(object_or_class) if object_or_class.is_a? Class - params = { type: object_or_class.name } + params = { type: object_or_class.index_document_type } self.perform_async(params) elsif object_or_class.id? params = { id: object_or_class.id, - type: object_or_class.event_type + type: object_or_class.class.index_document_type } self.perform_async(params) end @@ -34,14 +34,14 @@ def perform(options) end def update_class_index(type) - klass = type.classify.constantize + klass = RedmineElasticsearch.type2class(type) klass.update_index rescue ::RestClient::Exception, Errno::ECONNREFUSED => e raise IndexError, e, e.backtrace end def update_instance_index(type, id) - klass = type.classify.constantize + klass = RedmineElasticsearch.type2class(type) document = klass.find id document.update_index rescue ActiveRecord::RecordNotFound @@ -50,7 +50,6 @@ def update_instance_index(type, id) rescue ::RestClient::Exception, Errno::ECONNREFUSED => e raise IndexError, e, e.backtrace end - end end end