【问题标题】:ElasticSearch : How can I boost score depending on field value?ElasticSearch:如何根据字段值提高分数?
【发布时间】:2017-02-16 09:35:37
【问题描述】:

我试图通过基于字段值提高 _score 来摆脱弹性搜索中的排序。这是我的场景:

我的文档中有一个字段:applicationDate。这是自 EPOC 以来经过的时间。我希望具有更大 applicationDate (最近)的记录具有更高的分数。

如果两个文档的分数相同,我想在另一个字符串类型的字段上对它们进行排序。说“状态”是另一个可以具有值的字段(可用、进行中、关闭)。因此,具有相同 applicationDate 的文档应该具有基于状态的 _score。 Available 应该有更多 score , In Progress 一个 less , Cl​​osed , minimum 。所以通过这种方式,我不必在得到结果后对文档进行排序。

请给我一些指示。

【问题讨论】:

    标签: elasticsearch lucene elasticsearch-2.0


    【解决方案1】:

    您应该可以使用 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"
            }
         }
      ]
    

    【讨论】:

    • 有没有办法使用java客户端来实现这个?
    【解决方案2】:

    你看过功能分数吗? https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

    具体看上述文档中的衰减函数。

    【讨论】:

      【解决方案3】:

      有一个名为 rank_feature_field 的新字段可用于此用例:

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

      【讨论】:

        猜你喜欢
        • 2018-10-22
        • 2013-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        • 2017-07-07
        • 2014-06-06
        • 1970-01-01
        相关资源
        最近更新 更多