# Elasticsearch: Geo\_spacial search in a list of nested objects

**URL:** <https://discuss.elastic.co/t/elasticsearch-geo-spacial-search-in-a-list-of-nested-objects/177769>\
**Category:** Elasticsearch\
**Created:** [April 21, 2019, 5:41pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spacial-search-in-a-list-of-nested-objects/177769 "2019-04-21T17:41:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rachid\_59](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@rachid\_59](https://discuss.elastic.co/u/rachid_59)\
**Post date:** [April 21, 2019, 5:41pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spacial-search-in-a-list-of-nested-objects/177769/1 "2019-04-21T17:41:15Z")

</div>

I'm using Elasticsearch 6.3 and spring-data-elasticsearch, I have the index **restaurant** having the mapping below :

```auto
{
 "mapping": {
   "restaurant": {
     "properties": {
       "id": {
         "type": "keyword"
       },
       "name": {
         "type": "text",
         "store": true,
         "analyzer": "custom_analyzer",
         "search_analyzer": "custom_search_analyzer"
       },
       "locations": {
         "type": "nested",
         "include_in_parent": true,
         "properties": {
           "id": {
             "type": "long"
           },
           "fullAddress": {
             "type": "text"
           },
           "geo": {
             "type": "geo_point"
           },
           "phone": {
             "type": "keyword"
           }
         }
       },
       "menus": {
         "type": "nested",
         "properties": {
           "id": {
             "type": "long"
           },
           "name": {
             "type": "text",
             "store": true,
             "analyzer": "custom_analyzer",
             "search_analyzer": "custom_search_analyzer"
           }
         }
       }
     }
   }
 }
}

```

This is an example of a document of this mapping :

```auto
{
 "took": 1,
 "timed_out": false,
 "_shards": {
   "total": 5,
   "successful": 5,
   "skipped": 0,
   "failed": 0
 },
 "hits": {
   "total": 1,
   "max_score": 1,
   "hits": [
     {
       "_index": "restaurants",
       "_type": "restaurants",
       "_id": "1",
       "_score": 1,
       "_source": {
         "id": 1,
         "name": "Mc Donald",
         "menus": [
           {
             "id": 7097,
             "name": "Big Fish"
           },
           {
             "id": 13899,
             "name": "Big Mac"
           }
         ],
         "locations": [
           {
             "id": 1,
             "fullAddress": "7 Bd Solférino, 59000 Lille",
             "geo": { "lat": 50.6423461,"lon": 3.1390441},
             "phone": "0305060804"
           },
           {
             "id": 2,
             "fullAddress": "7 Rue Nationale, 75000 Paris",
             "geo": { "lat": 40.6423461,"lon": -7.1390441},
             "phone": "0305060804"
           }
         ]
       }
     }
   ]
 }
}

```

I try to search all the restaurants around me, for example, I search the nearest Mc Donald.

I tryed the 2 queries below but they return emty list :

- Query 1: (Using post filtering)

```auto
GET restaurants/_search
{
  "query": {
    "match": {
      "name": "donald"
    }
  },
  "post_filter": {
    "nested": {
      "path": "locations",
      "query": {
        "geo_distance": {
          "distance": "10km",
          "locations.geo": [50.6423461, 3.1390441]
        }
      }
    }
  }
}

```

- Query 2: (Using filtering inside query)

```auto
GET restaurants/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "donald"
          }
        }
      ],
      "filter": {
        "nested": {
          "path": "locations",
          "query": {
            "geo_distance": {
              "distance": "10km",
              "locations.geo": [50.6423461,3.1390441]
            }
          },
          "inner_hits": {}
        }
      }
    }
  }
}

```

PLEASE, could someone tell me what I do wrong in my mapping or queries.  
Thx in advance

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [April 22, 2019, 1:41pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spacial-search-in-a-list-of-nested-objects/177769/2 "2019-04-22T13:41:23Z")

</div>

The order of the geo points in the query is incorrect. Note that in that cases (array format) the position must be `[lon, lat]`, as defined here:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html)

Therefore this query should give you the expected answer:

```auto
GET restaurants/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "donald"
          }
        }
      ],
      "filter": {
        "nested": {
          "path": "locations",
          "query": {
            "geo_distance": {
              "distance": "10km",
              "locations.geo": [3.1390441, 50.6423461]
            }
          },
          "inner_hits": {}
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![rachid\_59](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@rachid\_59](https://discuss.elastic.co/u/rachid_59)\
**Post date:** [April 22, 2019, 3:13pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spacial-search-in-a-list-of-nested-objects/177769/3 "2019-04-22T15:13:10Z")

</div>

OK it's so simple, but I had to see it 😅  
It works like a charm 😉

Thank you very much.

---

<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:** [May 20, 2019, 3:13pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spacial-search-in-a-list-of-nested-objects/177769/4 "2019-05-20T15:13:13Z")

</div>

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