django-es / django-elasticsearch-dsl

This is a package that allows indexing of django models in elasticsearch with elasticsearch-dsl-py.
Other
1.01k stars 263 forks source link

ELastic Search Geo Point mapping #242

Open Shivendra2407 opened 4 years ago

Shivendra2407 commented 4 years ago

I have a model of Location which has the following fields:

class Location(models.Model):
    latitude = models.CharField(max_length=255, null=True, blank=True)
    longitude = models.CharField(max_length=255, null=True, blank=True)
    how_to_reach = models.TextField(default='', null=True, blank=True)

    class Meta:
        abstract = True

And another model City that inherits Location

class City(Location):
    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, null=True, blank=True)

I am using elasticsearch 6.3.2 and accordingly using django-elasticsearch-dsl and elasticsearch library versions, I have already used in another project so I know it is not compatibility issue of libraries and elasticsearch version. My question in how to use GeoPoint() field. In my document.py file I have:

from .models import City
from django_elasticsearch_dsl import Document, fields, GeoPoint
from django_elasticsearch_dsl.registries import registry

@registry.register_document
class CityDocument(Document):
    location = GeoPoint()
    class Index:
        name = 'cities'
        settings = {'number_of_shards': 1,
                    'number_of_replicas': 0}
    class Django:
        model = City
        fields = [
            'name',
            'slug',
            'id'
        ]

The latitude and longitude fields I have in model, how will I get them indexed when I use:

python manage.py search_index --rebuild

from terminal. Currently if I do this, only textual fields 'name', 'slug' and 'id' get indexed with each City doc, no location field is there in those docs.

Also, how would I query this using Search object from elasticsearch_dsl .

Appreciate all the help, thanks in advance!

barseghyanartur commented 4 years ago

Check this as a good example:

https://github.com/barseghyanartur/django-elasticsearch-dsl-drf/blob/master/examples/simple/books/models/location.py#L67

https://github.com/barseghyanartur/django-elasticsearch-dsl-drf/blob/master/examples/simple/search_indexes/documents/location.py#L205

Shivendra2407 commented 4 years ago

Hi, Thanks for the reply, really helpful. However, it shows default null for latitude and longitude in models, then when am rebuilding index it gives error of those records that will have None as latitude and longitude. As soon as I put a condition to return 0 as {'lat':0, 'lon':0} in case latitude and longitude are both None, it built index successfully.

How to deal with such situation? Can you please tell. Thanks in advance.

barseghyanartur commented 4 years ago

You could either:

P. S. Please, spend time on reading into documentation of this project, Elasticsearch itself and search the internet for solutions.

Shivendra2407 commented 4 years ago

Thanks a lot @barseghyanartur . Will definitely educate myself more on ES and django-es interaction.

barseghyanartur commented 4 years ago

This can be closed now.