我试图通过提高基于字段值的_score来消除elasticsearch中的排序问题。下面是我的场景:
我的文档中有一个字段: applicationDate。这是自EPOC以来的一段时间。我希望有更高的applicationDate (最近的)记录有更高的分数。
如果两个文档的得分相同,我希望在另一个类型为String的字段上对它们进行排序。说“状态”是另一个有价值的字段(可用,正在进行中,已关闭)。因此,具有相同applicationDate的文档应该具有基于状态的_score。可用应该有更多的分数,在进度较少,关闭,最少。因此,通过这种方法,我将不必在得到结果后对文档进行排序。
请给我一些指点。
发布于 2016-10-07 14:53:15
您应该能够使用功能评分实现这一点。根据您的需求,它可以像下面的示例一样简单:
put test/test/1
{
"applicationDate" : "2015-12-02",
"status" : "available"
}
put test/test/2
{
"applicationDate" : "2015-12-02",
"status" : "progress"
}
put test/test/3
{
"applicationDate" : "2016-03-02",
"status" : "progress"
}
post test/_search
{
"query": {
"function_score": {
"functions": [
{
"field_value_factor" : {
"field" : "applicationDate",
"factor" : 0.001
}
},
{
"filter": {
"term": {
"status": "available"
}
},
"weight": 360
},
{
"filter": {
"term": {
"status": "progress"
}
},
"weight": 180
}
],
"boost_mode": "multiply",
"score_mode": "sum"
}
}
}
**Results:**
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 1456877060,
"_source": {
"applicationDate": "2016-03-02",
"status": "progress"
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 1449014780,
"_source": {
"applicationDate": "2015-12-02",
"status": "available"
}
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 1449014660,
"_source": {
"applicationDate": "2015-12-02",
"status": "progress"
}
}
]发布于 2016-10-07 14:38:53
你看过功能分数了吗?https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
特别是在上面的文档中查看衰变函数。
发布于 2021-01-09 12:16:28
有一个名为rank_feature_field的新字段可用于此usecase:
https://www.elastic.co/guide/en/elasticsearch/reference/current/rank-feature.html
https://stackoverflow.com/questions/39916339
复制相似问题