Skip to content

Commit

Permalink
feat: simplify data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Aug 20, 2024
1 parent 1c0df51 commit dcc4479
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/esse/index/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ def import(*repo_types, context: {}, eager_load_lazy_attributes: false, update_l
update_lazy_attributes = options.delete(:lazy_update_document_attributes) if options.key?(:lazy_update_document_attributes)
eager_load_lazy_attributes = options.delete(:eager_include_document_attributes) if options.key?(:eager_include_document_attributes)

doc_header_check = ->(doc, hash) do
hash['_id'] && hash['_id'].to_s == doc.id.to_s &&
hash['_routing'] == doc.routing &&
(LazyDocumentHeader::ACCEPTABLE_DOC_TYPES.include?(doc.type) && LazyDocumentHeader::ACCEPTABLE_DOC_TYPES.include?(hash['_type']) || doc.type == hash['_type'])
doc_header_check = ->(doc, (id, routing, type)) do
id && id.to_s == doc.id.to_s &&
routing == doc.routing &&
(LazyDocumentHeader::ACCEPTABLE_DOC_TYPES.include?(doc.type) && LazyDocumentHeader::ACCEPTABLE_DOC_TYPES.include?(type) || doc.type == type)
end

repo_hash.slice(*repo_types).each do |repo_name, repo|
Expand Down Expand Up @@ -308,8 +308,8 @@ def import(*repo_types, context: {}, eager_load_lazy_attributes: false, update_l
search_request[:routing] = routing if routing
hits = repo.index.search(**search_request).response.hits
hits.each do |hit|
header = hit.slice('_id', '_routing', '_type')
next if header['_id'].nil?
header = [hit['_id'], hit['_routing'], hit['_type']]
next if header[0].nil?

hit.dig('_source')&.each do |attr_name, attr_value|
real_attr_name = repo.lazy_document_attribute_names(attr_name).first
Expand All @@ -331,7 +331,7 @@ def import(*repo_types, context: {}, eager_load_lazy_attributes: false, update_l
lazy_attrs_to_update_after.each do |attr_name|
preloaded_ids = preload_search_result[attr_name].keys
filtered_docs = entries.reject do |doc|
doc.ignore_on_index? || preloaded_ids.any? { |h| doc_header_check.call(doc, h) }
doc.ignore_on_index? || preloaded_ids.any? { |header| doc_header_check.call(doc, header) }
end
next if filtered_docs.empty?

Expand Down

0 comments on commit dcc4479

Please sign in to comment.