假設我們有一個檔案,它具有這樣的層次結構欄位:
POST subbuckets/_doc
{
"hierarchy": "this/is/some/hierarchy"
}
POST subbuckets/_doc
{
"hierarchy": "this/is/some/hierarchy2"
}
POST subbuckets/_doc
{
"hierarchy": "this/is/another/hierarchy1"
}
我想計算屬于每個層次結構級別的檔案數量,即
"this"層次結構級別有 3 個檔案"this/is"層次結構級別有 3 個檔案"this/is/some"層次結構級別有 2 個檔案"this/is/another"層次結構級別有 1 個檔案"this/is/another/hierarchy1"層次結構級別有 1 個檔案"this/is/some/hierarchy"層次結構級別有 1 個檔案"this/is/some/hierarchy2"層次結構級別有 1 個檔案
uj5u.com熱心網友回復:
我們不能應用分析器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://www.uj5u.com/shujuku/444299.html
標籤:弹性搜索
