Skip to content

Commit

Permalink
fixed search on wiki pages; closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
nodecarter committed Aug 14, 2014
1 parent 1ce9bbd commit 60d1ce1
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 13 deletions.
4 changes: 4 additions & 0 deletions app/elastic/application_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def async_update_index

module ClassMethods

def index_document_type
self.name.underscore
end

def index_mappings
{
document_type => {
Expand Down
6 changes: 5 additions & 1 deletion app/serializers/base_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/wiki_page_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 8 additions & 0 deletions lib/redmine_elasticsearch.rb
Original file line number Diff line number Diff line change
@@ -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'])
Expand Down
4 changes: 2 additions & 2 deletions lib/redmine_elasticsearch/indexer_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/redmine_elasticsearch/patches/redmine_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/redmine_elasticsearch/serializer_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/workers/indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 60d1ce1

Please sign in to comment.