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
相关推荐
HideF5 小时前
Linux
linux·服务器·网络
Ruiery7 小时前
Linux 6.6内核内存管理深度解析(十三):slab/slub 分配器
linux·运维·服务器
Eloudy8 小时前
Qwen2.5-14B-Instruct-AWQ 模型文件结构分析
大数据·elasticsearch·搜索引擎
丰锋ff10 小时前
压缩命令笔记
linux
GeW11 小时前
产业新优势基础软件的人才底盘,从Linux与数据库开始
linux·数据库
by2099911 小时前
Linux GCC/G++ 编译与 Makefile 构建
linux·运维·服务器·经验分享
cq林志炫12 小时前
两个Linux服务器之间同步文件
linux·运维·服务器
无人生还别怕13 小时前
如何搭建k8s服务(containerd部署)
linux·云原生·容器·kubernetes·openanolisos
企鹅侠客14 小时前
Linux CPU 负载平均值(Load Average)详解:从原理到实践
linux·运维·服务器·服务器负载
易点云徐源14 小时前
HP 暗影精灵 PRO 16 安装双系统后,开机直接进入 Windows,按 F9 才能进入 Ubuntu:GRUB 菜单不显示的处理方法
linux·运维·ubuntu