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

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"

{
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

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

{
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.

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!

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. :slightly_smiling_face:

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