Skip to content

Commit 03cf4b3

Browse files
prepare 0.4
1 parent a421aa4 commit 03cf4b3

File tree

13 files changed

+247
-20
lines changed

13 files changed

+247
-20
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ are used for versioning (schema follows below):
1717

1818
0.4
1919
---
20-
2017-mm-dd (not yet released)
20+
2017-09-26
2121

2222
- ``GeoSpatialFilteringFilterBackend`` filtering backend, supporting
23-
``geo_distance``, ``geo_distance_range``, ``geo_distance_gt``,
24-
``geo_distance_gte``, ``geo_distance_lt`` and ``geo_distance_lte``
25-
geo-spatial queries.
26-
- ``GeoSpatialOrderingFilterBackend`` ordering backend, supporting ?.
23+
``geo_distance`` and ``geo_polygon`` geo-spatial queries.
24+
- ``GeoSpatialOrderingFilterBackend`` ordering backend, supporting
25+
ordering of results for ``geo_distance`` filter.
2726
- ``OrderingFilterBackend`` is no longer provides defaults when no ordering is
2827
given. In order to take care of the defaults include the
29-
``DefaultOrderingFilterBackend`` in the list of ``filter_backends``.
28+
``DefaultOrderingFilterBackend`` in the list of ``filter_backends`` (after
29+
all other ordering backends).
3030

3131
0.3.12
3232
------

ROADMAP.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ Roadmap
22
=======
33
Road-map/upcoming releases.
44

5-
0.4
5+
0.5
66
---
77
yyyy-mm-dd (future)
88

9-
- All filters (mentioned in the ``constants`` module) implemented.
10-
- Awesome documentation.
9+
- Improved geo-spatial search/filtering/ordering support.
1110

12-
0.5
11+
0.6
1312
---
1413
yyyy-mm-dd (future)
1514

16-
- Add support for geo spatial search/filtering/ordering.
15+
- All filters (mentioned in the ``constants`` module) implemented.
16+
- Aggregations (also for geo-spatial backends).
17+
- Awesome documentation.

advanced_usage_examples.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,23 @@ Filter documents by radius of 100000km from the given location.
732732
733733
http://localhost:8000/search/publishers/?location__geo_distance=100000km|12.04|-63.93
734734
735+
**Geo-polygon filtering**
736+
737+
Filter documents that are located in the given polygon.
738+
739+
.. code-block:: text
740+
741+
http://localhost:8000/search/publishers/?location__geo_polygon=40,-70|30,-80|20,-90
742+
743+
Ordering
744+
~~~~~~~~
745+
746+
**Geo-distance ordering**
747+
748+
.. code-block:: text
749+
750+
http://localhost:8000/search/publishers/?ordering=location|48.85|2.30|km|plane
751+
735752
Suggestions
736753
-----------
737754

docs/advanced_usage_examples.rst

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ Sample models
6565
state_province = models.CharField(max_length=30)
6666
country = models.CharField(max_length=50)
6767
website = models.URLField()
68+
latitude = models.DecimalField(null=True,
69+
blank=True,
70+
decimal_places=15,
71+
max_digits=19,
72+
default=0)
73+
longitude = models.DecimalField(null=True,
74+
blank=True,
75+
decimal_places=15,
76+
max_digits=19,
77+
default=0)
6878
6979
class Meta(object):
7080
"""Meta options."""
@@ -74,6 +84,17 @@ Sample models
7484
def __str__(self):
7585
return self.name
7686
87+
@property
88+
def location_field_indexing(self):
89+
"""Location for indexing.
90+
91+
Used in Elasticsearch indexing/tests of `geo_distance` native filter.
92+
"""
93+
return {
94+
'lat': self.latitude,
95+
'lon': self.longitude,
96+
}
97+
7798
7899
@python_2_unicode_compatible
79100
class Author(models.Model):
@@ -379,6 +400,7 @@ Sample view
379400
from django_elasticsearch_dsl_drf.filter_backends import (
380401
FilteringFilterBackend,
381402
OrderingFilterBackend,
403+
DefaultOrderingFilterBackend,
382404
SearchFilterBackend,
383405
)
384406
from django_elasticsearch_dsl_drf.views import BaseDocumentViewSet
@@ -397,6 +419,7 @@ Sample view
397419
filter_backends = [
398420
FilteringFilterBackend,
399421
OrderingFilterBackend,
422+
DefaultOrderingFilterBackend,
400423
SearchFilterBackend,
401424
]
402425
# Define search fields
@@ -600,6 +623,20 @@ the example below, documents would be ordered first by field
600623
601624
http://127.0.0.1:8080/search/books/?search=title|lorem&ordering=-publication_date&ordering=price
602625
626+
Ids filter
627+
----------
628+
Filters documents that only have the provided ids.
629+
630+
.. code-block:: text
631+
632+
http://127.0.0.1:8000/api/articles/?ids=68|64|58
633+
634+
Or, alternatively:
635+
636+
.. code-block:: text
637+
638+
http://127.0.0.1:8000/api/articles/?ids=68&ids=64&ids=58
639+
603640
Faceted search
604641
--------------
605642

