# Best query to only return documents matching a nested unique id 'id' and not the ES \_id field?

**URL:** <https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986>\
**Category:** Elasticsearch\
**Created:** [November 10, 2020, 10:49pm UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986 "2020-11-10T22:49:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_Gibbs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_gibbs/32/45665_2.png) [@M\_Gibbs](https://discuss.elastic.co/u/M_Gibbs)\
**Post date:** [November 10, 2020, 10:49pm UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/1 "2020-11-10T22:49:40Z")

</div>

Hey all,

I've been pulling my hair out a bit with this.

We are running a basic query which matches a set of unique id's - this works perfectly when querying the ES \_id field.

But when we try to match a nested id field, using the exact same process (\_source.id) - it just returns an empty match.

Any guidance appreciated!

---

<div class="post-metadata">

**Author:** ![M\_Gibbs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_gibbs/32/45665_2.png) [@M\_Gibbs](https://discuss.elastic.co/u/M_Gibbs)\
**Post date:** [November 12, 2020, 7:45am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/2 "2020-11-12T07:45:07Z")

</div>

Gentle bump!

I've tried match and also terms, e.g.:

```
{
  "query": {
    "terms": {
      "id": ["...", "..."]
    }
  }
}

```

I just can't get it to return any results! Pulling my hair out a little. When we used the standard ES \_id field, it works fine.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 12, 2020, 8:08am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/3 "2020-11-12T08:08:21Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Here I suspect a problem with mapping, analyzers....

---

<div class="post-metadata">

**Author:** ![M\_Gibbs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_gibbs/32/45665_2.png) [@M\_Gibbs](https://discuss.elastic.co/u/M_Gibbs)\
**Post date:** [November 12, 2020, 8:56am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/4 "2020-11-12T08:56:09Z")

</div>

sure!

I have tried this, which I expect to work as an alternative to match:

```
GET index/_search
{
  "query": {
"terms": {
  "_source.id": ["...", "..."]
}
  }
}

```

And also:

```
GET index/_search
{
  "query": {
"terms": {
  "id": ["...", "..."]
}
  }
}

```

I have tried around 10/11 variations to try to get this to work!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 12, 2020, 9:21am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/5 "2020-11-12T09:21:33Z")

</div>

I understood what you tried. What I'm asking for is a script I can run myself which exactly reproduces your problem.

Here you are not even sharing the id you're searching for so we can only make wild guesses.

One supposition for now. Try to lowercase the id. But again, no reproduction = hard to help.

---

<div class="post-metadata">

**Author:** ![M\_Gibbs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_gibbs/32/45665_2.png) [@M\_Gibbs](https://discuss.elastic.co/u/M_Gibbs)\
**Post date:** [November 12, 2020, 9:28am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/6 "2020-11-12T09:28:28Z")

</div>

Apologies @dadoonet - I'm only newly taking over from an external dev and I'm not a developer by trade, just relatively good at picking technical things up.

I'm not sure I follow how else to provide a script for you (aside for including the ids). For you to reproduce locally, you would need our actual ES endpoint + access to the endpoint? Is that what you are asking?

I also just tried lowercasing the id, but that returned the same response, unfortunately.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 12, 2020, 2:28pm UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/7 "2020-11-12T14:28:53Z")

</div>

No. I don't want/need to access your machine fortunately.

I just need something I can copy and paste in Kibana dev console, then press run.

I can then find what your problem is and how to fix it.  
If you read the link I shared, there's a practical example of a reproduction script.

At the very least share an id you are searching for.

---

<div class="post-metadata">

**Author:** ![M\_Gibbs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_gibbs/32/45665_2.png) [@M\_Gibbs](https://discuss.elastic.co/u/M_Gibbs)\
**Post date:** [November 16, 2020, 8:31am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/8 "2020-11-16T08:31:11Z")

</div>

Found the solution.

For anyone with a similar issue reading this, I just replaced "\_source.id" with "id.keyword" and it worked.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 16, 2020, 9:26am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/9 "2020-11-16T09:26:30Z")

</div>

> [@M\_Gibbs](#):
>
> For anyone with a similar issue reading this, I just replaced "\_source.id" with "id.keyword" and it worked.

Which is basically what I suspected when I wrote:

> [@dadoonet](#):
>
> Here I suspect a problem with mapping, analyzers....

---

<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:** [December 14, 2020, 9:26am UTC](https://discuss.elastic.co/t/best-query-to-only-return-documents-matching-a-nested-unique-id-id-and-not-the-es-id-field/254986/10 "2020-12-14T09:26:55Z")

</div>

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