gpt4 book ai didi

java - Elasticsearch 嵌套聚合

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:15 25 4
gpt4 key购买 nike

关于模式的一些文字,我有一种类型的文档(评论),其中包含评论列表(嵌套对象),每个评论都有以下字段:极性(负面或相反)、关键字(评论的主要词)、评论者。我的目标是找到最重要的否定和肯定关键字,并为每个关键字找到其相反的计数(如果关键字位于最上面的肯定关键字,我需要找到该关键字的否定计数)

例如(基于下面提供的数据)

  • 顶部负数
      iPhone - 2
      • 相反计数(正) - 2
    • 三星 - 1
      • 相反数(正数) - 0
  • 最积极的
      iPhone - 2
      • 相反计数(负) - 2

提前感谢您抽出时间。

架构:

curl -XPOST "http://localhost:9200/forum_poc" -d 
{
"settings": {
"number_of_shards": 9,
"number_of_replicas": 1
},
"mappings": {
"_default_": {
"_all": {
"enabled": false
},
"_source": {
"enabled": true
},
"dynamic": "false"
},
"ReviewEvent": {
"_source": {
"enabled": true
},
"properties": {
"Reviews": {
"type": "nested",
"include_in_parent": true,
"properties": {
"polarity": {
"type": "string",
"index": "not_analyzed",
"store": "true"
},
"reviewer": {
"type": "string",
"index": "not_analyzed",
"store": "true"
},
"keyword": {
"type": "string",
"index": "not_analyzed",
"store": "true"
}
}
}
}
}
}
}

数据:

curl -XPOST "http://localhost:9200/forum_poc" -d 
{"index":{"_index":"forum_poc","_type":"ReviewEvent","_id":0}}
{"Reviews":[{"polarity":"negative","reviewer":"jhon","keyword":"iphone"},{"polarity":"negative","reviewer":"kevin","keyword":"samsung"}]}
{"index":{"_index":"forum_poc","_type":"ReviewEvent","_id":1}}
{"Reviews":[{"polarity":"positive","reviewer":"Doron","keyword":"iphone"}]}
{"index":{"_index":"forum_poc","_type":"ReviewEvent","_id":2}}
{"Reviews":[{"polarity":"negative","reviewer":"Michel","keyword":"iphone"}]}
{"index":{"_index":"forum_poc","_type":"ReviewEvent","_id":4}}
{"Reviews":[{"polarity":"positive","reviewer":"Afi","keyword":"iphone"}]}

我的查询:

POST forum_poc/_search?search_type=count
{
"aggs": {
"aggregation": {
"nested": {
"path": "Reviews"
},
"aggs": {
"polarity": {
"terms": {
"field": "polarity",
"size": 10
},
"aggs": {
"keyword": {
"terms": {
"field": "keyword",
"size": 10
}
}
}
}
}
}
}
}

我需要每个关键字的相反计数。

{
"took": 7,
"timed_out": false,
"_shards": {
"total": 9,
"successful": 9,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"aggregation": {
"doc_count": 5,
"polarity": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "negative",
"doc_count": 3,
"keyword": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "iphone",
"doc_count": 2
},
{
"key": "samsung",
"doc_count": 1
}
]
}
},
{
"key": "positive",
"doc_count": 2,
"keyword": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "iphone",
"doc_count": 2
}
]
}
}
]
}
}
}

最佳答案

为什么不进行交换聚合级别的聚合。首先聚合关键字,然后聚合极性 -

POST forum_poc/_search?search_type=count
{
"aggs": {
"aggregation": {
"nested": {
"path": "Reviews"
},
"aggs": {
"polarity": {
"terms": {
"field": "keyword",
"size": 10
},
"aggs": {
"keyword": {
"terms": {
"field": "polarity",
"size": 10
}
}
}
}
}
}
}
}

关于java - Elasticsearch 嵌套聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28557944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com