Updating nested in Elasticsearch DSL within DRF?

When updating nested objects in Django Rest Framework (DRF), they might be stored as objects instead of Nested, causing issues during search queries. How can I resolve this issue?

View file

profile = DetailsDocument.get(id=user_id)
profile.update(**serializer.validated_data)
profile.save() 

Document file:

    work_experience = Nested(
        properties={
            "employment_type": Keyword(),
            "job_title": Text(),
            "company": Text(),
            "start_date": Date(),
            "end_date": Date(),
            "job_description": Text(),
            "skills_acquired": Keyword(multi=True),
            "visibility": Boolean(),
            "verified": Boolean(),
        }
    )

Sample JSON Payload:

{
    "data": {
        "type": "Profile",
        "id": 19,
        "attributes": {
            "work_experience": [
                {
                    "employment_type": "Permanent",
                    "job_title": "Software Engineer",
                    "company": "TechCorp Inc.",
                    "start_date": "2017-06-01",
                    "end_date": "2020-12-31",
                    "job_description": "Developed web applications and software solutions.",
                    "skills_acquired": [
                        "Java",
                        "JavaScript",
                        "Database Management"
                    ],
                    "visibility": true,
                    "verified": true
                }
            ]
            }
        }
    }
}

Whether a certain level in the mappings is nested or not is something you must control through the mappings. Irrespective of the structure of the document, data will never be mapped as nested through dynamic mapping. If you are adding new subdocuments to your data model and you want these to be mapped as nested you need to explicitly update the mappings before you index that data so you avoid dynamic mapping.

from datetime import datetime
from elasticsearch_dsl import Document, Date, Nested, Boolean, \
    analyzer, InnerDoc, Completion, Keyword, Text

html_strip = analyzer('html_strip',
    tokenizer="standard",
    filter=["standard", "lowercase", "stop", "snowball"],
    char_filter=["html_strip"]
)

class Comment(InnerDoc):
    author = Text(fields={'raw': Keyword()})
    content = Text(analyzer='snowball')
    created_at = Date()

    def age(self):
        return datetime.now() - self.created_at

class Post(Document):
    title = Text()
    title_suggest = Completion()
    created_at = Date()
    published = Boolean()
    category = Text(
        analyzer=html_strip,
        fields={'raw': Keyword()}
    )

    comments = Nested(Comment)

    class Index:
        name = 'blog'

    def add_comment(self, author, content):
        self.comments.append(
          Comment(author=author, content=content, created_at=datetime.now()))

    def save(self, ** kwargs):
        self.created_at = datetime.now()
        return super().save(** kwargs)

View

# retrieve the document
first = Post.get(id=42)
# now we can call methods, change fields, ...
first.add_comment('me', 'This is nice!')
# and save the changes into the cluster again
first.save()

This way also stored as object it will i take from elasticsearch_dsl example