2017-10-25 4 views
1

내 Elasticsearch 색인에서 여러 문서를 업데이트해야하며 _update_by_query 플러그인을 사용하여 다음을 시도했습니다._update_by_query를 사용하여 탄성 색인의 여러 문서에 json 객체를 추가하는 방법은 무엇입니까?

내가해야할 일은 특정 조건과 일치하는 기존 문서에 새 필드를 추가하는 것입니다. 새 필드는 중첩 된 JSON입니다. 그래서를 추가 한 후 문서 소스는 내가이 작업이 완료 얻을 수있는 _update_by_query API를 사용하여 시도

_source: { 
    ...existing fields, 
    "new_field" : { 
     "attrName1" : "value", 
     "attrName2" : "value", 
    } 
} 

처럼 보일 것입니다. 그러나 지금까지는 String 필드와 배열을 추가 할 수만있었습니다. JSON에 다음 쿼리를 추가하려고하면 오류가 발생합니다.

쿼리

curl -XPOST "http://xxx.xxx.xxx.xxx:pppp/my_index_name/_update_by_query" -d' 
{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "term": { 
      "team.keyword": "search_phrase" 
      } 
     } 
     ] 
    } 
    }, 
    "script" : { 
    "inline":"ctx._source.field_name = {\"a\":\"b\"}" 
    } 
}' 

오류

{ 
    "error": { 
    "root_cause": [ 
     { 
     "type": "script_exception", 
     "reason": "compile error", 
     "script_stack": [ 
      "ctx._source.field_name = {\"a\":\"b\"}", 
      "       ^---- HERE" 
     ], 
     "script": "ctx._source.field_name = {\"a\":\"b\"}", 
     "lang": "painless" 
     } 
    ], 
    "type": "script_exception", 
    "reason": "compile error", 
    "caused_by": { 
     "type": "illegal_argument_exception", 
     "reason": "invalid sequence of tokens near ['{'].", 
     "caused_by": { 
     "type": "no_viable_alt_exception", 
     "reason": null 
     } 
    }, 
    "script_stack": [ 
     "ctx._source.field_name = {\"a\":\"b\"}", 
     "       ^---- HERE" 
    ], 
    "script": "ctx._source.field_name = {\"a\":\"b\"}", 
    "lang": "painless" 
    }, 
    "status": 500 
} 

지금까지 난 단지 새로운 분야로 문자열을 추가 할 수 있습니다. 이것을 달성하기위한 올바른 방법은 무엇입니까?

답변

1

직접 할당 대신 params를 사용하여 동일한 결과를 얻을 수 있습니다.

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "term": { 
      "team.keyword": "search_phrase" 
      } 
     } 
     ] 
    } 
    }, 
    "script": { 
    "inline": "ctx._source.field_name = params.new_field", 
    "params": { 
     "new_field": { 
     "a": "b" 
     } 
    } 
    } 
}