Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions elasticsearch-model/lib/elasticsearch/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,30 @@ def search(query_or_payload, models=[], options={})
request = Searching::SearchRequest.new(models, query_or_payload, options)
Response::Response.new(models, request)
end

# Define a rule for mapping hits to models when searching across multiple models
#
# @param model_to_hit_selector [lambda(model,hit)] - Must return a boolean
#
# @example Map indices to models that operate against aliases
#
# Elasticsearch::model_to_hit_selector = lambda do |model, hit|
# /#{model.index_name}-.*/ =~ hit[:_index] && model.document_type == hit[:_type]
# end
#
# @example Map indices to models but disregard the model's index name
#
# Elasticsearch::model_to_hit_selector = lambda do |model, hit|
# model.document_type == hit[:_type]
# end
#
def model_to_hit_selector=(model_to_hit_selector)
@model_to_hit_selector = model_to_hit_selector
end

def model_to_hit_selector
@model_to_hit_selector ||= lambda { |model, hit| model.index_name == hit[:_index] && model.document_type == hit[:_type] }
end
end
extend ClassMethods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __type_for_hit(hit)

@@__types[ "#{hit[:_index]}::#{hit[:_type]}" ] ||= begin
Registry.all.detect do |model|
model.index_name == hit[:_index] && model.document_type == hit[:_type]
Model.model_to_hit_selector.call(model, hit)
end
end
end
Expand Down