您可以使用以下查詢使用terms aggregation
和top_hits
。
{
"size": 0,
"aggs": {
"group": {
"terms": {
"script": "_source.field1.each{}"
},
"aggs":{
"top_hits_log" :{
"top_hits" :{
}
}
}
}
}
}
輸出將是:
"buckets": [
{
"key": "item1",
"doc_count": 3,
"top_hits_log": {
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "so",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"field1": [
"item1",
"item2",
"item3"
],
"field2": "something1"
}
},
{
"_index": "so",
"_type": "test",
"_id": "2",
"_score": 1,
"_source": {
"field1": [
"item1"
],
"field2": "something2"
}
},
{
"_index": "so",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"field1": [
"item1",
"item2"
],
"field2": "something3"
}
}
]
}
}
},
{
"key": "item2",
"doc_count": 2,
"top_hits_log": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "so",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"field1": [
"item1",
"item2",
"item3"
],
"field2": "something1"
}
},
{
"_index": "so",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"field1": [
"item1",
"item2"
],
"field2": "something3"
}
}
]
}
}
},
{
"key": "item3",
"doc_count": 1,
"top_hits_log": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "so",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"field1": [
"item1",
"item2",
"item3"
],
"field2": "something1"
}
}
]
}
}
}
]
確保啓用dynamic scripting
。設置script.disable_dynamic: false
希望這會有所幫助。
你能分享一下你想要的結果嗎? – Richa
@Richa說我有3個記錄:記錄1具有{ 「field1的」: 「[」 ITEM1" , 「ITEM2」, 「項目3」], 「另一惡魔」: 「其他值」,...},RECORD2: { 「字段1」: 「[」 物品1 「],...},RECORD3:{」 字段1 「:」[ 「物品1」, 「ITEM2」],...}現在我想找到一種方法來聚合所以這一結果將會像{ 「物品1」:「記錄1:{數據從-記錄1},RECORD2:十數據來自-RECORD2},RECORD3:十數據來自-RECORD3}, 「ITEM2」:記錄1,RECORD3 ,「item3」:Record1} – JChao