对象映射错误在弹性搜索中将groson脚本中的Json添加到数组类型时

时间:2016-09-07 11:01:53

标签: elasticsearch groovy

以下是对弹性搜索的请求。

post sample/item_type/1/_update
{
"script" : {
    "file" : "my_script",
    "params" : {
        "slice_id" : 2,
        "hash" : "xyjjz"
    }
},
"upsert" : {
    "item_body" : {
        "name" : "dummy"
    }, 
    "attributes" : {
        "name" : "attributes"
    },
    "versions": [
        {
            "version_id" : 1,
            "begin_slice_id" : 1,
            "end_slice_id" : 1,
            "hash" : "xyz",
            "count" : 1
        }
    ],
    "version_count" : 1
}

以下是我在上述请求中使用的groovy脚本。

import groovy.json.JsonBuilder

 Integer versionCount = ctx._source.version_count;
 if (ctx._source.versions[versionCount-1].hash == hash)
 {
     ctx._source.versions[versionCount-1].end_slice_id = slice_id;
     ctx._source.versions[versionCount-1].count++; 
 }
 else
{
    def json = new JsonBuilder()
    def root = json{
        "name" : "wh"
    }
    ctx._source.versions+=root.toString();
    ctx._source.version_count += 1;
}

以下是我得到的错误

{
  "error": {
  "root_cause": [
     {
        "type": "mapper_parsing_exception",
        "reason": "object mapping for [versions] tried to parse field [null] as object, but found a concrete value"
     }
  ],
  "type": "mapper_parsing_exception",
  "reason": "object mapping for [versions] tried to parse field [null] as object, but found a concrete value"
   },
   "status": 400

我理解这个问题,版本数组插入了不同类型的对象。首先直接从查询主体,然后从groovy脚本。但是如何在没有弹性搜索的情况下从groovy插入Json抱怨映射。提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

else
{
    def json = new JsonBuilder()
    def root = json name:"wh"
    ctx._source.versions+=json.content;
    ctx._source.version_count += 1;
}