Aggregation on fields within nested objects in elasticsearch

hi ,

I have two entries in nested object mapping as below :

"itemslist":
{
      "type": "nested",
      "properties": {
      "type": "object",
      "properties": {
        "itemname": {
          "type": "text"
        },
        "quantity": {
          "type": "integer"
        }
}
}

I want to aggregate sum on the quantity based on itemname , issue i'm not sure how to aggregate on array of objects .

Sample data :

 Itemlist : 
     {
{
      "itemname": "1234",
      "quantityint": 2,
    }  ,
    {
      "itemname": "1235",
      "quantityint": 2,
    } 
}

Requirement :
To be able to query "itemname": "1234" , with sum aggregation on quantity with result =2 . Presently i'm getting result as 4 in kibana visualization .

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

The main question I have is why are you indexing an array of items if you actually want to compute items. Just indexing individual items is looking a better design to me but may be I don't have the full picture here.

So I'd index things like:

PUT items/_doc/1234
{
  "itemname": "1234",
  "quantityint": 2
} 
PUT items/_doc/1234
{
  "itemname": "1235",
  "quantityint": 2
} 

Then creating a sum is quite easy I believe.

Hi ,

So basically its a list of object in java that i'm trying to create equivalent mapping of . In a single request there can be multiple items , therefore i've mapped the object as nested .

Is there a way to fetch only the corresponding quantity of a given item in an array of items for a given object?

Thanks,
Toshi

I was not speaking about the implementation details but about the need.

If you are trying to compute on "item", then index "item" and not a group of items.

Is there a way to fetch only the corresponding quantity of a given item in an array of items for a given object?

I don't know but may be provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem.

items is not the index in this case , it's a field which contains nested objects . My mappings looks something like this :

{
  "properties": {
    "timestamp": {
      "type": "date",
      "format": "date_optional_time"
    },
    "updatedon": {
      "type": "date",
      "format": "date_optional_time"
    },
    "personinfolist": {
            "type": "nested",
             properties:{
                   "field1": {"type":"text"}
                                }
    },
     "itemslist":
              {
               "type": "nested",
              "properties": {
                     "itemname": {
                     "type": "text"
                                      },
                     "quantity": {
                      "type": "integer"
                                        }
                 }
}
}

Found the solution :sunglasses: , below query can be used for aggregation on the inner hits of the nested object :

 {
   "aggs": {
      "nested_nestedobjects": {
         "nested": {
            "path": "items"
         },
         "aggs": {
            "filtered_nestedobjects": {
               "filter": {
                  "term": {
                     "items.name": "abc"
                  }
               },
               "aggs": {
                  "total_quantity": {
                     "sum": {
                        "field": "items.quantity"
                     }
                  }
               }
            }
         }
      }
   }
}
1 Like

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