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

**URL:** <https://discuss.elastic.co/t/how-to-make-elasticsearch-returns-products-from-stores-that-deliver-to-the-users-region/277234>\
**Category:** Elasticsearch\
**Created:** [June 28, 2021, 1:50pm UTC](https://discuss.elastic.co/t/how-to-make-elasticsearch-returns-products-from-stores-that-deliver-to-the-users-region/277234 "2021-06-28T13:50:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ironzep](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@ironzep](https://discuss.elastic.co/u/ironzep)\
**Post date:** [June 28, 2021, 1:50pm UTC](https://discuss.elastic.co/t/how-to-make-elasticsearch-returns-products-from-stores-that-deliver-to-the-users-region/277234/1 "2021-06-28T13:50:55Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 29, 2021, 2:01am UTC](https://discuss.elastic.co/t/how-to-make-elasticsearch-returns-products-from-stores-that-deliver-to-the-users-region/277234/2 "2021-06-29T02:01:06Z")

</div>

Welcome to our community! 😃

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

```auto
{
      "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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 27, 2021, 2:01am UTC](https://discuss.elastic.co/t/how-to-make-elasticsearch-returns-products-from-stores-that-deliver-to-the-users-region/277234/3 "2021-07-27T02:01:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
