我的檔案是這樣的:
[
{
'user_id': 1,
'search_text': 'hi',
'searched_departments': ["dep4", "dep5", "dep6"]
},
{
'user_id': 1,
'search_text': 'hi there',
'searched_departments': ["dep4", "dep6"]
},
{
'user_id': 5,
'search_text': 'I like candy',
'searched_departments': ["dep4", "dep11", "dep999"]
},
{
'user_id': 2,
'search_text': 'hi',
'searched_departments': ["dep4", "dep6", "dep7"]
}
]
我想做一個回傳每個部門計數的聚合,所以在這種情況下,我希望我的最終結果是這樣的:
{
"dep4" : 4,
"dep6" : 3,
"dep5" : 1,
# and so on
}
我的映射:
{'mappings': {'properties': {'date': {'type': 'date'},
'searched_departments': {'type': 'text'},
'search_text': {'type': 'text'},
'user_id': {'type': 'text'}}}
uj5u.com熱心網友回復:
您無法對欄位的文本型別進行聚合(如果您仍想對欄位的文本型別生成聚合,則應使用fielddata引數 to啟用欄位true)。為了獲得聚合,您可以將searched_departments欄位定義為具有關鍵字和文本兩種型別的多欄位。
以下是示例映射:
{
"mappings": {
"properties": {
"date": {
"type": "date"
},
"searched_departments": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"search_text": {
"type": "text"
},
"user_id": {
"type": "text"
}
}
}
}
然后下面的查詢會給你你的預期結果:
POST index_name/_search
{
"query": {
"match_all": {}
},
"aggs": {
"dept_count": {
"terms": {
"field": "searched_departments.keyword",
"size": 10
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/383829.html
