Permalink
Browse files

Fixed distance handling in result parser of the elasticsearch backend…

…. This is basically the second part of #566. Thanks to Josh Drake for the initial patch.
  • Loading branch information...
1 parent 58f4edd commit 9d92d4da0a1ec75978fc3949375dda9a1707469f @jezdez jezdez committed Dec 3, 2012
Showing with 23 additions and 3 deletions.
  1. +23 −3 haystack/backends/elasticsearch_backend.py
View
26 haystack/backends/elasticsearch_backend.py
@@ -473,6 +473,13 @@ def search(self, query_string, **kwargs):
search_kwargs = self.build_search_kwargs(query_string, **kwargs)
search_kwargs['from'] = kwargs.get('start_offset', 0)
+ order_fields = set()
+ for order in search_kwargs.get('sort', []):
+ for key in order.keys():
+ order_fields.add(key)
+
+ geo_sort = '_geo_distance' in order_fields
+
end_offset = kwargs.get('end_offset')
start_offset = kwargs.get('start_offset', 0)
if end_offset is not None and end_offset > start_offset:
@@ -489,8 +496,10 @@ def search(self, query_string, **kwargs):
self.log.error("Failed to query Elasticsearch using '%s': %s", query_string, e)
raw_results = {}
- return self._process_results(raw_results, highlight=kwargs.get('highlight'),
- result_class=kwargs.get('result_class', SearchResult))
+ return self._process_results(raw_results,
+ highlight=kwargs.get('highlight'),
+ result_class=kwargs.get('result_class', SearchResult),
+ distance_point=kwargs.get('distance_point'), geo_sort=geo_sort)
def more_like_this(self, model_instance, additional_query_string=None,
start_offset=0, end_offset=None, models=None,
@@ -527,7 +536,9 @@ def more_like_this(self, model_instance, additional_query_string=None,
return self._process_results(raw_results, result_class=result_class)
- def _process_results(self, raw_results, highlight=False, result_class=None):
+ def _process_results(self, raw_results, highlight=False,
+ result_class=None, distance_point=None,
+ geo_sort=False):
from haystack import connections
results = []
hits = raw_results.get('hits', {}).get('total', 0)
@@ -580,6 +591,15 @@ def _process_results(self, raw_results, highlight=False, result_class=None):
if 'highlight' in raw_result:
additional_fields['highlighted'] = raw_result['highlight'].get(content_field, '')
+ if distance_point:
+ additional_fields['_point_of_origin'] = distance_point
+
+ if geo_sort and raw_result.get('sort'):
+ from haystack.utils.geo import Distance
+ additional_fields['_distance'] = Distance(km=float(raw_result['sort'][0]))
+ else:
+ additional_fields['_distance'] = None
+
result = result_class(app_label, model_name, source[DJANGO_ID], raw_result['_score'], **additional_fields)
results.append(result)
else:

0 comments on commit 9d92d4d

Please sign in to comment.