elasticsearch-dsl-py
elasticsearch-dsl-py copied to clipboard
Elasticsearch attribute name different from Python name
Hi, I currently have a document definition similar to the following:
class BlogPost(Document):
title = Text()
content = Text()
type = Keyword()
However, my code style dictates that I avoid shadowing Python-interals, such as type() here.
Therefore, I'd like to change the name of the class attribute without changing the name in the Elasticsearch mapping. I'm thinking of syntax similar to the following:
class BlogPost(Document):
title = Text()
content = Text()
type_ = Keyword(name="type")
Is something like this supported?
I just noticed that there is an even better reason for needing this. I currently have an ElasticSearch index with a mapping that I do not want to change (to expensive to reindex). Here from is used as a field name. Is there any way to assign a mapping to this field with elasticsearch-dsl?
class BlogPost(Document):
from = Keyword()
The conventional way of having it as a class attribute fails because from is a Python keyword and not allowed in this place.
It seems that you could use an attribute mapping in class Meta to define extra fields.
link
I'm not sure whether this does what I need it to do. Would you mind to provide some short example code?
Also mentioned in #1659
Closing this one, since it is a duplicate of #1659