Elastica or ElasticSearch remove field from all documents

1nstinct picture 1nstinct · Jun 10, 2013 · Viewed 7.6k times · Source

I am wondering whether Elastica or ElasticSearch provides the ability to remove a single field from all documents in a given index. I have found the correct way to do this in the REST API, but when trying to use it, it gives a syntax error.

I wrote

curl -XPOST localhost:9200/products/product/O2bMZYRek5/_update -d '{
"script": "ctx._source.remove("Color")"
}'

which returned

{"error":"JsonParseException[Unexpected character ('C' (code 67)): was expecting comma to separate OBJECT entries\n at [Source: [B@fddc294; line: 2, column: 32]]","status":500}

Answer

watchzerg picture watchzerg · Sep 17, 2013

you can do this via http api:

curl -XPOST 'http://localhost:9200/goods_city_1/meirong/552899/_update' -d '{
    "script" : "ctx._source.remove(\"text\")"
}'  

or you can do this via java api:

StringBuilder sb = new StringBuilder();
for(String stringField : stringFields){
    sb.append("ctx._source.remove(\"").append(stringField).append("\");");
}
updateRequestBuilder.setScript(sb.toString());  

I'v tried this, it gonna work. (at 0.90.2 at least.)

If you need it works for all doc in a index, you should put all this update action (or every 5000 action) into one BulkRequest, then throw it to sever.
Or maybe "elasticsearch-reindex"(https://github.com/karussell/elasticsearch-reindex) could help you, if you use alias to approach you data.