在elasticsearch中删除字段

Deleting field in elasticsearch

在我的 elasticsearch 中,我需要删除一个结构如下所示的字段:

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

我想从记录中删除第二个条目,该怎么做?

我试过使用更新 api,传递脚本,但这似乎不起作用:

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

谢谢!

由于您 post 编辑的文件似乎不合法,我假设您的意思是:

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

因此,如果我创建一个索引并 post 该文档,

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

这是否解决了您的问题?如果没有,请发表评论,我会尽力提供进一步帮助。