2016-11-15 16 views
5

Tôi có một tài liệu như thế nàylĩnh vực Đổi tên trong elasticsearch

{ 
    "_index": "testindex", 
    "_type": "logs", 
    "_id": "1", 
    "_score": 1, 
    "_source": { 
     "field1": "data1", 
     "field2": "data2" 
    } 
} 

tôi cần phải thay đổi field2 để Request.field3

{ 
    "_index": "testindex", 
    "_type": "logs", 
    "_id": "1", 
    "_score": 1, 
    "_source": { 
     "field1": "data1", 
     "Request": { 
     "field3": "data2" 
     } 
    } 
} 

Đối với điều này, đầu tiên thêm một ánh xạ trường để chỉ số hiện tại

PUT testindex/_mapping/logs 
{ 
    "properties": 
    { 
     "Request": 
     { 
      "properties": 
      { 
       "field3" : 
       { 
        "type": "string" 
       } 
      } 
     } 
    } 
} 

Sau đó, thử reindexing

POST _reindex 
{ 
    "source": { 
     "index": "testindex" 
    }, 
    "dest": { 
     "index": "testindex1" 
    }, 
    "script": { 
     "inline": "ctx._source.Request.field3 = ctx._source.remove(\"field2\")" 
    } 
} 

Lỗi là

"reason": "failed to run inline script [ctx._source.Request.field3 = ctx._source.remove(\"field2\")] using lang [groovy]", 
"caused_by": { 
    "type": "null_pointer_exception", 
    "reason": "Cannot set property 'field3' on null object" 
} 
+0

cảm ơn @ saeed-zhiany để định dạng .. –

Trả lời

13

Trường Request chưa tồn tại trong tài liệu của bạn, vì vậy kịch bản của bạn cần phải tạo ra nó đầu tiên:

POST _reindex 
{ 
    "source": { 
     "index": "testindex" 
    }, 
    "dest": { 
     "index": "testindex1" 
    }, 
    "script": { 
     "inline": "ctx._source.Request = [:]; ctx._source.Request.field3 = ctx._source.remove(\"field2\") ]" 
    } 
} 

Hoặc ngắn hơn một chút như thế này:

POST _reindex 
{ 
    "source": { 
     "index": "testindex" 
    }, 
    "dest": { 
     "index": "testindex1" 
    }, 
    "script": { 
     "inline": "ctx._source.Request = [field3: ctx._source.remove(\"field2\") ]" 
    } 
} 
+0

Cảm ơn Val. Điều này làm việc. –

+0

Tuyệt vời, vui vì nó đã giúp! – Val

+0

Còn gì nữa không? – Val

Các vấn đề liên quan