Skip to content

Commit

Permalink
do not show error when searching on deleted items; refs #11
Browse files Browse the repository at this point in the history
  • Loading branch information
nodecarter committed Aug 13, 2014
1 parent 33bb140 commit dcc2a6e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/redmine_elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ module RedmineElasticsearch

require 'redmine_elasticsearch/patches/redmine_search'
require 'redmine_elasticsearch/patches/search_controller_patch'
require 'redmine_elasticsearch/patches/tire_patch'
63 changes: 63 additions & 0 deletions lib/redmine_elasticsearch/patches/tire_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module RedmineElasticsearch::Patches::TirePatch
extend ActiveSupport::Concern

included do
alias_method_chain :__get_results_with_load, :check
alias_method_chain :__find_records_by_ids, :check
end

def __get_results_with_load_with_check(hits)
return [] if hits.empty?

records = {}
@response['hits']['hits'].group_by { |item| item['_type'] }.each do |type, items|
raise NoMethodError, "You have tried to eager load the model instances, " +
"but Tire cannot find the model class because " +
"document has no _type property." unless type

begin
klass = type.camelize.constantize
rescue NameError => e
raise NameError, "You have tried to eager load the model instances, but " +
"Tire cannot find the model class '#{type.camelize}' " +
"based on _type '#{type}'.", e.backtrace
end

records[type] = Array(__find_records_by_ids klass, items.map { |h| h['_id'] })
end

# Reorder records to preserve the order from search results
@response['hits']['hits'].map do |item|
records[item['_type']].detect do |record|
record.id.to_s == item['_id'].to_s
end || dummy_document(item)
end
end

def __find_records_by_ids_with_check(klass, ids)
@options[:load] === true ? klass.where(id: ids) : klass.find(ids, @options[:load])
end

def dummy_document(hit)
document = {}

# Update the document with fields and/or source
document.update hit['_source'] if hit['_source']
document.update __parse_fields__(hit['fields']) if hit['fields']

# Set document ID
document['id'] = hit['_id']

# Update the document with meta information
['_score', '_type', '_index', '_version', 'sort', 'highlight', '_explanation'].each do |key|
document.update key => hit[key]
end

# Return an instance of the "wrapper" class
@wrapper.new(document)
end
end

unless Tire::Results::Collection.included_modules.include?(RedmineElasticsearch::Patches::TirePatch)
Tire::Results::Collection.send :include, RedmineElasticsearch::Patches::TirePatch
end

0 comments on commit dcc2a6e

Please sign in to comment.