# Retrieving only matching locations

**URL:** <https://discuss.elastic.co/t/retrieving-only-matching-locations/54590>\
**Category:** Elasticsearch\
**Created:** [July 2, 2016, 11:31pm UTC](https://discuss.elastic.co/t/retrieving-only-matching-locations/54590 "2016-07-02T23:31:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gokhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokhan/32/10691_2.png) [@gokhan](https://discuss.elastic.co/u/gokhan)\
**Post date:** [July 2, 2016, 11:31pm UTC](https://discuss.elastic.co/t/retrieving-only-matching-locations/54590/1 "2016-07-02T23:31:31Z")

</div>

Hi all,

I have an index that stores set of locations as **geo\_point**

```
    {
      ......,
      "locations" : [
         {
            "lat" : ...
            "lon" : ...
         },
        .......
      ]
    }

```

and I use query which works fine

```
{
   "sort": [{
      "_geo_distance": {
         "locations": {
            "lat": 38.7762021,
            "lon": 26.954897
         },
         "order": "asc",
         "unit": "km"
      }
   }],
   "query": {
      "filtered": {
         "filter": {
            "geo_distance": {
               "distance": "30km",
               "locations": {
                  "lat": 38.7762021,
                  "lon": 26.954897
               }
            }
         }
      }
   }
}

```

I just wonder if I can retrieve subset of locations. Lets say I have 1000 geo point in an index and I would like to retrieve 5 closest matching point. I have tried to make this with inner\_hits and nested\_filter. Also, I tried to sort the geo points but I could not manage it. I am not sure that I am on right track.

Could you please advise me if it is possible and if so how can I solve the problem?

Thanks

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 4, 2016, 7:32am UTC](https://discuss.elastic.co/t/retrieving-only-matching-locations/54590/2 "2016-07-04T07:32:12Z")

</div>

Hey,

can you share a fully-fledged example using nested/inner\_hits - this means, include your mapping, index creation, document indexing with geo points and your query - so others can follow your steps.

--Alex

---

<div class="post-metadata">

**Author:** ![gokhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokhan/32/10691_2.png) [@gokhan](https://discuss.elastic.co/u/gokhan)\
**Post date:** [July 8, 2016, 9:49pm UTC](https://discuss.elastic.co/t/retrieving-only-matching-locations/54590/3 "2016-07-08T21:49:45Z")

</div>

Hi Alex,

Here is the steps to reproduce the case.

`PUT localhost:9200/geo`

```
PUT /geo/test/_mapping
{
   "properties": {
      "locations": {
         "type": "geo_point"
      }
   }
}

```

And sample data to work on

```
POST localhost:9200/geo/test/1
{
    "locations": [
      {
        "lon": 27.09519515440661,
        "lat": 38.478413567935185
      },
      {
        "lon": 34.80236053466797,
        "lat": 39.81952667236328
      },
      {
        "lon": 39.83582532931803,
        "lat": 40.96827796407719
      },
      {
        "lon": 36.333538930028226,
        "lat": 41.28705790254833
      },
      {
        "lon": 30.37797194349696,
        "lat": 40.77331286046352
      },
      {
        "lon": 28.254863739013672,
        "lat": 37.468238830566406
      },
      {
        "lon": 34.62808663310784,
        "lat": 36.80608547444202
      },
      {
        "lon": 27.417057,
        "lat": 38.61295
      },
      {
        "lon": 30.023533189852515,
        "lat": 39.39730937110715
      },
      {
        "lon": 32.5051864924701,
        "lat": 37.89531395220301
      },
      {
        "lon": 32.508596011506825,
        "lat": 37.8492941159215
      },
      {
        "lon": 32.488811510405185,
        "lat": 37.87698760572874
      }
    ]
}

POST localhost:9200/geo/test/2
{
    "locations": [
      {
        "lon": 32.51198884183816,
        "lat": 37.90110180235243
      },
      {
        "lon": 32.03915426223398,
        "lat": 36.540804011837125
      },
      {
        "lon": 30.750701764736277,
        "lat": 36.890245468561396
      },
      {
        "lon": 30.595000683221542,
        "lat": 36.84250172907872
      },
      {
        "lon": 29.130974405626603,
        "lat": 36.67013612740797
      },
      {
        "lon": 28.442826999999966,
        "lat": 37.021167
      },
      {
        "lon": 27.357373237609863,
        "lat": 37.056478551035404
      },
      {
        "lon": 29.059229208694887,
        "lat": 37.81761128155548
      },
      {
        "lon": 27.260725335147754,
        "lat": 37.843887300205395
      },
      {
        "lon": 27.138333320617676,
        "lat": 38.3168448192717
      }
    ]
}

```

Query

```
POST localhost:9200/geo/test/_search
{
   "query": {
      "bool": {
         "must": [
            {
               "geo_distance": {
                  "distance": "100km",
                  "locations": {
                     "lon": 28.442826999999966,
                     "lat": 37.121167
                  }
               }
            }
         ]
      }
   },
   "inner_hits": {
      "matches": {
         "type": {
            "test": {   
               
            }
         }
      }
   },
   "sort": {
      "_geo_distance": {
         "locations": {
            "lon": 28.442826999999966,
            "lat": 37.121167
         },
         "order": "asc",
         "unit": "km"
      }
   }
}

```

I have tried almost every variation possible with inner\_hits yet I didn't manage to get what I want. I am also not sure **inner\_hits** is possible with geo points and it is the right way to solve the case.

```
"inner_hits": {
      "matchedpoints": {
         "type": {
            "test": {   
               "query": {
                  "geo_distance": {
                     "distance": "100km",
                     "locations": {
                        "lon": 28.442826999999966,
                        "lat": 37.121167
                     }
                  }
               }
            }
         }
      }
   }

```

what I want is to get/identify geo points that matches with given point and with **_from_** and **_size_** options of **inner\_hits** to limit locations such as closest 3 lat/lon.

Thanks.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 9, 2016, 1:05pm UTC](https://discuss.elastic.co/t/retrieving-only-matching-locations/54590/4 "2016-07-09T13:05:30Z")

</div>

Hey,

`inner_hits`only work, when nested or parent child is used, which is not configured in your mapping.

--Alex

---

<div class="post-metadata">

**Author:** ![gokhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokhan/32/10691_2.png) [@gokhan](https://discuss.elastic.co/u/gokhan)\
**Post date:** [July 9, 2016, 3:41pm UTC](https://discuss.elastic.co/t/retrieving-only-matching-locations/54590/5 "2016-07-09T15:41:20Z")

</div>

Hi,

Right, sorry I misread the documentation of **inner\_hits**. I thought it was the way to get geo points in order. I was not sure about it. In fact, I did what I need with custom groovy script that calculates each location and sorts in order that might be expensive since I had to iterate and calculate the distances again. I didn't test implications fully yet. However, I believe It could be extracted through [\_geo\_distance](https://github.com/elastic/elasticsearch/blob/b6f26c0c3794660902d6b69c9e2e7654822cd8ee/core/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortBuilder.java#L555) sorting. so It could be nice to have like

```
"sort": {
      "_geo_distance": {
         "locations": {
            "lon": 28.442826999999966,
            "lat": 37.101167
         },
         "order": "asc",
         "unit": "km",
         "extra" : {
               "name" : "closest_points",
               "limit" : 3
          }  
      }
   }

```

So it might be included \_source if it is possible. It might be useful such use cases.

If there is better inbuilt way to handle this issue instead of custom script, please let us know.

Thanks.

---

<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 5, 2017, 10:36pm UTC](https://discuss.elastic.co/t/retrieving-only-matching-locations/54590/6 "2017-07-05T22:36:50Z")

</div>


