# Kibana bar chart aggr query doc\_count

**URL:** <https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149>\
**Category:** Kibana\
**Created:** [April 3, 2023, 4:39am UTC](https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149 "2023-04-03T04:39:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![math1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@math1](https://discuss.elastic.co/u/math1)\
**Post date:** [April 3, 2023, 4:39am UTC](https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149/1 "2023-04-03T04:39:44Z")

</div>

I want to draw the query statement below as a bar chart in Kibana.

```auto
GET food_info/_search
{
  "size": 0,
  "aggs": {
    "country": {
      "terms": {
        "field": "food.countryCode"
      },
      "aggs": {
        "type": {
          "terms": {
            "field": "food.type.code"
          }
        }
      }
    }
  }
}

```

The following code is the result of the code above.

```auto
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "country": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "CN",
          "doc_count": 2,
          "food": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "burger",
                "doc_count": 2
              }
            ]
          }
        }
      ]
    }
  }
}

```

The x-axis is the country code, and the y-axis is the number of documents per country code(doc\_count). However, the y-axis is not the number of documents, but 1, which is the number of countries.

 ![캡처](https://us1.discourse-cdn.com/elastic/original/3X/5/0/5083a7203f61fd7211c05ae3f4e90a1558afa9d7.png)

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 3, 2023, 4:42am UTC](https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149/2 "2023-04-03T04:42:18Z")

</div>

Hi @math1 Welcome to the community!

What version of the elastic stack are you on?

Can you provide a few more sample data?

---

<div class="post-metadata">

**Author:** ![math1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@math1](https://discuss.elastic.co/u/math1)\
**Post date:** [April 3, 2023, 4:45am UTC](https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149/3 "2023-04-03T04:45:09Z")

</div>

elastic 8.6.2.

This is my sample data.

```auto
PUT food_info/_doc/1
{
  "food": [
    {
      "countryCode": "CN",
      "type": {
        "code": "burger",
        "name": "burger1"
      }
    }
  ]
}

PUT food_info/_doc/2
{
  "food": [
    {
      "countryCode": "CN",
      "type": {
        "code": "burger",
        "name": "burger2"
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 3, 2023, 5:10am UTC](https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149/4 "2023-04-03T05:10:44Z")

</div>

Hi @math1 Take a look at this perhaps it will help

I created a mapping (not sure if you intentionally have an array  
Then POSTed some Docs  
Create A Data View  
Then Go To Lens

```auto

DELETE food_info

PUT food_info
{
  "mappings": {
    "properties": {
      "food": {
        "properties": {
          "countryCode": {
            "type": "keyword"
          },
          "type": {
            "properties": {
              "code": {
                "type": "keyword"
              },
              "name": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}

POST food_info/_doc/
{
  "food": [
    {
      "countryCode": "CN",
      "type": {
        "code": "burger",
        "name": "burger1"
      }
    }
  ]
}

POST food_info/_doc/
{
  "food": [
    {
      "countryCode": "CN",
      "type": {
        "code": "burger",
        "name": "burger2"
      }
    }
  ]
}

POST food_info/_doc/
{
  "food": [
    {
      "countryCode": "CN",
      "type": {
        "code": "chicken",
        "name": "BBQ chicken"
      }
    }
  ]
}

POST food_info/_doc/
{
  "food": [
    {
      "countryCode": "US",
      "type": {
        "code": "pizza",
        "name": "pepperoni"
      }
    }
  ]
}

```

Create Data View food\_info  
Kibana - Stack Management - Create Data View

The Create a Lens Visualization

Kibana - Visualization - Lens

 ![Screenshot 2023-04-02 at 10.06.29 PM](https://us1.discourse-cdn.com/elastic/original/3X/4/4/445c78388fc9e53b2c4be99429a96b9d291314f6.jpeg)

Pie Chart multi-level Breakdown

 ![Screenshot 2023-04-02 at 10.09.04 PM](https://us1.discourse-cdn.com/elastic/original/3X/e/9/e98f3a30d96c3c7fce4cdffdb7a9081924b38c3c.jpeg)

---

<div class="post-metadata">

**Author:** ![math1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@math1](https://discuss.elastic.co/u/math1)\
**Post date:** [April 3, 2023, 5:44am UTC](https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149/5 "2023-04-03T05:44:38Z")

</div>

Thanks!

---

<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:** [May 1, 2023, 5:44am UTC](https://discuss.elastic.co/t/kibana-bar-chart-aggr-query-doc-count/329149/6 "2023-05-01T05:44:45Z")

</div>

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