@@ -695,6 +732,23 @@ Filter documents by radius of 100000km from the given location.
695732
696733
http://localhost:8000/search/publishers/?location__geo_distance=100000km|12.04|-63.93
697734
735+
**Geo-polygon filtering**
736+
737+
Filter documents that are located in the given polygon.
738+
739+
.. code-block:: text
740+
741+
http://localhost:8000/search/publishers/?location__geo_polygon=40,-70|30,-80|20,-90
742+
743+
Ordering
744+
~~~~~~~~
745+
746+
**Geo-distance ordering**
747+
748+
.. code-block:: text
749+
750+
http://localhost:8000/search/publishers/?ordering=location|48.85|2.30|km|plane
751+
698752
Suggestions
699753
-----------
700754

@@ -787,6 +841,9 @@ To make use of suggestions, you should properly indexed your documents using
787841
788842
website = fields.StringField()
789843
844+
# Location
845+
location = fields.GeoPointField(attr='location_field_indexing')
846+
790847
class Meta(object):
791848
"""Meta options."""
792849
@@ -795,6 +852,50 @@ To make use of suggestions, you should properly indexed your documents using
795852
After that the ``name.suggest``, ``city.suggest``, ``state_province.suggest``
796853
and ``country.suggest`` fields would be available for suggestions feature.
797854

855+
856+
Serializer definition
857+
---------------------
858+
859+
This is how publisher serializer would look like.
860+
861+
*search_indexes/serializers.py*
862+
863+
.. code-block:: python
864+
865+
import json
866+
867+
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
868+
869+
class PublisherDocumentSerializer(DocumentSerializer):
870+
"""Serializer for Publisher document."""
871+
872+
location = serializers.SerializerMethodField()
873+
874+
class Meta(object):
875+
"""Meta options."""
876+
877+
# Note, that since we're using a dynamic serializer,
878+
# we only have to declare fields that we want to be shown. If
879+
# somehow, dynamic serializer doesn't work for you, either extend
880+
# or declare your serializer explicitly.
881+
fields = (
882+
'id',
883+
'name',
884+
'info',
885+
'address',
886+
'city',
887+
'state_province',
888+
'country',
889+
'website',
890+
)
891+
892+
def get_location(self, obj):
893+
"""Represent location value."""
894+
try:
895+
return obj.location.to_dict()
896+
except:
897+
return {}
898+
798899
ViewSet definition
799900
~~~~~~~~~~~~~~~~~~
800901

@@ -863,6 +964,15 @@ the following way:
863964
},
864965
}
865966
967+
# Geo-spatial filtering fields
968+
geo_spatial_filter_fields = {
969+
'location': {
970+
'lookups': [
971+
LOOKUP_FILTER_GEO_DISTANCE,
972+
],
973+
},
974+
}
975+
866976
In the example below, we show suggestion results (auto-completion) for
867977
``country`` field.
868978

docs/basic_usage_examples.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ books/models.py:
4040
state_province = models.CharField(max_length=30)
4141
country = models.CharField(max_length=50)
4242
website = models.URLField()
43+
latitude = models.DecimalField(null=True,
44+
blank=True,
45+
decimal_places=15,
46+
max_digits=19,
47+
default=0)
48+
longitude = models.DecimalField(null=True,
49+
blank=True,
50+
decimal_places=15,
51+
max_digits=19,
52+
default=0)
4353
4454
class Meta(object):
4555
"""Meta options."""
@@ -49,6 +59,17 @@ books/models.py:
4959
def __str__(self):
5060
return self.name
5161
62+
@property
63+
def location_field_indexing(self):
64+
"""Location for indexing.
65+
66+
Used in Elasticsearch indexing/tests of `geo_distance` native filter.
67+
"""
68+
return {
69+
'lat': self.latitude,
70+
'lon': self.longitude,
71+
}
72+
5273
Sample document
5374
---------------
5475

