# Filter by ID without providing ID (turn 2 queries into one)

**URL:** <https://discuss.elastic.co/t/filter-by-id-without-providing-id-turn-2-queries-into-one/258837>\
**Category:** Elasticsearch\
**Created:** [December 16, 2020, 10:18am UTC](https://discuss.elastic.co/t/filter-by-id-without-providing-id-turn-2-queries-into-one/258837 "2020-12-16T10:18:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mekanix](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@mekanix](https://discuss.elastic.co/u/mekanix)\
**Post date:** [December 16, 2020, 10:18am UTC](https://discuss.elastic.co/t/filter-by-id-without-providing-id-turn-2-queries-into-one/258837/1 "2020-12-16T10:18:06Z")

</div>

My problem can be distilled and exemplified by these 4 documents:

```json
{
    "_id": 1,
    "owner": "A",
    "distance": 1,
    "age": 2
},
{
    "_id": 2,
    "owner": "A",
    "distance": 2,
    "age": 1
},
{
    "_id": 3,
    "owner": "B",
    "distance": 3,
    "age": 1
},
{
    "_id": 4,
    "owner": "B",
    "distance": 4,
    "age": 2
}

```

The requirements are:

1. Return MAX 1 document pr. `owner`.
2. Returned documents MUST be the one with the shortest distance for `owner`.
3. Sorting keys are `owner`, `distance`, `age` or any combination of those.

This means we only one return document 1+3 sorted according to whatever key we choose.

The way we solve it today is to split the request in 2:

- First request: Prefetch the IDs of the documents with the shortest distance.
- Second request: Filter by IDs and sort according to chosen key.

We would like to avoid the latency of a second roundtrip and moving a lot of data (IDs) back and forth. And I'm wrecking my brain, looking into nested and parent/child and other ways to de-normalize the data and can't come up with a better solution.

NOTE: `distance` is dynamic i real world - it is the distance between a user and document location.

---

<div class="post-metadata">

**Author:** ![mekanix](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@mekanix](https://discuss.elastic.co/u/mekanix)\
**Post date:** [December 16, 2020, 10:36am UTC](https://discuss.elastic.co/t/filter-by-id-without-providing-id-turn-2-queries-into-one/258837/2 "2020-12-16T10:36:08Z")

</div>

One thing that may work would be to make a `match_all` request sorted by `distance` with `size: 0` and `collapse` on `owner`.

Then in `aggs` do the sorting.

Problem with this is we lose built in pagination and there is the overhead of aggregation that would have to be repeated for each page.

---

<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:** [January 13, 2021, 10:36am UTC](https://discuss.elastic.co/t/filter-by-id-without-providing-id-turn-2-queries-into-one/258837/3 "2021-01-13T10:36:10Z")

</div>

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