2015-01-14 118 views
0

在我elasticsearch,我需要刪除了,看起來像這樣的結構域:刪除字段elasticsearch

{"key": 
    {"anotherKey": 
       {"firstEntryKey":"firstValue"}, 
       {"secondEntry":"secondValue"} 
     } 
} 

我想從記錄中刪除secondEntry,這怎麼辦呢?

我使用更新的API嘗試,通過一個腳本,但這似乎並沒有工作:

{"script" : 
     "ctx._source.remove("key.anotherKey.secondEntry") 
} 

謝謝!

+0

這是嵌套文檔嗎? –

+0

我相信正確的teminology是多層次的對象,所以我知道它的關鍵和路徑,但我該如何刪除它? –

回答

1

既然你貼不會出現在文件是合法的,我假設你的意思是這樣的:

{ 
    "key": { 
     "anotherKey": { 
     "firstEntryKey": "firstValue", 
     "secondEntry": "secondValue" 
     } 
    } 
} 

所以,如果我創建一個索引,並張貼文件,

DELETE /test_index 

PUT /test_index 

PUT /test_index/doc/1 
{ 
    "key": { 
     "anotherKey": { 
     "firstEntryKey": "firstValue", 
     "secondEntry": "secondValue" 
     } 
    } 
} 

GET /test_index/doc/1 
... 
{ 
    "_index": "test_index", 
    "_type": "doc", 
    "_id": "1", 
    "_version": 1, 
    "found": true, 
    "_source": { 
     "key": { 
     "anotherKey": { 
      "firstEntryKey": "firstValue", 
      "secondEntry": "secondValue" 
     } 
     } 
    } 
} 

然後用新版本更新文檔,我找回新版本:

PUT /test_index/doc/1 
{ 
    "key": { 
     "anotherKey": { 
     "firstEntryKey": "firstValue" 
     } 
    } 
} 

GET /test_index/doc/1 
... 
{ 
    "_index": "test_index", 
    "_type": "doc", 
    "_id": "1", 
    "_version": 2, 
    "found": true, 
    "_source": { 
     "key": { 
     "anotherKey": { 
      "firstEntryKey": "firstValue" 
     } 
     } 
    } 
} 

這是我使用的代碼:

http://sense.qbox.io/gist/fb38750594550d4bf7f8a168883a168c7adc3d49

這是否解決問題了嗎?如果沒有,留下評論,我會盡力幫助。

+1

謝謝,這是一個有效的解決方案,但是我試圖使用更新api,並且我發現使用腳本的正確語法如下所示:{「script」:「ctx._source.key.anotherKey.remove ( 'secondEntry')「}。我使用uuids作爲鍵,但必須刪除所有短劃線,並確保它不是以數字開頭才能使其工作。 –