需要幫忙。
我在elasticsearch中有資料,欄位“興趣”是這樣的:
{ "興趣": "A,C,D,E" }, { "興趣": "B,C,D" }, { "興趣": "A,B,C,D,E" }, { "興趣": "D,E" }
我想得到這樣的資料:{ "key": "A", "doc_count": 2 }, { "key": "B", "doc_count": 2 }, { "key": "C", " doc_count": 3 }, { "key": "D", "doc_count": 4 }, { "key": "E", "doc_count": 3 }
我應該采取什么步驟。謝謝你。
uj5u.com熱心網友回復:
一個簡單的terms聚合應該可以完成這項作業:
POST test/_search
{
"aggs": {
"interests": {
"terms": { "field": "interests" }
}
}
}
更新
由于該interests欄位是單個字串,因此您首先需要拆分包含的值。如果您不想重新索引所有資料,您可以使用一個就地更新資料的攝取管道來實作這一點。
首先使用這樣的split處理器創建管道:
PUT _ingest/pipeline/splitter
{
"processors": [
{
"split": {
"field": "interests",
"separator": ","
}
},
{
"trim": {
"field": "interests"
}
}
]
}
然后使用該新管道更新您的索引,如下所示:
POST your-index/_update_by_query?pipeline=splitter
您的interests欄位將從這里更改
{
"interests" : "A, C, D, E"
},
對此:
{
"interests" : [
"A",
"C",
"D",
"E"
]
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350363.html
標籤:弹性搜索
