Nis*_*mar 1 lucene elasticsearch elasticsearch-2.0
我试图通过基于字段值提升_score来摆脱Elasticsearch中的排序。这是我的情况:
我的文档中有一个字段:applicationDate。自EPOC以来已经过去了。我希望记录具有更大的applicationDate(最新)以具有更高的分数。
如果两个文档的分数相同,我想在另一个字符串类型的字段上对它们进行排序。说“状态”是另一个可以具有值的字段(可用,正在进行中,已关闭)。因此,具有相同applicationDate的文档应具有基于状态的_score。可用分数应该更高,进行中的分数应该更低,封闭的分数最少。因此,通过这种方式,我不必在获得结果后对文档进行排序。
请给我一些指示。
您应该可以使用Function Score实现此目的。根据您的要求,它可能像下面的示例一样简单:
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"
}
}
]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1198 次 |
最近记录: |