Odd Hogstad
Odd Hogstad

Reputation: 119

Deleting field in elasticsearch

In my elasticsearch, I need to remove a field that has a structure that looks something like this:

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

I want to remove the secondEntry from the record, how can that be done?

I've tried using the update api,passing a script, but this does not seem to work:

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

Thanks!

Upvotes: 0

Views: 2143

Answers (1)

Sloan Ahrens
Sloan Ahrens

Reputation: 8718

Since the document you posted doesn't appear to be legal, I'm assuming you meant this:

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

So if I create an index and post that document,

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"
         }
      }
   }
}

Then update the document with the new version, I get back the new version:

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"
         }
      }
   }
}

Here is the code I used:

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

Does this solve your problem? If not, leave a comment and I'll try to help further.

Upvotes: 1

Related Questions