Nested filter on sub document with the max value of a field for each document

Hi,

First post so have mercy :sweat_smile:

I have a mapping like:

"docType": {
    "properties": {
      "nestedField": {
        "type": "nested",
        "include_in_parent": true,
        "properties": {
          "field1": {
            "type": "boolean"
          },
          "field2": {
            "type": "long"
          },
      }
}

Where each document has an array of nesteField objects. Each object, increments field2 with 1.

So if i have 2 documents:

{
    "nestedField": [
        {
            "field1": true,
            "field2": 1,
        },
        {
            "field1": false,
            "field2": 2,
        }
    ]
},
{
    "nestedField": [
        {
            "field1": true,
            "field2": 1,
        },
        {
            "field1": false,
            "field2": 2,
        },
        {
            "field1": true,
            "field2": 3,
        }
    ]
}

I want to filter documents where field1 is true/false in the nested document with the max field2, but max for the document, not overall.

So if i use a nested filter with path 'nestedField' and field1 true, it will select both documents because it will look in all nestedField sub documents.

I want to select documents where the nested filterfor field1 only checks the object which has max field2 for that document.
In the example above, it would only select the second document because it's field2 with value 3 (max for this document) has field1 true. While the first document has it's max field2 nested object field1 false.

Hope i explained the case well enough.

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