Elasticsearch按热门命中参数中的字段排序

niwlg2el  于 7个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(64)

我试图在搜索查询Elasticsearch中通过top_hits参数对数据进行排序,但不知何故,它并没有影响任何东西。有人可以帮助我吗?
所以我试着使用sort,就像有些人说的那样:

{
    "size" : 0,
    "from" : 0,
    "aggs": {
        "by_filter": {
            "filter": {
                "bool": {
                    "must": [
                    {
                        "range": {
                            "published_at": {
                                "gte": "2019-08-01 00:00:00",
                                "lte": "2023-10-30 23:59:59"
                            }
                        }
                    },
                    {
                        "match": {
                            "status": "published"
                        }
                    }
                    ]
                }
            },
            "aggs": {
                "by_created": {
                    "terms": {
                        "field": "created_by.id",
                        "size": 10
                    },
                    "aggs" : {
                        "count_data": {
                            "terms": {
                                "field": "created_by.id"
                            }
                        },
                        "hits": {
                            "top_hits": {
                                "sort": [                         <---- the sort query that I found
                                    {
                                        "created_by.name.keyword": {
                                            "order": "desc"
                                        }
                                    }
                                ],
                                "_source":["created_by.name"],
                                "size": 1
                            }
                        }
                    }
                }
            }
        }
    }
}

字符串
但结果并没有改变

"aggregations": {
    "by_filter": {
        "doc_count": 21,
        "by_created": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 3,
            "buckets": [
                {
                    "key": 34,
                    "doc_count": 3,
                    "hits": {
                        "hits": {
                            "total": {
                                "value": 3,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "re_article",
                                    "_id": "53822",
                                    "_score": null,
                                    "_source": {
                                        "created_by": {
                                            "name": "Edwin"
                                        }
                                    },
                                    "sort": [                <--- I think this is the result of the sort
                                        "Edwin"
                                    ]
                                }
                            ]
                        }
                    },
                    "count_data": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 34,
                                "doc_count": 3
                            }
                        ]
                    }
                },
                {
                    "key": 52,
                    "doc_count": 3,
                    "hits": {
                        "hits": {
                            "total": {
                                "value": 3,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "re_article",
                                    "_id": "338610",
                                    "_score": null,
                                    "_source": {
                                        "created_by": {
                                            "name": "Tito"
                                        }
                                    },
                                    "sort": [
                                        "Tito"
                                    ]
                                }
                            ]
                        }
                    },
                    "count_data": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 52,
                                "doc_count": 3
                            }
                        ]
                    }
                }
            ]
        }
    }
}


我所期望的是桶首先显示created_by name“蒂托”,然后显示“Edwin”,如果可能的话,就像这样:

"aggregations": {
    "by_filter": {
        "doc_count": 21,
        "by_created": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 3,
            "buckets": [
                {
                    "key": 52,
                    "doc_count": 3,
                    "hits": {
                        "hits": {
                            "total": {
                                "value": 3,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "re_article",
                                    "_id": "338610",
                                    "_score": null,
                                    "_source": {
                                        "created_by": {
                                            "name": "Tito"
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    "count_data": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 52,
                                "doc_count": 3
                            }
                        ]
                    }
                },
                {
                    "key": 34,
                    "doc_count": 3,
                    "hits": {
                        "hits": {
                            "total": {
                                "value": 3,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "re_article",
                                    "_id": "53822",
                                    "_score": null,
                                    "_source": {
                                        "created_by": {
                                            "name": "Edwin"
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    "count_data": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 34,
                                "doc_count": 3
                            }
                        ]
                    }
                }
            ]
        }
    }
}


我想我选择了一个错误的例子,因为在top_hits结果中有新的“sort”字段,但不是我真正想要的,有人能帮忙吗?谢谢
这是我的数据示例:

{
    "id": 53822,
    "created_at": "2019-09-03 18:17:13",
    "published_at": "2019-09-04 01:17:13",
    "status": "published",
    "created_by": {
        "id": 34,
        "name": "Edwin",
        "role_id": 4,
        "is_active": "Y"
},
{
    "id": 338610,
    "created_at": "2022-10-16 20:48:39",
    "published_at": "2022-10-16 21:08:12",
    "status": "published",
    "created_by": {
        "id": 52,
        "name": "Tito",
        "role_id": 4,
        "is_active": "Y"
},
{
    "id": 54272,
    "created_at": "2019-09-10 08:28:57",
    "published_at": "2019-09-10 15:30:03",
    "status": "published",
    "created_by": {
        "id": 34,
        "name": "Edwin",
        "role_id": 4,
        "is_active": "Y"
}


我尝试使用count_data aggs按字段created_by.id分组,然后按created_by.name对结果进行排序,这就是为什么我包含top hits参数,因为我需要显示人员的名称,而不仅仅是id
另外,我需要按created_by.id分组的键,而不是按created_by.name分组的键,即使同一个id总是具有相同的名称

dy2hfwbg

dy2hfwbg1#

不幸的是,没有好的方法来做到这一点。有一些方法可以通过子聚合的数值对聚合进行排序,但在您的情况下,您需要通过字符串值进行排序。我将按照我的偏好从最差到最好列出一些可能的解决方法:

  • 使用运行时字段,你可以合并将你的键组合为Edwin:34,并使用这个Tito:52,并在这个运行时字段上运行terms聚合。这里的问题是,你需要在你的应用程序中解析键,如果你改变了没有id的名称,它将产生两个桶,而不是一个。
  • 因为你已经在做后期处理了,你可以使用run terms aggregation by name,然后使用top_hits来检索id,并使用这个id进行查找。这个解决方案的问题是,如果你有不同id的匹配名称,或者如果名称更改,它会中断。
  • 由于您已经在应用中进行了后期处理,因此您可以按照示例中的方式检索name,并在应用中对bucket进行排序。

我知道这不是您正在寻找的解决方案,但据我所知,这是我们在当前聚合框架的限制下所能做的最好的事情。

相关问题