Elasticsearch搜索出现NAN异常

原因分析

  • Elasticsearch默认的打分,一般是不会出现异常的
  • 之所以会出现NAN异常,往往是因为我们重新计算了打分,使用了function_score
  • 核心原因是在function_score中,出现了计算异常,比如 0/0,比如log1p(x),x为负数等

真实案例分析

测试索引

bash 复制代码
PUT tx
POST tx/_mapping
{
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "time" : {
          "type" : "date",
          "format" : "yyyy-MM-dd"
        },
        "title" : {
          "type" : "text",
          "analyzer" : "ik_smart"
        }
      }
}
POST tx/_doc/19
{
  "title":"中古",
   "time" : "1969-10-12",
   "age":12
}

搜索query示例

bash 复制代码
POST /tx/_search
{
  "query": {
    "function_score": {
      "query": {
          "match_all": {}
      },
      "field_value_factor": {
        "field": "time",
        "modifier": "log1p"
      }
    }
  }
}
- 其实modifier.log1p和如下搜索是完全一样的
POST /tx/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "script_score": {
        "script": "_score * Math.log1p(1+doc['time'].value.toInstant().toEpochMilli())"
      }
    }
  }
}

搜索异常返回值

bash 复制代码
{
  "error" : {
    "root_cause" : [
      {
        "type" : "exception",
        "reason" : "function score query returned an invalid score: NaN for doc: 0"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "tx",
        "node" : "aPOBCVYkRA-a0R9FR4eDzQ",
        "reason" : {
          "type" : "exception",
          "reason" : "function score query returned an invalid score: NaN for doc: 0"
        }
      }
    ]
  },
  "status" : 500
}

搜索异常原因分析

  • 搜索使用了function_score重新打分,并且使用了log1p函数
  • 但是由于数据中的时间是"1969-10-12",但是es计算时间是从1970年计算开始,这个值换算成毫秒就是负数,导致log1p异常
  • 所以最终得分异常,出现是NAN
相关推荐
名誉寒冰9 分钟前
深入理解fd_set:从基础到实战应用(Linux/C++)
java·linux·c++
2201_7611990412 分钟前
jenkins找不到Build when a change is pushed to GitHub
运维·github·jenkins
此生只爱蛋13 分钟前
【Linux】内网穿透(实操)/打洞
linux·github
老猿讲编程24 分钟前
【车载信息安全系列1】车载Linux系统常用的OpenSSL, HSE加密工作原理
linux·网络
猛喝威士忌24 分钟前
【虚拟机】使用OpenWrt作为虚拟机集群的软路由(下)
linux·网络协议
博语小屋27 分钟前
Linux 地址转换函数详解
linux·运维·服务器·c++
饱饱要坚持可持续发展观31 分钟前
Linux 防火墙开放/限制端口
linux·运维·网络
longxibo1 小时前
【Ubuntu datasophon1.2.1 二开之三:解决HDFS安装后,启动失败:sudo: unknown user hdfs】
linux·ubuntu·hdfs
初学者_xuan1 小时前
安装与配置Jenkins(OpenEuler)
运维·servlet·jenkins
SunPengMSE1 小时前
基于运行unitree rl lab 的 Ubuntu 20.04 配置isaac sim + isaac lab
linux·运维·ubuntu