# Updating nested in Elasticsearch DSL within DRF?

**URL:** <https://discuss.elastic.co/t/updating-nested-in-elasticsearch-dsl-within-drf/356014>\
**Category:** Elasticsearch\
**Created:** [March 22, 2024, 6:02pm UTC](https://discuss.elastic.co/t/updating-nested-in-elasticsearch-dsl-within-drf/356014 "2024-03-22T18:02:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manikandan\_FMJ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manikandan_fmj/32/132664_2.png) [@Manikandan\_FMJ](https://discuss.elastic.co/u/Manikandan_FMJ)\
**Post date:** [March 22, 2024, 6:02pm UTC](https://discuss.elastic.co/t/updating-nested-in-elasticsearch-dsl-within-drf/356014/1 "2024-03-22T18:02:19Z")

</div>

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**

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

```

**Document file:**

```auto
    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:**

```auto
{
    "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
                }
            ]
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 24, 2024, 8:05am UTC](https://discuss.elastic.co/t/updating-nested-in-elasticsearch-dsl-within-drf/356014/2 "2024-03-24T08:05:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Manikandan\_FMJ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manikandan_fmj/32/132664_2.png) [@Manikandan\_FMJ](https://discuss.elastic.co/u/Manikandan_FMJ)\
**Post date:** [March 25, 2024, 4:14pm UTC](https://discuss.elastic.co/t/updating-nested-in-elasticsearch-dsl-within-drf/356014/3 "2024-03-25T16:14:24Z")

</div>

> [@Manikandan\_FMJ](#):
>
> `work_experience`

```auto
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

```auto
# 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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 22, 2024, 4:14pm UTC](https://discuss.elastic.co/t/updating-nested-in-elasticsearch-dsl-within-drf/356014/4 "2024-04-22T16:14:45Z")

</div>

Closed on April 22, 2024, 4:14pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
