zoukankan      html  css  js  c++  java
  • Elasticsearch painless脚本中空值的检查

    最近在做脚本重评分时,遇上了一个空值问题。查询时painless脚本中若遇到字段中的值为空值就会报错,本来想用 value == null 这种形式的判断来判断字段值是否为空,然后过来掉,结果发现并不行,

    以下时错误示范:

    image

    这个重评分的代码在defprice字段全部都有值得时候运行正常,但是一旦出现没有值得情况,就会报以下错误

    {
      "error": {
        "root_cause": [{
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",
            "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",
            "if(doc['cpinfo_youkewang.defprice'].value>0) {",
            "                                   ^---- HERE"
          ],
          "script": "if(doc['cpinfo_youkewang.defprice'].value>0) {return 1.0/doc['cpinfo_youkewang.defprice'].value;} else return 0;",
          "lang": "painless"
        }],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [{
          "shard": 0,
          "index": "aiso_20q2v1_detail_gasstation",
          "node": "NE8IhhppQKalVXnjU-axBQ",
          "reason": {
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
              "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",
              "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",
              "if(doc['cpinfo_youkewang.defprice'].value>0) {",
              "                                   ^---- HERE"
            ],
            "script": "if(doc['cpinfo_youkewang.defprice'].value>0) {return 1.0/doc['cpinfo_youkewang.defprice'].value;} else return 0;",
            "lang": "painless",
            "caused_by": {
              "type": "illegal_state_exception",
              "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
            }
          }
        }]
      },
      "status": 400
    }

    辗转之后偶然看了下报错信息,发现报错原因里面有提示如何看是否为空值,

    image

    于是把代码改成以下就可以了:

    "rescore": [{
        "window_size": 10000,
        "query": {
          "score_mode": "total",
          "rescore_query": {
            "function_score": {
              "script_score": {
                "script": "if(doc['cpinfo_youkewang.defprice'].size() > 0 && doc['cpinfo_youkewang.defprice'].value>0) {return 1.0/doc['cpinfo_youkewang.defprice'].value;} else return 0;"
              }
            }
          }
        }
      }],

    就是用得提示中得doc[<field>].size()来判断的。


  • 相关阅读:
    深入理解JavaScript系列(41):设计模式之模板方法
    深入理解JavaScript系列(40):设计模式之组合模式
    深入理解JavaScript系列(39):设计模式之适配器模式
    深入理解JavaScript系列(38):设计模式之职责链模式
    Mysql的用户名密码设置方法
    微信公众平台开发教程新手解惑40则
    自定义菜单的创建及菜单事件响应
    微信接口测试之发送被动响应消息
    QQ表情的发送与接收
    properties文件使用{0}...
  • 原文地址:https://www.cnblogs.com/betterwgo/p/13368431.html
Copyright © 2011-2022 走看看