How to make elasticsearch returns products from stores that deliver to the user's region?

I am struggling with this for a time. The scenario is the following: I have an elasticsearch index which has products and each product has a store. The store has fields that store its latitude, longitude and the max distance which it delivers (basically, it's the max radius). I also have the user's location (lat and lon).

What I need is this: user searches for a product. I need to show him only the ones that are in stores that deliveries to his region.

More logically: get every product which lat and lon of user are inside the store's max distance of delivery.

I don't know if it is possible... I achieved a very similar scenario, using geo distance query. The problem with it is that user needs to select the distance (max radius) that he wants.

Contextualizing: It would be very strange to search for a product, and it returns from a store that doesn't delivery to user.

This is the structure of documents which I am saving at the index.

  {
      "id" : 425406,
      "codAlfa" : "1",
      "description" : "Product",
      "store" : {
        "id" : 1,
        "name" : "testeapi",
        "lat": 51.489761,
        "lon": 0.122890,
        "maxDeliveryDistance" : 20 
      }
  }

Welcome to our community! :smiley:

You really want to move that lat and lon field into it's own geo field, eg;

{
      "id" : 425406,
      "codAlfa" : "1",
      "description" : "Product",
      "store" : {
        "id" : 1,
        "name" : "testeapi",
        "location": {
          "lat": 51.489761,
          "lon": 0.122890,
        },
        "maxDeliveryDistance" : 20 
      }
  }

And then have that location field mapped as a geopoint.

Otherwise it's likely they are mapped as some kind of numeric data, which you cannot do geo range queries on.