Elasticsearch nested array Aggregation


(Lavan Giri) #1

Hello, I'm trying to aggregate a nested array, the count is including the duplicate value also..

Here is my Mapping

    {"mappings" : {
      "mention" : {
        "properties" : {
          "Topics" : {
            "type" : "nested",
            "properties" : {
              "CategoryLev1" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          }
        }
      }
    }
}

Sample Data

{
	"Topics" : [ 
        {
            "CategoryLev1" : "Price"          
        }, 
        {
            "CategoryLev1" : "Price"
        }
        
    ]
}

my Search Query

{  
   "size":0,
   "from":0,
   
    "aggs":{  
      "Topics":{  
         "nested":{  
            "path":"Topics"
         },
         "aggregations":{  
            "group_by_topic":{  
               "terms":{  
                  "field":"Topics.CategoryLev1.keyword",
                  "size":50,
                  "min_doc_count":1,
                  "shard_min_doc_count":0,
                  "show_term_doc_count_error":false,
                  "order":[  
                     {  
                        "_count":"desc"
                     },
                     {  
                        "_term":"asc"
                     }
                  ]
               }
            }
         }
      }
   }
}

Actual Output

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "Topics": {
            "doc_count": 2,
            "group_by_topic": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "Price",
                        "doc_count": 2
                    }
                ]
            }
        }
    }
}

Expected Output

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "Topics": {
            "doc_count": 2,
            "group_by_topic": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "Price",
                        "doc_count": **1**
                    }
                ]
            }
        }
    }
}

There is only 1 document, but duplicate inside the nested array.. So the count should be 1


(system) #2

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