Skip to content

Commit 866b24f

Browse files
committed
Add tests for Solr/ES spatial order_by
This exists primarily to avoid the possibility of breaking compatibility with the inconsistent lat, lon ordering used by Django, Solr and ElasticSearch.
1 parent df8f525 commit 866b24f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

test_haystack/elasticsearch_tests/test_elasticsearch_backend.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from haystack.query import RelatedSearchQuerySet, SearchQuerySet, SQ
1616
from haystack.utils import log as logging
1717
from haystack.utils.loading import UnifiedIndex
18+
from haystack.utils.geo import Point
1819

1920
from ..core.models import (AFourthMockModel, AnotherMockModel, ASixthMockModel,
2021
MockModel)
@@ -436,6 +437,21 @@ def test_search(self):
436437
# Restore.
437438
settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = old_limit_to_registered_models
438439

440+
def test_spatial_search_parameters(self):
441+
p1 = Point(1.23, 4.56)
442+
kwargs = self.sb.build_search_kwargs('*:*', distance_point={'field': 'location', 'point': p1},
443+
sort_by=(('distance', 'desc'), ))
444+
445+
self.assertIn('sort', kwargs)
446+
self.assertEqual(1, len(kwargs['sort']))
447+
geo_d = kwargs['sort'][0]['_geo_distance']
448+
449+
# ElasticSearch supports the GeoJSON-style lng, lat pairs so unlike Solr the values should be
450+
# in the same order as we used to create the Point():
451+
# http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-filter.html#_lat_lon_as_array_4
452+
453+
self.assertDictEqual(geo_d, {'location': [1.23, 4.56], 'unit': 'km', 'order': 'desc'})
454+
439455
def test_more_like_this(self):
440456
self.sb.update(self.smmi, self.sample_objs)
441457
self.assertEqual(self.raw_search('*:*')['hits']['total'], 3)

test_haystack/solr_tests/test_solr_backend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from haystack.models import SearchResult
1717
from haystack.query import RelatedSearchQuerySet, SearchQuerySet, SQ
1818
from haystack.utils.loading import UnifiedIndex
19+
from haystack.utils.geo import Point
1920
from mock import patch
2021

2122
from ..core.models import (AFourthMockModel, AnotherMockModel, ASixthMockModel,
@@ -382,6 +383,17 @@ def test_search(self):
382383
# Restore.
383384
settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = old_limit_to_registered_models
384385

386+
def test_spatial_search_parameters(self):
387+
p1 = Point(1.23, 4.56)
388+
kwargs = self.sb.build_search_kwargs('*:*', distance_point={'field': 'location', 'point': p1},
389+
sort_by='distance asc')
390+
391+
# Points in Solr are lat, lon pairs but Django GIS Point() uses lon, lat so we'll check for the flip
392+
# See http://django-haystack.readthedocs.org/en/latest/spatial.html#points
393+
self.assertEqual(kwargs.get('pt'), '4.56,1.23')
394+
self.assertEqual(kwargs.get('sfield'), 'location')
395+
self.assertEqual(kwargs.get('sort'), 'geodist() asc')
396+
385397
def test_altparser_query(self):
386398
self.sb.update(self.smmi, self.sample_objs)
387399

0 commit comments

Comments
 (0)