Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pyelasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
# For Python >= 2.6
import json

class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%dT%H:%M:%S')
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)


__author__ = 'Robert Eanes'
__all__ = ['ElasticSearch']
Expand Down Expand Up @@ -219,7 +227,7 @@ def _prep_request(self, body):
Encodes body as json.
"""
try:
return json.dumps(body)
return json.dumps(body, cls=DateEncoder)
except (TypeError, json.JSONDecodeError), e:
raise ElasticSearchError('Invalid JSON %r' % body, e)

Expand Down Expand Up @@ -260,6 +268,7 @@ def index(self, doc, index, doc_type, id=None, force_insert=False):
else:
request_method = 'PUT'
path = self._make_path([index, doc_type, id])

response = self._send_request(request_method, path, doc, querystring_args)
return response

Expand Down Expand Up @@ -477,4 +486,4 @@ def to_python(self, value):

if __name__ == "__main__":
import doctest
doctest.testmod()
doctest.testmod()