How can i change the _id for the document django elasticsearch dsl python

742 views Asked by At

For thew document class i want to change to _id used to be a slug or another option

@registry.register_document
class TestDocument(Document):
id = fields.IntegerField(attr='id', multi=True)

name = fields.TextField(
    analyzer=html_strip,
    fields={
        'raw': fields.TextField(analyzer='keyword'),
    }
)

built_area = fields.TextField(
    analyzer=html_strip,
    fields={
        'raw': fields.TextField(analyzer='keyword'),
    }
)
living_area = fields.TextField(
    analyzer=html_strip,
    fields={
        'raw': fields.TextField(analyzer='keyword'),
    }
)

project = fields.ObjectField(properties={
    'name': fields.TextField(),
    'pk': fields.TextField(),
})

this thread mentions the meta, but do not works

1

There are 1 answers

0
Hetdev On

the right way to do it is using the document function generate_id

def generate_id(self, object_instance):
    return self.project.id

the final document will be:

...
project = fields.ObjectField(properties={
    'name': fields.TextField(),
    'pk': fields.TextField(),
})

def generate_id(self, object_instance):
    return self.project.id

class Index:
    # Name of the Elasticsearch index
    name = 'typologies_test'