0
votes

Is there a way to return only one product if it has different color. e.g. suppose I have a product with following properties:

brand,color,title
nike, red, air max
nike, blue, air max

now I want to create elastic search query to return only one product while aggregation but count as two belonging to brand nike.

{
    "query" : {
        "match_all" : {}

    },

    "aggs" : {
        "brand" : {
            "terms" : {
                "field" : "brand"
            },
            "aggs" : {
                "size" : {
                    "terms" : {
                        "field" : "title"
                    }
                }
            }
        }

    }

}

I am not able to get desired results. I want like select name,color,title, count(*) title from product group by name,title

1

1 Answers

2
votes

I think you want to get document, aggregated by name,title

This can be done using topHits aggregation.

{
   "size": 0,
   "query": {
      "match_all": {}
   },
   "aggs": {
      "brand": {
         "terms": {
            "field": "name"
         },
         "aggs": {
            "size": {
               "terms": {
                  "field": "title"
               }
            },
            "aggs":{
                "top_hits" :{
                    "_source" :[ "name","color","band"],
                    "size":1
                }
            }
         }
      }
   }
}

For count, there is always doc_count in returned buckets.

Hope this helps!! If I am missing something, do mention.

Thanks