Home > Enterprise >  Aggregation on an array of objects
Aggregation on an array of objects

Time:03-10

I have the following data in my elastic.

{
...someData,
languages: [
{
language:{_id: 1, name:"English"}
},
{
language:{_id: 2, name:"Arabic"}
}
]
}

But when I aggregate the data using this query

    aggs: {

languages: {
            terms: {
                field: "languages.language._id.keyword",
                size: 50
            },
            aggs: {
                value: {
                    terms: {
                        field: "languages.language.name.keyword"
                    }
                }
            }
        }
}

I will get the English id with 2 buckets for Arabic and English and same for Arabic id, because technically its included there.

Is there a way to return only the count of the object I need?

Thanks

CodePudding user response:

You need to define languages field as nested for applying aggregation on individual element of array.

Configured Nested field:

PUT index0
{
  "mappings": {
    "properties": {
      "languages":{
        "type": "nested"
      }
    }
  }
}

Sample document index:

POST index0/_doc
{
  "languages": [
    {
      "language": {
        "_id": 1,
        "name": "English"
      }
    },
    {
      "language": {
        "_id": 2,
        "name": "Arabic"
      }
    }
  ]
}

Sample Aggregation Query:

{
  "size": 0,
  "aggs": {
    "languages": {
      "nested": {
        "path": "languages"
      },
      "aggs": {
        "id": {
          "terms": {
            "field": "languages.language._id",
            "size": 10
          },
          "aggs": {
            "name": {
              "terms": {
                "field": "languages.language.name.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

Result:

"aggregations" : {
    "languages" : {
      "doc_count" : 2,
      "id" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : 1,
            "doc_count" : 1,
            "name" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "English",
                  "doc_count" : 1
                }
              ]
            }
          },
          {
            "key" : 2,
            "doc_count" : 1,
            "name" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "Arabic",
                  "doc_count" : 1
                }
              ]
            }
          }
        ]
      }
    }
  }

CodePudding user response:

Did you try this :


    aggs: {

languages: {
            terms: {
                field: "languages.language._id.keyword",
                size: 50
            },
      }
 
}

You do not need the other aggregation. You can access using the doc_count key

  • Related