【问题标题】:Painless script to add new fields into _source object when querying into elasticsearch v6.0.1查询到 elasticsearch v6.0.1 时,将新字段添加到 _source 对象中的无痛脚本
【发布时间】:2019-08-01 09:22:13
【问题描述】:

我有一个带有一个属性(id:整数)的字段映射的索引。 当我查询该索引时,我能够得到正确的响应。现在,我想在查询时使用无痛脚本在 _source 对象中添加一个额外的字段。 elasticsearch版本是6.0.1。

我已经尝试在查询块中添加脚本作为字段。但是它会抛出一个错误:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 7,
        "col": 7
      }
    ],
    "type": "parsing_exception",
    "reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 7,
    "col": 7
  },
  "status": 400
}
GET 20190719_candidate/candidate/_search
{
    "min_score": 0.001,
    "query": {
      "term": {
        "id": 1234
      }, 
      "script": {
        "script": {
          "inline": "doc['field_1'] = 'field_1_value'"
        }
      }
    }, 
    "from": 0,
    "size": 20
}

_source 对象的预期结果是:

{
  "id": "1234567",
  "field_1": "field_1_value"
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你缺少结构:

    GET 20190719_candidate/candidate/_search
    {
        "min_score": 0.001,
        "query": {
          "term": {
            "id": 1234
          }, 
          "script_fields": {
               "test1":{
                     "script": {
                          "lang": "painless",                       
                          "source": "'field_1_value'"
                     }
                }
          }
        }, 
        "from": 0,
        "size": 20
    }
    

    看看这个例子:

    GET /_search
    {
        "query" : {
            "match_all": {}
        },
        "script_fields" : {
            "test1" : {
                "script" : {
                    "lang": "painless",
                    "source": "doc['price'].value * 2"
                }
            },
            "test2" : {
                "script" : {
                    "lang": "painless",
                    "source": "doc['price'].value * params.factor",
                    "params" : {
                        "factor"  : 2.0
                    }
                }
            }
        }
    }
    

    来源:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-script-fields

    【讨论】:

      【解决方案2】:

      “根本原因”:[ { “类型”:“解析异常”, "reason": "[term] 格式错误的查询,应为 [END_OBJECT] 但找到 [FIELD_NAME]", “行”:7, “科尔”:7 } ],

      错误表明您的查询格式不正确,您在第 7 行中错过了一个右括号来关闭“查询”属性。

      你的查询应该是这样的:

      GET 20190719_candidate/candidate/_search
      {
          "min_score": 0.001,
          "query": {
            "term": {
              "id": 1234
            }}, 
            "script": {
                "lang": "painless",
                "inline": "doc['field_1'] = 'field_1_value'"
            }, 
          "from": 0,
          "size": 20
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        相关资源
        最近更新 更多