# Issue in solving nested aggregation

**URL:** <https://discuss.elastic.co/t/issue-in-solving-nested-aggregation/188713>\
**Category:** Elasticsearch\
**Created:** [July 3, 2019, 12:54pm UTC](https://discuss.elastic.co/t/issue-in-solving-nested-aggregation/188713 "2019-07-03T12:54:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticpatrick](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticpatrick/32/49939_2.png) [@elasticpatrick](https://discuss.elastic.co/u/elasticpatrick)\
**Post date:** [July 3, 2019, 12:54pm UTC](https://discuss.elastic.co/t/issue-in-solving-nested-aggregation/188713/1 "2019-07-03T12:54:17Z")

</div>

Hello, I have a "PRODUCT\_SUPPLIER" index in which I have a documents of products with its suppliers, having fields name "product\_name" and "product\_supplier".

```auto
"mappings" : {
      "properties" : {
        "product_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        },
        "product_supplier" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        }
      }
    }

```

A product can have more than one supplier. I have a supplier name say "supplier1". I get the buckets of products this supplier supply. Now from that buckets i want to do query to get others supplier names of specific products in buckets except that supplier "supplier1". My query is this

```auto
GET /products_supplier/_search
{
  "query": {
    "term": {
      "product_supplier": {
        "value": "supplier1"
      }
    }
  }, "aggs": {
    "products_list": {
      "terms": {
        "field": "product_name.keyword",
        "size": 10
      }, "aggs": {
        "others_supplier": {
          "terms": {
            "field": "product_supplier.keyword",
            "size": 10
          }
        }
      }
    }
  }
}

```

I get following results. I don't know how to eliminate the "Supplier1" in the "others\_supplier" aggragation.

```auto
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "products_supplier",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931472,
        "_source" : {
          "product_name" : "maggie",
          "product_supplier" : "supplier1"
        }
      },
      {
        "_index" : "products_supplier",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.6931472,
        "_source" : {
          "product_name" : "potatoes",
          "product_supplier" : "supplier1"
        }
      },
      {
        "_index" : "products_supplier",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.6931472,
        "_source" : {
          "product_name" : "chips",
          "product_supplier" : "supplier1"
        }
      }
    ]
  },
  "aggregations" : {
    "products_list" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "chips",
          "doc_count" : 1,
          "others_supplier" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "supplier1",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "maggie",
          "doc_count" : 1,
          "others_supplier" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "supplier1",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "potatoes",
          "doc_count" : 1,
          "others_supplier" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "supplier1",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 31, 2019, 12:54pm UTC](https://discuss.elastic.co/t/issue-in-solving-nested-aggregation/188713/2 "2019-07-31T12:54:25Z")

</div>

Closed on July 31, 2019, 12:54pm UTC

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