2016-09-07 17 views
0

下面是對彈性搜索的請求。對象映射錯誤將彈性搜索中的Json從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而沒有彈性搜索的抱怨映射。提前致謝。

回答

0

嘗試這樣的:

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