# How to check/query only the first element of a nested field?

**URL:** <https://discuss.elastic.co/t/how-to-check-query-only-the-first-element-of-a-nested-field/231311>\
**Category:** Elasticsearch\
**Created:** [May 6, 2020, 9:51am UTC](https://discuss.elastic.co/t/how-to-check-query-only-the-first-element-of-a-nested-field/231311 "2020-05-06T09:51:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bart\_de\_Man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bart_de_man/32/48319_2.png) [@Bart\_de\_Man](https://discuss.elastic.co/u/Bart_de_Man)\
**Post date:** [May 6, 2020, 9:51am UTC](https://discuss.elastic.co/t/how-to-check-query-only-the-first-element-of-a-nested-field/231311/1 "2020-05-06T09:51:25Z")

</div>

**Elasticsearch version** ( `bin/elasticsearch --version` ):  
5.6.11  
**Plugins installed** :

**JVM version** ( `java -version` ):  
openjdk version "1.8.0\_222"  
OpenJDK Runtime Environment (build 1.8.0\_222-8u222-b10-1ubuntu1~16.04.1-b10)  
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

**OS version** ( `uname -a` if on a Unix-like system):  
Linux myserver 4.4.0-131-generic #157-Ubuntu SMP Thu Jul 12 15:51:36 UTC 2018 x86\_64 x86\_64 x86\_64 GNU/Linux

**Description of the problem including expected versus actual behavior** :  
I have a nested field in my index which holds the most recent element at the top (index 0), how could i enforce a query to ONLY check for this said first element and not query the rest of the elements in said nested field?

E.g. :

document with field "nested"

```auto
{
propx: "hi",
mynested: { 
[
  firs_el: "valueY",
  second_el: 123,
],
[
  firs_el: "dontmatter",
  second_el: 666,
],
}

```

in the example above I would like to query and check if the first element matches my criteria, and if it does i want to return the whole document

Thanks in advance

note: this concerns just the matching, not the returned results

---

<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:** [May 6, 2020, 10:08am UTC](https://discuss.elastic.co/t/how-to-check-query-only-the-first-element-of-a-nested-field/231311/3 "2020-05-06T10:08:15Z")

</div>

I believe you would need to add an order field like:

```auto
{
propx: "hi",
mynested: { 
[
  sort: 1,
  firs_el: "valueY",
  second_el: 123,
],
[
  sort: 2,
  firs_el: "dontmatter",
  second_el: 666,
],

```

And add `sort: 1` as one of the criterias.

---

<div class="post-metadata">

**Author:** ![Bart\_de\_Man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bart_de_man/32/48319_2.png) [@Bart\_de\_Man](https://discuss.elastic.co/u/Bart_de_Man)\
**Post date:** [May 6, 2020, 12:35pm UTC](https://discuss.elastic.co/t/how-to-check-query-only-the-first-element-of-a-nested-field/231311/4 "2020-05-06T12:35:34Z")

</div>

Thanks that did the trick!

Would there be any other way to achieve the result as described without the new field? (just curious)

I'm asking because we have quite a few documents in this index, and as far as i understand when you want to add a new field, I pretty much have to clear the whole index, and start indexing all my documents (millions)

Still looking for a better approach to the above if there is any at all.

Thanks again!

---

<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:** [May 6, 2020, 2:58pm UTC](https://discuss.elastic.co/t/how-to-check-query-only-the-first-element-of-a-nested-field/231311/5 "2020-05-06T14:58:17Z")

</div>

I don't see anything that could run fast at least. May be a script could do the trick but I'm kind of against computing things at search time. 🙂

---

<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:** [June 3, 2020, 3:01pm UTC](https://discuss.elastic.co/t/how-to-check-query-only-the-first-element-of-a-nested-field/231311/6 "2020-06-03T15:01:49Z")

</div>

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