elasticsearch-dsl-py icon indicating copy to clipboard operation
elasticsearch-dsl-py copied to clipboard

Elasticsearch attribute name different from Python name

Open lschmelzeisen opened this issue 5 years ago • 4 comments

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?

lschmelzeisen avatar Apr 19 '20 08:04 lschmelzeisen

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.

lschmelzeisen avatar May 06 '20 12:05 lschmelzeisen

It seems that you could use an attribute mapping in class Meta to define extra fields. link

craynic avatar Jul 07 '20 13:07 craynic

I'm not sure whether this does what I need it to do. Would you mind to provide some short example code?

lschmelzeisen avatar Jul 10 '20 13:07 lschmelzeisen

Also mentioned in #1659

miguelgrinberg avatar Mar 25 '24 17:03 miguelgrinberg

Closing this one, since it is a duplicate of #1659

miguelgrinberg avatar Apr 26 '24 15:04 miguelgrinberg