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

Lazy document attribute #16

Merged
merged 10 commits into from
Jul 10, 2024
Merged

Lazy document attribute #16

merged 10 commits into from
Jul 10, 2024

Conversation

marcosgz
Copy link
Owner

@marcosgz marcosgz commented Jul 9, 2024

Lazy Document attributes

The idea is to define document attributes that can be lazy loaded and updated using update or bulk update operations. This is useful for example when a document has nested and denormalized fields that are expensive to update the whole document. So, you can retrieve the value of the attribute and partially update the document.

The lazy document attribute can be defined on the index repository level:

class PostsIndex < Esse::Index
  repository :post do
    colection do
      # ...
    end

    document do |post|
      {
        _id: post.id,
        title: post.title,
      }
    end

    lazy_document_attribute :comments do |posts|
      Comment.where(post_id: posts.map(&:id)).group(:post_id).transform_values(&:count)
    end
  end
end

Note that the result of lazy_document_attribute is a hash where the key is one of the given post or its id.

So you to partially update the document by updating the comments count:

PostsIndex::Post.update_documents_attribute(:comments, postid1, postid2)

This could be part of some callback in the model or a background job.

You can also import documents by eager loading the lazy attribute or lazy loading it:

# Eager loading it, means that the attribute will be added to the document before bulk indexing it
PostsIndex.import(context: context, eager_include_document_attributes: [:comments])

# Lazy loading it, means that the attribute will be add to the document with a bulk update request after bulk indexing it
PostsIndex.import(context: context, lazy_update_document_attributes: [:comments])

@marcosgz marcosgz merged commit 9d9e0fd into main Jul 10, 2024
30 of 32 checks passed
@marcosgz marcosgz deleted the marcosgz/lazy-update-attr branch July 10, 2024 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant