You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are trying to use the Documents API to connect with our existing Elasticsearch server however, we have a convention of prefixing our field names with '@' ("field1" = "value" is persisted as "@field1" = "value"). What we are looking for is a way to define custom names to properties as such:
The current way we are approaching this problem is by overriding the parent to_dict() method with the same to_dict method with just the key name adjusted.
classBaseDocument(Document):
defto_dict(self, include_meta=False, skip_empty=True):
d=self._serialize(skip_empty=skip_empty)
ifnotinclude_meta:
returndmeta= {"_"+k: self.meta[k] forkinDOC_META_FIELDSifkinself.meta}
# in case of to_dict include the index unlike save/update/deleteindex=self._get_index(required=False)
ifindexisnotNone:
meta["_index"] =indexmeta["_source"] =dreturnmetadef_serialize(self, skip_empty=True):
out= {}
fork, vinself._d_.items():
# if this is a mapped field,try:
f=self._doc_type.mapping[k]
exceptKeyError:
passelse:
iffandf._coerce:
v=f.serialize(v)
# if someone assigned AttrList, unwrap itifisinstance(v, AttrList):
v=v._l_ifskip_empty:
# don't serialize empty values# careful not to include numeric zerosifvin ([], {}, None):
continueout[f"@{k}"] =vreturnout
However, this solution isn't the most favorable way of doing this while working on the init() and save() methods it may result in problems with the other methods of the class in the future. Is there a better way of doing this or a future plan to support custom field names?
The text was updated successfully, but these errors were encountered:
I am sorry, but there is no better way of doing it. By prefixing fields with @ you have practically disabled any functionality of the DSL library which tries to treat documents/fields in a pythonic way. There is just no way to treat something that is not a valid python identifier as a python object.
Introducing possible aliases would be a way but currently there is no one maintaining the library. See #1652
We are trying to use the Documents API to connect with our existing Elasticsearch server however, we have a convention of prefixing our field names with '@' ("field1" = "value" is persisted as "@field1" = "value"). What we are looking for is a way to define custom names to properties as such:
The current way we are approaching this problem is by overriding the parent to_dict() method with the same to_dict method with just the key name adjusted.
However, this solution isn't the most favorable way of doing this while working on the init() and save() methods it may result in problems with the other methods of the class in the future. Is there a better way of doing this or a future plan to support custom field names?
The text was updated successfully, but these errors were encountered: