Please refer this issue.
Basically the need is to have type casting for objects that can't be serialized. In this case that object is a set which should be type casted to a list.
@jaisharma639 I'm sorry to hear you're having problems. But the good news is that there's a solution to your problem! The python client by default uses the JSONSerializer. But you are able to overwrite that serializer with any serializer you want to use.
es = Elasticsearch(serializer=MyCustomSerializer)
Just did a quick little search for python json serializer sets and this stack overflow came up. By default a set is not a native datatype that the JSON spec can handle. So as such it needs to be special cased.
I'm more than thrilled to read your response, couldn't find this anywhere. I think the only problem here was not able to find this particular documentation anywhere. Maybe it didn't come up in the top search hits. Anyway, really appreciate the response.
If you want to share your serializer I'll make an example of it and put it in the docs.
Hopefully make things easier for the next person!
Here's how you implement your own custom serializer on client side.
from elasticsearch.serializer import JSONSerializer
class SetEncoder(JSONSerializer):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
if isinstance(obj, Something):
return 'CustomSomethingRepresentation'
return JSONSerializer.default(self, obj)
and just call it while initializing your connection instance:
es = Elasticsearch(serializer=SetEncoder())
@fxdgear , does it look ok?
Can i raise a PR updating the documentation with this example?
Can i raise a PR updating the documentation with this example?
Yes that would be great! :)
@fxdgear , can you please review my PR?
Most helpful comment
Here's how you implement your own custom serializer on client side.
and just call it while initializing your connection instance:
es = Elasticsearch(serializer=SetEncoder())@fxdgear , does it look ok?
Can i raise a PR updating the documentation with this example?