不替代自然评分的 ElasticSearch 自定义脚本评分

Posted

技术标签:

【中文标题】不替代自然评分的 ElasticSearch 自定义脚本评分【英文标题】:ElasticSearch custom script score that does not replace the natural scoring 【发布时间】:2020-12-14 10:33:39 【问题描述】:

我有一个 ElasticSearch 查询,其中包含用 Painless 语言编写的自定义脚本分数。现在 ES 请求看起来像这样,自然的 ES _score 完全被我脚本中的自定义评分所取代:

 
  "_source": 
    "excludes": [
      "field_to_exclude",
    ]
  ,
  "from": 0,
  "size": 100,
  "query": 
    "script_score": 
      "query": 
        "bool": 
          "must": 
            "match_all": 
          ,
          "filter": [
            
              "term": 
                "field_to_filter": 4
              
            
          ]
        
      ,
      "script": 
        "lang": "painless",
        "source": "COMPLEX_PAINLESS_SCRIPT"
      
    
  ,
  "sort": [
    
      "price": 
        "order": "asc"
      
    ,
    "_score"
  ]

根据前端的某些参数,我希望仍然能够单独计算 ES 自然评分,并将此自定义评分保留在另一个字段中计算,即使可能用作辅助排序标准。

这可能吗?

【问题讨论】:

【参考方案1】:

我终于想通了。如果您在sort 数组中使用自定义_script 字段以及用于自然ES 分数的_score 字段,则实际上可以做到这一点。

除此之外,我们还可以使用 script_fields 将自定义评分保留在响应中的特定字段中。

根据排序标准,两个 ES 请求看起来像:


  "_source": 
    "excludes": [
      "field_to_exclude",
    ]
  ,
  "from": 0,
  "size": 100,
  "query": 
    "bool": 
      "must": 
        "match_all": 
      ,
      "filter": [
        
          "term": 
            "field_to_filter": 4
          
        
      ]
    
  ,
  "sort": [
    
      "price": 
        "order": "desc"
      
    ,
    
      "_score": 
        "order": "desc"
      
    ,
    
      "_script": 
        "type": "number",
        "script": 
          "lang": "painless",
          "source": "COMPLEX_PAINLESS_SCRIPT"
        ,
        "order": "desc"
      
    
  ],
  "script_fields": 
    "custom_score": 
      "script": 
        "lang": "painless",
        "source": "COMPLEX_PAINLESS_SCRIPT"
      
    
  


  "_source": 
    "excludes": [
      "field_to_exclude",
    ]
  ,
  "from": 0,
  "size": 100,
  "query": 
    "bool": 
      "must": 
        "match_all": 
      ,
      "filter": [
        
          "term": 
            "field_to_filter": 4
          
        
      ]
    
  ,
  "sort": [
    
      "_script": 
        "type": "number",
        "script": 
          "lang": "painless",
          "source": "COMPLEX_PAINLESS_SCRIPT"
        ,
        "order": "desc"
      
    ,
    
      "_score": 
        "order": "desc"
      
    
  ],

  "script_fields": 
    "fit_score": 
      "script": 
        "lang": "painless",
        "source": "COMPLEX_PAINLESS_SCRIPT"
      
    
  

【讨论】:

以上是关于不替代自然评分的 ElasticSearch 自定义脚本评分的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch——评分机制详解

Elasticsearch学习笔记(十四)relevance score相关性评分的计算

ElasticSearch 默认评分机制

基于Elasticsearch的自定义评分算法扩展

基于匹配token比率的Elasticsearch评分

Elasticsearch系列---相关性评分算法及正排索引