use existing field as _id using elasticsearch dsl python DocType
Question: I have class, where I try to set `student_id` as `_id` field in
[![](//i.stack.imgur.com/817gJ.png)elasticsearch](/questions/tagged/elasticsearch
"show questions tagged 'elasticsearch'"). I am referring [persistent
example](https://elasticsearch-dsl.readthedocs.io/en/latest/#persistence-
example) from elasticsearch-dsl docs.

    
    
    from elasticsearch_dsl import DocType, String
    
    ELASTICSEARCH_INDEX = 'student_index'
    
    class StudentDoc(DocType):
        '''
        Define mapping for Student type
        '''
    
        student_id = String(required=True)
        name = String(null_value='')
    
        class Meta:
            # id = student_id
            index = ELASTICSEARCH_INDEX
    

I tied by setting `id` in `Meta` but it not works.

I get solution as override [`save`
method](https://github.com/elastic/elasticsearch-dsl-py/issues/360) and I
achieve this

    
    
    def save(self, **kwargs):
        '''
        Override to set metadata id
        '''
        self.meta.id = self.student_id
        return super(StudentDoc, self).save(**kwargs)
    

I am creating this object as

    
    
    >>> a = StudentDoc(student_id=1, tags=['test'])
    >>> a.save()
    

Is there any direct way to set from `Meta` without override `save` method ?


Answer: There are a few ways to assign an id:

You can do it like this

    
    
    a = StudentDoc(meta={'id':1}, student_id=1, tags=['test'])
    a.save()
    

Like this:

    
    
    a = StudentDoc(student_id=1, tags=['test'])
    a.meta.id = 1
    a.save()
    

Also note that before ES 1.5, one was able to [specify a
field](https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-id-
field.html#_path) to use as the document `_id` (in your case, it could have
been `student_id`), but this has been deprecated in 1.5 and from then onwards
you must explicitly provide an ID or let ES pick one for you.

