【问题标题】:to use elastic search's script_score to get the value of a long type field by using the doc['field_name'] syntax failed使用弹性搜索的 script_score 通过使用 doc['field_name'] 语法获取长类型字段的值失败
【发布时间】:2017-04-27 11:53:10
【问题描述】:

我使用弹性搜索的 script_score 通过使用 doc['field_name'] 语法`获取长类型字段的值,而查询操作抛出异常。 查询语法及异常详情如下:

     curl -XPOST http://211.159.155.44:9200/iktest/item/_search?pretty  -d'
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "content": "likaifu"
        }
      },
      "functions": [
        {
          "gauss": {
            "date": {
              "offset": "0d",
              "scale":  "5d"
            }
          }
        },
        {
          "script_score": {
            "script": {
              "lang":   "expression",
              "inline": "doc['score'].value"
            }
          }
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "replace"
    }
  }
}
'

"caused_by" : {
      "type" : "query_shard_exception",
      "reason" : "script_score: the script could not be loaded",
      "index_uuid" : "jVYGC8-jTpCCyZJi-VJtmA",
      "index" : "iktest",
      "caused_by" : {
        "type" : "script_exception",
        "reason" : "compile error",
        "caused_by" : {
          "type" : "parse_exception",
          "reason" : "unexpected character '[' on line (1) position (3)",
          "caused_by" : {
            "type" : "lexer_no_viable_alt_exception",
            "reason" : null
          }
        },
        "script_stack" : [
          "doc[score].value",
          "   ^---- HERE"
        ],
        "script" : "doc[score].value",
        "lang" : "expression"
      }
    }

【问题讨论】:

  • 尝试研究如何正确转义“inline”行中的单引号:“doc['score'].value”
  • 也许像 "inline": "doc[\"score\"].value" 会起作用?
  • @DanielGray 将内联语法更改为 "inline": "doc[\"score\"].value" 成功。非常感谢。

标签: elasticsearch


【解决方案1】:

转义 script 元素的 inline 键的值...后端解析器似乎不能很好地处理单引号字符。

将单引号字符 (') 更改为转义的普通/双引号 (",前面为 \),或者更改行:

"inline": "doc['score'].value"

"inline": "doc[\"score\"].value"

【讨论】:

    猜你喜欢
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多