Home > front end >  How to create sub-bucket aggregation on the basis of hierarchy field value (part of the field)
How to create sub-bucket aggregation on the basis of hierarchy field value (part of the field)

Time:03-10

Assuming that we have a documents that has a hierarchy field like this:

POST subbuckets/_doc
{
  "hierarchy": "this/is/some/hierarchy"
}

POST subbuckets/_doc
{
  "hierarchy": "this/is/some/hierarchy2"
}

POST subbuckets/_doc
{
  "hierarchy": "this/is/another/hierarchy1"
}

I would like to count number of documents that belongs to each hierarchy level I.e.

  • "this" hierarchy level has 3 documents
  • "this/is" hierarchy level has 3 documents
  • "this/is/some" hierarchy level has 2 documents
  • "this/is/another" hierarchy level has 1 document
  • "this/is/another/hierarchy1" hierarchy level has 1 document
  • "this/is/some/hierarchy" hierarchy level has 1 document
  • "this/is/some/hierarchy2" hierarchy level has 1 document

CodePudding user response:

We can not applied analyzer on keyword so to resolved this issue we have define field as text type and enable aggregation on text field and set "fielddata": true. Please check below configuration.

Index Mapping:

PUT index5
{
  "settings": {
    "analysis": {
      "analyzer": {
        "path-analyzer": {
          "tokenizer": "path-tokenizer"
        }
      },
      "tokenizer": {
        "path-tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "/"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "hierarchy": {
        "type": "text",
        "analyzer": "path-analyzer",
        "search_analyzer": "keyword",
        "fielddata": true
      }
    }
  }
}

Index Documents:

POST index5/_doc
{
  "hierarchy": "this/is/some/hierarchy"
}

POST index5/_doc
{
  "hierarchy": "this/is/some/hierarchy2"
}

POST index5/_doc
{
  "hierarchy": "this/is/another/hierarchy1"
}

Query:

POST index5/_search
{
  "aggs": {
    "path": {
      "terms": {
        "field": "hierarchy"
      }
    }
  },
  "size": 0
}

Response:

{
 "aggregations" : {
    "path" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "this",
          "doc_count" : 3
        },
        {
          "key" : "this/is",
          "doc_count" : 3
        },
        {
          "key" : "this/is/some",
          "doc_count" : 2
        },
        {
          "key" : "this/is/another",
          "doc_count" : 1
        },
        {
          "key" : "this/is/another/hierarchy1",
          "doc_count" : 1
        },
        {
          "key" : "this/is/some/hierarchy",
          "doc_count" : 1
        },
        {
          "key" : "this/is/some/hierarchy2",
          "doc_count" : 1
        }
      ]
    }
  }
}
  • Related