Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define how models are mapped to hits #1

Open
wants to merge 1 commit into
base: active-attr-6.x
Choose a base branch
from
Open
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 @@ -205,6 +205,30 @@ def inheritance_enabled
def inheritance_enabled=(inheritance_enabled)
@inheritance_enabled = inheritance_enabled
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