@@ -108,6 +129,9 @@ search_indexes/documents/publisher.py:
108129
)
109130
website = fields.StringField()
110131
132+
# Location
133+
location = fields.GeoPointField(attr='location_field_indexing')
134+
111135
class Meta(object):
112136
"""Meta options."""
113137
@@ -128,6 +152,8 @@ search_indexes/serializers.py:
128152
class PublisherDocumentSerializer(DocumentSerializer):
129153
"""Serializer for Publisher document."""
130154
155+
location = serializers.SerializerMethodField()
156+
131157
class Meta(object):
132158
"""Meta options."""
133159
@@ -146,13 +172,23 @@ search_indexes/serializers.py:
146172
'website',
147173
)
148174
175+
def get_location(self, obj):
176+
"""Represent location value."""
177+
try:
178+
return obj.location.to_dict()
179+
except:
180+
return {}
181+
149182
Sample view
150183
-----------
151184

152185
search_indexes/views.py:
153186

154187
.. code-block:: python
155188
189+
from django_elasticsearch_dsl_drf.constants import (
190+
LOOKUP_FILTER_GEO_DISTANCE,
191+
)
156192
from django_elasticsearch_dsl_drf.filter_backends import (
157193
FilteringFilterBackend,
158194
OrderingFilterBackend,
@@ -173,6 +209,7 @@ search_indexes/views.py:
173209
filter_backends = [
174210
FilteringFilterBackend,
175211
OrderingFilterBackend,
212+
DefaultOrderingFilterBackend,
176213
SearchFilterBackend,
177214
]
178215
# Define search fields
@@ -201,6 +238,14 @@ search_indexes/views.py:
201238
}
202239
# Specify default ordering
203240
ordering = ('id', 'name',)
241+
# Define geo-spatial filtering fields
242+
geo_spatial_filter_fields = {
243+
'location': {
244+
'lookups': [
245+
LOOKUP_FILTER_GEO_DISTANCE,
246+
],
247+
},
248+
}
204249
205250
Usage example
206251
-------------
@@ -321,6 +366,15 @@ Filter documents by a part word part in single field (``city``) "ondon".
321366
322367
http://127.0.0.1:8080/search/publisher/?city__wildcard=*ondon
323368
369+
370+
**Geo-distance filtering**
371+
372+
Filter documents by radius of 100000km from the given location.
373+
374+
.. code-block:: text
375+
376+
http://127.0.0.1:8000/search/publishers/?location__geo_distance=100000km|12.04|-63.93
377+
324378
Ordering
325379
^^^^^^^^
326380

docs/changelog.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ are used for versioning (schema follows below):
1515
0.3.4 to 0.4).
1616
- All backwards incompatible changes are mentioned in this document.
1717

18+
0.4
19+
---
20+
2017-09-26
21+
22+
- ``GeoSpatialFilteringFilterBackend`` filtering backend, supporting
23+
``geo_distance`` and ``geo_polygon`` geo-spatial queries.
24+
- ``GeoSpatialOrderingFilterBackend`` ordering backend, supporting
25+
ordering of results for ``geo_distance`` filter.
26+
- ``OrderingFilterBackend`` is no longer provides defaults when no ordering is
27+
given. In order to take care of the defaults include the
28+
``DefaultOrderingFilterBackend`` in the list of ``filter_backends`` (after
29+
all other ordering backends).
30+
1831
0.3.12
1932
------
2033
2017-09-21

docs/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ Main features and highlights
4444
``isnull``, ``range``, ``in``, ``term`` and ``terms``
4545
is implemented.
4646
- :doc:`Geo-spatial filtering filter backend <advanced_usage_examples>` (the
47+
following filters implemented: ``geo_distance``, ``geo_polygon``).
48+
- :doc:`Geo-spatial ordering filter backend <advanced_usage_examples>` (the
4749
following filters implemented: ``geo_distance``).
4850
- :doc:`Faceted search filter backend <advanced_usage_examples>`.
4951
- :doc:`Suggester filter backend <advanced_usage_examples>`.
5052
- :doc:`Pagination (Page number and limit/offset pagination) <advanced_usage_examples>`.
51-
- Ids filter backend.
53+
- :doc:`Ids filter backend <advanced_usage_examples>`.
5254

5355
Installation
5456
============

0 commit comments

Comments
 (0)