@@ -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 ``
796853and ``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
0 commit comments