Kibana bar chart aggr query doc_count

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

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.

{
  "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.

Hi @math1 Welcome to the community!

What version of the elastic stack are you on?

Can you provide a few more sample data?

elastic 8.6.2.

This is my sample data.

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"
      }
    }
  ]
}

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


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

Pie Chart multi-level Breakdown

Thanks!

1 Like

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