model shop
class Shop(models.Model):
name = models.CharField(max_length=100)
lat = models.DecimalField(
null=True, blank=True, decimal_places=15, max_digits=19, default=0
)
long = models.DecimalField(
null=True, blank=True, decimal_places=15, max_digits=19, default=0
)
radius = models.FloatField(default=5)
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
@property
def location_field_indexing(self):
return {
"lat": self.lat,
"lon": self.long,
}
document
@registry.register_document
class ShopDocument(Document):
location = fields.GeoPointField(attr="location_field_indexing")
id = fields.TextField(attr="id")
name = fields.TextField(attr="name")
radius = fields.FloatField(attr="radius")
class Index:
name = "shops"
settings = {"number_of_shards": 1, "number_of_replicas": 0}
class Meta:
# parallel_indexing = True
# queryset_pagination = 50exit
fields = ["id", "name"]
class Django:
model = Shop
my lookup
lat = 10.943198385746596
lon = 76.66936405971812
distance = 1
shops = (
ShopDocument.search()
.filter(
Q(
"script_score",
query={"match_all": {}},
script={
"source": """
def distance = doc['location'].arcDistance(params.lat, params.lon);
def radius = doc['radius'].value;
def total_distance = params.dis.value + radius;
if (distance <= total_distance) {
return distance;
} else {
return 0;
}
""",
"params": {
"lat": lat,
"lon": lon,
"dis": {
"value": distance
},
},
},
)
)
.sort(
{
"_geo_distance": {
"location": {
"lat": lat,
"lon": lon,
},
"order": "asc",
"unit": "km",
"distance_type": "plane",
}
}
)
.extra(size=10, from_=0)
)
my requirement: get the shops list from the Elastic document that does not fare more than the value of the distance variable. but some of the shops provide their services to some kilometers. so I need to add a distance variable+service radius and then filter the shops.
the above lookup code is not showing any error but it returns all the shops, the filtering is not working. how can I solve this?