Open 12102man opened 5 years ago
Sorry for replying 🙇
This is an example to use the geospatial search
documents.py
from django.db import models
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
class Hotel(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255, null=False)
address = models.CharField(max_length=512, null=False)
lat = models.DecimalField(max_digits=9, decimal_places=6, null=False)
lng = models.DecimalField(max_digits=9, decimal_places=6, null=False)
class Meta:
db_table = 'hotels'
managed = True
@property
def location(self):
return [self.lng, self.lat]
@registry.register_document
class HotelDocument(Document):
name = fields.TextField()
address = fields.TextField()
location = fields.GeoPointField()
class Index:
name = 'hotels'
class Django:
model = Hotel
search.py
from elasticsearch_dsl import Q
from .documents import HotelDocument
def geo_search(lat, lng, distance):
q = Q('geo_distance', distance='{}km'.format(distance), location=[lng, lat])
s = HotelDocument.search()
s = s.query(q)
response = s.execute()
you also could check elasticsearch_dsl official site to understand how to search by es https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html
@ogiogi93 Thanks for your example. Maybe it can be added to the documentation?
Hey! Thank you for a cool library for elasticsearch! I'm doing my university project with your lib and I faced with the problem, that there's no example of geospatial search. I have the following structure:
"mappings": { "doc": { "properties": { "address": { "type": "text" }, "lat": { "type": "double" }, "lon": { "type": "double" } }
My question is how to perform a search and how to create GeoPoint attribute. Thank you!