How can I resolve issues with updating nested objects in Elasticsearch DSL within Django Rest Framework?

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

from .details_document import DetailsDocument

class Profile(APIView):
    def patch(self, request: Request):
        serializer = ProfileSerializer(data=request.data)

        if serializer.is_valid():
            user_id = request.token_payload["user_id"]
            try:
                profile = DetailsDocument.get(id=user_id)
                profile.update(**serializer.validated_data)
                profile.save()
                response_data = {
                    DATA: {
                        TYPE: "Profile",
                        ID: user_id,
                    },
                    META: {MESSAGE: "User profile details updated successfully"},
                }
                return custom_json_response(response_data)

Document file:

from elasticsearch_dsl import (
    Boolean,
    Date,
    Document,
    Float,
    Integer,
    Keyword,
    Nested,
    Object,
    Text,
)


# Define a reusable object for visibility properties
visibility_properties = Object(
    properties={
        "email": Boolean(),
    }
)


class UserJobDetailsIndex:
    """
    Configuration for Elasticsearch Index.
    """

    name = config("DB_TABLE_NAME_PREFIX") + "_test"


class DetailsDocument(Document):
    class Index:
        name = UserJobDetailsIndex.name

    username = Keyword()
    interests_hobbies = Keyword(multi=True)
    address = Object(
        properties={
            "street": Text(),
            "city": Text(),
            "state": Keyword(),
            "zip_code": Keyword(),
        }
    )

    education = Nested(
        properties={
            "type": Keyword(),
            "institution": Text(),
            "degree": Text(),
            "major": Text(),
            "percentage": Float(),
            "location": Text(),
            "start_date": Date(),
            "end_date": Date(),
        }
    )

    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(),
        }
    )

    skills = Nested(
        properties={
            "name": Keyword(),
            "proficiency": Keyword(),
            "total_experience_year": Integer(),
            "last_used": Integer(),
        }
    )

    job_preferences = Object(
        properties={
            "job_type": Keyword(multi=True),
            "salary_per_year": Object(
                properties={
                    "currency_code": Keyword(),
                    "current_bonus": Keyword(),
                }
            ),
        }
    )
    
    privacy_settings = Object(
        properties={
            "connection_visibility": visibility_properties,
            "block_list": Object(
                properties={
                    "user": Integer(multi=True),
                }
            ),
        }
    )

Sample JSON Payload:

{
    "data": {
        "type": "Profile",
        "id": 19,
        "attributes": {
            "full_name": "testuser",
            "date_of_birth": "1880-01-12",
            "interests_hobbies": [
                "Chat",
                "Programming",
                "Browsing"
            ],
            "address": {
                "street": "Boulevard des Champs-lyses,",
                "city": "Washing - ton, D.C.",
                "state": "TN",
                "zip_code": "612999"
            },
            "education": [
                {
                    "type": "Education",
                    "institution": "D'Amore-McKim School of Business, Northeastern & University.",
                    "degree": "Master of Science (M.Sc.) in Engineering",
                    "major": "Psychology - (Clinical & Counseling)",
                    "percentage": 89,
                    "location": "Sydney, NSW, Australia",
                    "start_date": "2010-09-01",
                    "end_date": "2013-06-15"
                },
                {
                    "type": "Education Break",
                    "start_date": "2013-07-01",
                    "end_date": "2014-12-31",
                    "location": "Various",
                    "description": "Took time off for self-exploration, learning, and personal development through independent study and experiences."
                }
            ],
            "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
                }
            ],
            "skills": [
                {
                    "name": "Java",
                    "proficiency": "Intermediate",
                    "total_experience_year": 6,
                    "last_used": 2023
                }
            ],
            "job_preferences": {
                "job_type": [
                    "Permanent"
                ],
                "salary_per_year": {
                    "currency_code": "INR",
                    "current_bonus": "100"
                }
            },
            "privacy_settings": {
                "connection_visibility": {
                    "email": false
                },
                "block_list": {
                    "user": [
                        56
                    ]
                }
            }
        }
    }
}

I expected all data to be in the correct format, but some of it is incorrectly mapped to different data types. What is the proper way to update this, and is there an easier method or package available to streamline this process?

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