2016-10-07 22 views
1

我想通過提高基於字段值的_score來擺脫elasticsearch中的排序。這是我的場景:ElasticSearch:如何根據字段值提高分數?

我在我的文檔中有一個字段:applicationDate。這是自EPOC以來所經歷的時間。我希望記錄有更大的applicationDate(最近)有更高的得分。

如果兩個文檔的分數相同,我想在另一個字符串類型的字段上排序。說「狀態」是可以有價值的另一個字段(可用,正在進行中,已關閉)。因此,具有相同applicationDate的文檔應基於狀態具有_score。 可用的應該有更多的分數,進行中少一點,關閉,至少。所以通過這種方式,我不必在得到結果後對文檔進行排序。

請給我一些指點。

回答

2

你應該能夠使用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" 
     } 
    } 
    ] 
相關問題