Search code examples
elasticsearch

Empty Buckets from ElasticSearch


I am getting empty buckets from a nested aggregation. I expect to get a result of all the keys and nested buckets with their values, but no matter how I slice it I always end up with an empty bucket result set. using elastic v6.2.4 her are the steps to reproduce:

PUT aggregation_test
{"settings": {"number_of_shards": 1}}

PUT aggregation_test/_mapping/doc
{
  "properties": {
    "Description": {
      "type": "text"
    },
    "string_attributes": {
      "type": "nested",
      "properties": {
        "key": {"type": "text"},
        "value": {"type": "text"}
      }
    }
  }
}

Then fill with data

POST aggregation_test/doc/1
{
  "Description": "one",
  "string_attributes": [
    { "key": "Variant", "value": "Red"},
    { "key": "Variant", "value": "Yellow"},
    { "key": "Variant", "value": "Blue"},
    { "key": "EcoFriendly", "value": "Yes"}
  ]
}

POST aggregation_test/doc/2
{
  "Description": "two",
  "string_attributes": [
    { "key": "Variant", "value": "Red"},
    { "key": "Variant", "value": "Green"},
    { "key": "Variant", "value": "Blue"},
    { "key": "EcoFriendly", "value": "No"}
  ]
}

POST aggregation_test/doc/3
{
  "Description": "three",
  "string_attributes": [
    {"key": "Variant", "value": "Red"},
    {"key": "Variant", "value": "Green"},
    {"key": "Variant", "value": "Blue"}
  ]
}

Here is my aggregation

GET aggregation_test/_search
{
  "size": 0,
  "aggs": {
    "nested_level": {
      "nested": {
        "path": "string_attributes"
      },
      "aggs": {
        "keyword_field": {
          "terms": {
            "field": "string_attributes.key.keyword"
          }, "aggs": {
            "value_field": {
              "terms": {"field": "string_attributes.value.keyword"}
            }
          }
        }
      }
    }
  }
}

and the result

{
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "nested_level": {
      "doc_count": 11,
      "keyword_field": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": []
      }
    }
  }
}

Any idea why I am not seeing any bucket information?


Solution

  • Looking at your query, it targets fields string_attributes.key.keyword and string_attributes.value.keyword but based on your schema, it doesn't have keyword field for key and value.

    You must change your schema to add the keyword field there

    PUT aggregation_test 
    {
      "mappings": {
        "doc": {
          "properties": {
            "Description": {
              "type": "text"
            },
            "string_attributes": {
              "type": "nested",
              "properties": {
                "key": {
                  "type": "text",
                  "fields": {
                    "keyword": { // add this
                      "type": "keyword" // type should be keyword for proper aggregation
                    }
                  }
                },
                "value": {
                  "type": "text",
                  "fields": {
                    "keyword": { // add this
                      "type": "keyword" 
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Hope it works