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
相关推荐
donoot3 小时前
Linux系统下图书馆级电子书全自动标准化分类整理完整实施方案
大数据·linux·运维·电子书管理
Felix-lxd4 小时前
Ubuntu 22.04 配置 Nginx
linux·nginx·ubuntu
Elastic 中国社区官方博客4 小时前
如何比较两个 Elasticsearch 索引并找出缺失的文档
大数据·运维·数据库·elasticsearch·搜索引擎
Tim_Van5 小时前
在 CentOS 7 中安装 Chromium 的完整步骤
linux·运维·chrome·centos
2023自学中5 小时前
C++ 内存追踪器
linux·c++
啵啵鱼爱吃小猫咪6 小时前
Ubuntu 20.04 + ROS Noetic 源码安装 libfranka 0.8.0 和 franka_ros 0.8.0 完整教程
linux·运维·ubuntu
ljs6482739517 小时前
Linux运维实操:vi编辑器永久配置静态IP(CentOS系列)
linux·运维·编辑器
dddwjzx7 小时前
嵌入式Linux C应用编程入门——高级I/O
linux·嵌入式
阿里云大数据AI技术8 小时前
Elasticsearch 智能助手:Agent 让运维从经验驱动迈向智能协同
人工智能·elasticsearch·agent
Waay9 小时前
Linux 三个核心环境变量配置文件、作用域、生效方式完整梳理
linux·运维·学习·云原生·容器