Reputation: 45
I want to compare two fields and check if they are the same. If they are found to be equal,I need to delete one of the fields from the document. How can I achieve this in elasticsearch by using scripts?
Upvotes: 2
Views: 503
Reputation: 217424
Using the update-by-query plugin you can achieve this easily. That plugin allows one to pass a query to match documents and a script to modify the matching documents. So in your case, you can use it like this:
curl -XPOST localhost:9200/your_index/your_type/_update_by_query -d '
{
"query": {
"filtered": {
"filter": {
"script": {
"script": "doc.field1.value == doc.field2.value"
}
}
}
},
"script": "ctx._source.remove(\"field2\");"
}'
Upvotes: 4
Reputation: 19273
You can make use of scripts and update by script for doing this. It can be done by making use of filters,by first comparing the fields and then removing any of the desired fields by using "remove" method like below:
{
"query": {
"filtered": {
"filter": {
"script": {
"script": "doc[\"fieldA\"].value == doc[\"fieldB\"].value"
}
}
}
},
"script": "ctx._source.remove(\"fieldA\");"
}
Upvotes: 2