# Nested aggregation with min\_doc\_count=0

**URL:** <https://discuss.elastic.co/t/nested-aggregation-with-min-doc-count-0/146038>\
**Category:** Elasticsearch\
**Created:** [August 25, 2018, 5:54pm UTC](https://discuss.elastic.co/t/nested-aggregation-with-min-doc-count-0/146038 "2018-08-25T17:54:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdesousa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jdesousa/32/34813_2.png) [@jdesousa](https://discuss.elastic.co/u/jdesousa)\
**Post date:** [August 25, 2018, 5:54pm UTC](https://discuss.elastic.co/t/nested-aggregation-with-min-doc-count-0/146038/1 "2018-08-25T17:54:41Z")

</div>

Hi all,

I'am trying to use nested aggregation with min\_doc\_count=0

My schema:

```auto
PUT /example
{
  "mappings": {
    "doc": {
      "properties": {
        "id": {
          "type": "text"
        },
        "properties": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "keyword"
            },
            "value": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
```

My documents are like this:

`{
  "id": "1",
  "properties": [
    {
      "key": "color",
      "value": [
        "red"
      ]
    },
    {
      "key": "size",
      "value": [
        "big"
      ]
    },
    {
      "key": "shape",
      "value": [
        "circle"
      ]
    }
  ]
}`  
and this is my values:

> id=1 color=red size=big shape=circle  
> id=2 color=blue size=big shape=square  
> id=3 color=red size=small shape=circle  
> id=4 color=yellow size=medium shape=rectangle

This is my search request example, I want only document with red color, and with aggregations I want to know results properties and associated count :

```auto
{
  "_source": [
    "id"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "properties.key": "color"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "red"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "filters": {
      "nested": {
        "path": "properties"
      },
      "aggregations": {
        "key": {
          "terms": {
            "field": "properties.key"
          },
          "aggregations": {
            "value": {
              "terms": {
                "field": "properties.value"
              }
            }
          }
        }
      }
    }
  }
}
```

The result is OK, I retrieve documents with id=1 and id=3, and with aggregations I know the 2 documents are circle, and one is small and the other is big:

```auto
 "aggregations": {
    "filters": {
      "doc_count": 6,
      "key": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "color",
            "doc_count": 2,
            "value": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "red",
                  "doc_count": 2
                }
              ]
            }
          },
          {
            "key": "shape",
            "doc_count": 2,
            "value": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "circle",
                  "doc_count": 2
                }
              ]
            }
          },
          {
            "key": "size",
            "doc_count": 2,
            "value": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "big",
                  "doc_count": 1
                },
                {
                  "key": "small",
                  "doc_count": 1
                }
              ]
            }
          }
        ]
      }
    }
  }
```

Now the problem is if I want to know the others possiblity of properties and to display the properties values with no document associated.

I changed my search request by adding `"min_doc_count: 0` on the `"field": "properties.values"` aggregation.

Result, all properties values are mixed and not sorting by key:

```auto
"aggregations": {
    "filters": {
      "doc_count": 4,
      "filters": {
        "doc_count": 2,
        "properties": {
          "doc_count": 6,
          "key": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "color",
                "doc_count": 2,
                "value": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": "red",
                      "doc_count": 2
                    },
                    {
                      "key": "big",
                      "doc_count": 0
                    },
                    {
                      "key": "circle",
                      "doc_count": 0
                    },
                    {
                      "key": "small",
                      "doc_count": 0
                    }
                  ]
                }
              },
              {
                "key": "shape",
                "doc_count": 2,
                "value": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": "circle",
                      "doc_count": 2
                    },
                    {
                      "key": "big",
                      "doc_count": 0
                    },
                    {
                      "key": "red",
                      "doc_count": 0
                    },
                    {
                      "key": "small",
                      "doc_count": 0
                    }
                  ]
                }
              },
              {
...
```

It is possible to obtain only values by key in the result aggregation ?

Thank's for your help.

Julien

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 28, 2018, 4:42pm UTC](https://discuss.elastic.co/t/nested-aggregation-with-min-doc-count-0/146038/2 "2018-08-28T16:42:31Z")

</div>

I'm not 100% sure I understand correctly what it is that you're trying to do. Do I understand it correctly as: you're looking for the key-value pairs that occur in all the documents that do not match your query?

If so, take a look at [the global aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html). This aggregation resets the aggregation scope to be all documents, rather than just the documents that match your query. If you would nest your existing aggregations inside of a global agg, you would get all existing key-value pairs, regardless of whether they are part of a document that matches your query:

```auto
GET example/_search
{
  "_source": [
    "id"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "properties.key": "color"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "red"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "get_all": {
      "global": {},
      "aggregations": {
        "filters": {
          "nested": {
            "path": "properties"
          },
          "aggregations": {
            "key": {
              "terms": {
                "field": "properties.key"
              },
              "aggregations": {
                "value": {
                  "terms": {
                    "field": "properties.value"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Now if you would want to exclude the key-value pairs that **do** match your query, then inside of the global aggregation, you can nest a `filter` aggregation that is the opposite of your existing query. The request would look like this (note that inside the `filter` agg I use `must_not` instead of `must` to aggregate on the documents that do not match the original query):

```auto
GET example/_search
{
  "_source": [
    "id"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "properties.key": "color"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "red"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "get_all": {
      "global": {},
      "aggs": {
        "remove_matching_doc": {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "nested": {
                    "path": "properties",
                    "query": {
                      "bool": {
                        "must": [
                          {
                            "match": {
                              "properties.key": "color"
                            }
                          },
                          {
                            "match": {
                              "properties.value": "red"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          },
          "aggregations": {
            "filters": {
              "nested": {
                "path": "properties"
              },
              "aggregations": {
                "key": {
                  "terms": {
                    "field": "properties.key"
                  },
                  "aggregations": {
                    "value": {
                      "terms": {
                        "field": "properties.value"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Hope this helps!

---

<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:** [September 25, 2018, 4:50pm UTC](https://discuss.elastic.co/t/nested-aggregation-with-min-doc-count-0/146038/3 "2018-09-25T16:50:29Z")

</div>

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