From d5cc42f4ddaa800b5a19e73eecdecdce50954f64 Mon Sep 17 00:00:00 2001 From: Josh Drake Date: Wed, 9 May 2012 14:27:51 -0500 Subject: [PATCH] Properly handle distance information returned in ElasticSearch results. --- haystack/backends/elasticsearch_backend.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/haystack/backends/elasticsearch_backend.py b/haystack/backends/elasticsearch_backend.py index 9c03d3c5b..55c75c4b9 100644 --- a/haystack/backends/elasticsearch_backend.py +++ b/haystack/backends/elasticsearch_backend.py @@ -299,6 +299,7 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None, "unit" : "km" } } + geo_sort = True else: if field == 'distance': warnings.warn("In order to sort by distance, you must call the '.distance(...)' method.") @@ -495,7 +496,7 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None, self.log.error("Failed to query Elasticsearch using '%s': %s", query_string, e) raw_results = {} - return self._process_results(raw_results, highlight=highlight, result_class=result_class) + return self._process_results(raw_results, highlight=highlight, result_class=result_class, distance_point=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, @@ -534,7 +535,7 @@ 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) @@ -587,6 +588,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: