弹性搜索中的聚合与排序
我想在弹性搜索中对查询的聚合结果进行排序 等价SQL查询:-按col1从表组中选择col1、col2、sum(col3),按sum(col3)desc顺序选择col2 我尝试了下面的查询,它返回结果,但不是我期望的排序顺序弹性搜索中的聚合与排序,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我想在弹性搜索中对查询的聚合结果进行排序 等价SQL查询:-按col1从表组中选择col1、col2、sum(col3),按sum(col3)desc顺序选择col2 我尝试了下面的查询,它返回结果,但不是我期望的排序顺序 { "from": 0, "size": 0, "_source": { "includes": [ "col1", "c
{
"from": 0,
"size": 0,
"_source": {
"includes": [
"col1",
"col2",
"SUM"
],
"excludes": []
},
"stored_fields": [
"col1",
"col2"
],
"aggregations": {
"col1": {
"terms": {
"field": "col1",
"size": 200,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false
},
"aggregations": {
"col2": {
"terms": {
"field": "col2",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false
},
"aggregations": {
"SUM_0": {
"sum": {
"field": "col3"
}
},
"col3_bucket_sort": {
"bucket_sort": {
"sort": [
{ "SUM_0": { "order": "desc" } }
],
"size": 3
}
}
}
}
}
}
}
}
抽样索引数据
{
"_index": "my_index",
"_type": "products",
"_id": "OJfBSXUB0GzAt2o_zVdS",
"_score": 1.0,
"_source": {
"product_name": "car",
"product_type": "retail",
"qty": 5
}
}
{
"_index": "my_index",
"_type": "report",
"_id": "OpfBSXUB0GzAt2o_zVfG1",
"_score": 1.0,
"_source": {
"product_name": "bike",
"product_type": "retail",
"qty": 5
}
},
{
"_index": "my_index",
"_type": "report",
"_id": "OpfBSXUB0GzAt2o_zVfG",
"_score": 1.0,
"_source": {
"product_name": "car",
"product_type": "retail",
"qty": 3
}
},
{
"_index": "my_index",
"_type": "report",
"_id": "OpfBSXUB0GzAt2o_zVfG2",
"_score": 1.0,
"_source": {
"product_name": "bike",
"product_type": "retail",
"qty": 1
}
}
预期输出:-希望根据字段product_name和product_type聚合(分组)我的文档,并按总和(数量)排序
等价SQl查询:-按产品名称从产品表组中选择产品名称、产品类型、合计(数量)、按合计(数量)说明选择产品类型订单
但我得到了低于输出的结果,即成功地对文档进行了聚合,但排序不适用于总和(数量)
指摘自 与所有管道聚合一样,bucket_排序聚合是 在所有其他非管道聚合之后执行。这意味着 排序仅适用于已从 父聚合。例如,如果父聚合是术语 如果将其大小设置为10,则bucket_排序将仅对这些 10个返回的术语桶
以上是您的查询未给出正确结果的原因。指的是 与所有管道聚合一样,bucket_排序聚合是 在所有其他非管道聚合之后执行。这意味着 排序仅适用于已从 父聚合。例如,如果父聚合是术语 如果将其大小设置为10,则bucket_排序将仅对这些 10个返回的术语桶
以上是您的查询未给出正确结果的原因。因为您是按col1、col2对数据进行分组(即使用两项聚合),因此,当您尝试使用bucket sort aggregation根据总和聚合对结果进行排序时,结果并不合适 您需要使用的是同级管道聚合,它使用同级聚合中指定度量的最大值标识存储桶,并输出存储桶的值和键 然后,您应该对聚合结果执行 添加带有索引数据(与问题中使用的数据相同)、搜索查询和搜索结果的工作示例 搜索查询:
{
"size": 0,
"aggs": {
"agg1": {
"terms": {
"field": "product_name.keyword"
},
"aggs": {
"agg2": {
"terms": {
"field": "product_type.keyword"
},
"aggregations": {
"SUM_0": {
"sum": {
"field": "qty"
}
}
}
},
"sum_max_bucket": {
"max_bucket": {
"buckets_path": "agg2>SUM_0" <-- note this
}
},
"sum_bucket_sort": {
"bucket_sort": {
"sort": {
"sum_max_bucket": {
"order": "desc"
}
}
}
}
}
}
}
}
"aggregations": {
"agg1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "car",
"doc_count": 2,
"agg2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "retail",
"doc_count": 2,
"SUM_0": {
"value": 8.0 <-- note this
}
}
]
},
"sum_max_bucket": {
"value": 8.0,
"keys": [
"retail"
]
}
},
{
"key": "bike",
"doc_count": 2,
"agg2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "retail",
"doc_count": 2,
"SUM_0": {
"value": 6.0 <-- note this
}
}
]
},
"sum_max_bucket": {
"value": 6.0,
"keys": [
"retail"
]
}
}
]
}
{
“大小”:0,
“aggs”:{
“agg1”:{
“条款”:{
“字段”:“产品名称.关键字”
},
“aggs”:{
“agg2”:{
“条款”:{
“字段”:“产品类型.关键字”
},
“聚合”:{
“总和0”:{
“总和”:{
“字段”:“数量”
}
}
}
},
“总和最大桶”:{
“最大桶”:{
“bucket_path”:“agg2>SUM_0”因为您是通过col1、col2对数据进行分组(即使用两项聚合),因此当您尝试使用bucket sort aggregation根据SUM aggregation对结果进行排序时,结果并不合适
您需要使用的是同级管道聚合,它使用同级聚合中指定度量的最大值标识存储桶,并输出存储桶的值和键
然后,您应该对聚合结果执行
添加带有索引数据(与问题中使用的数据相同)、搜索查询和搜索结果的工作示例
搜索查询:
{
"size": 0,
"aggs": {
"agg1": {
"terms": {
"field": "product_name.keyword"
},
"aggs": {
"agg2": {
"terms": {
"field": "product_type.keyword"
},
"aggregations": {
"SUM_0": {
"sum": {
"field": "qty"
}
}
}
},
"sum_max_bucket": {
"max_bucket": {
"buckets_path": "agg2>SUM_0" <-- note this
}
},
"sum_bucket_sort": {
"bucket_sort": {
"sort": {
"sum_max_bucket": {
"order": "desc"
}
}
}
}
}
}
}
}
"aggregations": {
"agg1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "car",
"doc_count": 2,
"agg2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "retail",
"doc_count": 2,
"SUM_0": {
"value": 8.0 <-- note this
}
}
]
},
"sum_max_bucket": {
"value": 8.0,
"keys": [
"retail"
]
}
},
{
"key": "bike",
"doc_count": 2,
"agg2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "retail",
"doc_count": 2,
"SUM_0": {
"value": 6.0 <-- note this
}
}
]
},
"sum_max_bucket": {
"value": 6.0,
"keys": [
"retail"
]
}
}
]
}
{
“大小”:0,
“aggs”:{
“agg1”:{
“条款”:{
“字段”:“产品名称.关键字”
},
“aggs”:{
“agg2”:{
“条款”:{
“字段”:“产品类型.关键字”
},
“聚合”:{
“总和0”:{
“总和”:{
“字段”:“数量”
}
}
}
},
“总和最大桶”:{
“最大桶”:{
“bucket\u path”:“agg2>SUM\u 0”你能提供一些样本索引数据和你期望的搜索结果吗?@Bhavya我已经添加了所需的信息。这是一段很长的时间。如果我的答案帮助你解决了问题,你能提供一些样本索引数据和你期望的搜索结果,如果你能投票并接受我的答案,那就太好了。你能提供一些样本索引数据和你期望的搜索结果吗?@Bhavya我已经添加了所需的信息这是一段很长的时间。如果你能投票并接受我的答案,那就太好了。如果我的答案帮助你解决了你的问题,我也在探索如何实现你想要的……我将更新答案。我也在探索如何实现你想要的……我将更新答案。@Codinggeek这是一段很长的时间了。我希望你做得很好:)如果我的答案帮助你解决了问题,那你就可以投票接受我的答案了@Codinggeek谢谢你接受答案,你也可以投票接受我的答案吗@Codinggeek已经很久了。我希望你做得很好:)如果我的答案帮助你解决了问题,你就可以投票接受我的答案,那就太好了e@Codinggeek谢谢你接受这个答案,请你也投票给我好吗