I have two models

  1. Author
  2. Book
  • Book is also a ManyToMany relationship field in Author class

My query is - I want to ingest my model data into Elasticsearch using django_elasticsearch_dsl, using pairs for Author & Book models

This is my index class and fields using the elasticsearch_dsl

from elasticsearch_dsl import Document

class AuthorBookPair(Document)
    author = Text()
    book = Text()
    isbn = Text()

class Index:
        name = "author-book-pair"
        settings = {"number_of_shards": 1, "number_of_replicas": 0}

if I would use the above class for mapping, my indexing queryset & code would look like this

    authors = Author.objects.all()
    
    for author in authors:
        for book in author.books.all():
            pair = AuthorBookPair()
            pair.book = book
            pair.isbn = book.isbn
            pair.save()

However I want to use django_elasticsearch_dsl whose index requires model definition in Django class like the example below

class AuthorBookPair(Document):
    author = fields.TextField()
    book = fields.TextField()
    isbn = fields.TextField()

    class Index:
        name = "author-book-pair"
        settings = {"number_of_shards": 1, "number_of_replicas": 0}

    class Django:
        model = Author 

    def get_queryset(self):
        Author.objects.all()

    def prepare_author(self,instance):
        return instance.author

How can I imitate the elasticsearch_dsl queryset combination and logic django_elasticsearch_dsl?

I tried using a for loop outside of the AuthorBookPair Class as a generator to later call while populating the field, but my Author query-set count and generator query-set count will mismatch, resulting in early termination

Is it even possible?

I tried using a for loop outside of the AuthorBookPair Class as a generator to later call while populating the field, but my Author query-set count and generator query-set count will mismatch, resulting in early termination

Is it even possible?

0

There are 0 best solutions below