# Query to Determine All Unique IDs that are distance X away from a particular ID?

**URL:** <https://discuss.elastic.co/t/query-to-determine-all-unique-ids-that-are-distance-x-away-from-a-particular-id/229813>\
**Category:** Elasticsearch\
**Created:** [April 26, 2020, 6:28am UTC](https://discuss.elastic.co/t/query-to-determine-all-unique-ids-that-are-distance-x-away-from-a-particular-id/229813 "2020-04-26T06:28:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brolo](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@brolo](https://discuss.elastic.co/u/brolo)\
**Post date:** [April 26, 2020, 6:28am UTC](https://discuss.elastic.co/t/query-to-determine-all-unique-ids-that-are-distance-x-away-from-a-particular-id/229813/1 "2020-04-26T06:28:36Z")

</div>

I have data in this format generated from a random walk (to simulate people walking around). It is set up in this manner { location : { lat: someLat, lon: someLong }, id: uniqueId, date:date }. I am trying to write a query given a users unique ID, find how many other unique IDs came within X distance of the given ID between a certain time range. Any hints on how to accomplish this?

My idea is to have a top level filter aggregration, with a nested geo-query of some sort. I think the geo-distance query is the way to go, but I am not sure how to include it into the below query to get all of unique IDs that come within X distance of the ID I am filtering on. The query below is where I am starting from, I am filtering all documents from now - 1 day to now, where the documents user Id is the provided value. How would I check all other documents for their distances against documents that match this query?

```auto
{
    "aggs" : {
        "range": {
            "date_range": {
                "field": "date",
                "format": "MM-yyyy",
                "ranges": [
                    { "to": "now" }, 
                    { "from": "now-1d" } 
                ]
            }
        },
        "locations" : {
            "filter" : { 
              "term": { "id.keyword": "7a50ab18-886b-42a2-80ad-3d45112e3cfd" }
            }
        }
    }
}

```

---

<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 24, 2020, 6:28am UTC](https://discuss.elastic.co/t/query-to-determine-all-unique-ids-that-are-distance-x-away-from-a-particular-id/229813/2 "2020-05-24T06:28:38Z")

</div>

Closed on May 24, 2020, 6:28am UTC

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