簡體   English   中英

ElasticSearch:如何根據字段值提高分數?

[英]ElasticSearch : How can I boost score depending on field value?

我試圖通過基於字段值提升 _score 來擺脫 elasticsearch 中的排序。 這是我的場景:

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

如果兩個文檔的分數相同,我想在另一個字符串類型的字段上對它們進行排序。 說“狀態”是另一個可以有價值的字段(Available, in progress, closed )。 因此,具有相同 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"
        }
     }
  ]

你看過函數分數嗎? https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

具體看上面文檔中的衰減函數。

有一個名為 rank_feature_field 的新字段可用於此用例:

https://www.elastic.co/guide/en/elasticsearch/reference/current/rank-feature.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM