Skip to content

Commit 2df8d6d

Browse files
committed
fix: Handle negative timestamps returned from ES
Elastic search can return negative timestamps for histograms if the dates are pre-1970. This PR properly handles these pre-1970 dates. Thanks to @speedplane for the patch Closes django-haystack#1239
1 parent b7c4b4a commit 2df8d6d

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

haystack/backends/elasticsearch_backend.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
from __future__ import absolute_import, division, print_function, unicode_literals
44

5-
import datetime
65
import re
76
import warnings
7+
from datetime import datetime, timedelta
88

99
from django.conf import settings
1010
from django.core.exceptions import ImproperlyConfigured
@@ -586,13 +586,22 @@ def _process_results(self, raw_results, highlight=False,
586586
'queries': {},
587587
}
588588

589+
# ES can return negative timestamps for pre-1970 data. Handle it.
590+
def from_timestamp(tm):
591+
if tm >= 0:
592+
return datetime.utcfromtimestamp(tm)
593+
else:
594+
return datetime(1970, 1, 1) + timedelta(seconds=tm)
595+
589596
for facet_fieldname, facet_info in raw_results['facets'].items():
590597
if facet_info.get('_type', 'terms') == 'terms':
591598
facets['fields'][facet_fieldname] = [(individual['term'], individual['count']) for individual in facet_info['terms']]
592599
elif facet_info.get('_type', 'terms') == 'date_histogram':
593600
# Elasticsearch provides UTC timestamps with an extra three
594601
# decimals of precision, which datetime barfs on.
595-
facets['dates'][facet_fieldname] = [(datetime.datetime.utcfromtimestamp(individual['time'] / 1000), individual['count']) for individual in facet_info['entries']]
602+
facets['dates'][facet_fieldname] = [(from_timestamp(individual['time'] / 1000),
603+
individual['count'])
604+
for individual in facet_info['entries']]
596605
elif facet_info.get('_type', 'terms') == 'query':
597606
facets['queries'][facet_fieldname] = facet_info['count']
598607

@@ -706,10 +715,12 @@ def _to_python(self, value):
706715
for dk, dv in date_values.items():
707716
date_values[dk] = int(dv)
708717

709-
return datetime.datetime(
710-
date_values['year'], date_values['month'],
711-
date_values['day'], date_values['hour'],
712-
date_values['minute'], date_values['second'])
718+
return datetime(date_values['year'],
719+
date_values['month'],
720+
date_values['day'],
721+
date_values['hour'],
722+
date_values['minute'],
723+
date_values['second'])
713724

714725
try:
715726
# This is slightly gross but it's hard to tell otherwise what the

0 commit comments

Comments
 (0)