假设我们有一个具有如下层次结构字段的文档:
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.e。
"this"层次结构层有3个documents"this/is"层次层、3个documents"this/is/some"层次层、2个documents"this/is/another"层次层、1个document"this/is/another/hierarchy1"层次层、1个document"this/is/some/hierarchy"层次层、1个document"this/is/some/hierarchy2"层次层和1个文档级别。
发布于 2022-03-08 14:01:52
我们不能在keyword上应用分析器,因此要解决这个问题,必须将字段定义为text类型,并在text字段上启用聚合并设置"fielddata": true。请检查下面的配置。
索引映射:
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
}
}
}
}索引文档
POST index5/_doc
{
"hierarchy": "this/is/some/hierarchy"
}
POST index5/_doc
{
"hierarchy": "this/is/some/hierarchy2"
}
POST index5/_doc
{
"hierarchy": "this/is/another/hierarchy1"
}查询:
POST index5/_search
{
"aggs": {
"path": {
"terms": {
"field": "hierarchy"
}
}
},
"size": 0
}响应:
{
"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
}
]
}
}
}https://stackoverflow.com/questions/71396029
复制相似问题