Home > Net >  How remove an element in all documents of an index in elasticsearch?
How remove an element in all documents of an index in elasticsearch?

Time:04-14

I have list of documents in an index of Elasticsearch like below:

...
{
  "_index" : "index-name",
  "_type" : "_doc",
  "_id" : "table1c7151240c583e60c8e2cbad351",
  "_score" : 0.28322574,
  "_source" : {
    ...
    "table.tag" : {
      "datasources_keys" : [...],
      "tags" : [
        "6e7358e2bfc84c34af32a01f6d19e9b2",
        "ab450ae5c1734fb0aad5fed052b42023",
        "f725e3100bba4b5eb8a5199a2b3e62fc"
      ]
    }
  }
},
...

I wanna delete an element in all documents.. for example should remove a specified tag_id in tags like "6e7358e2bfc84c34af32a01f6d19e9b2" .. how should I write a script for that? Is there other way in elasticsearch?

I'm using this script.. but it doesnt work!!

POST index-name/_update_by_query
{
  "query": {
    "match":{
      "index.tag.tags": "6e7358e2bfc84c34af32a01f6d19e9b2"
    }
  },
  "script": {
    "source": "ctx._source['table.tag']['tags'] -= 6e7358e2bfc84c34af32a01f6d19e9b2",
    "lang": "painless"
  }
}

CodePudding user response:

You can try below script:

POST index-name/_update_by_query
{
  "query": {
    "match": {
      "table.tag.tags": "6e7358e2bfc84c34af32a01f6d19e9b2"
    }
  },
 "script": {
    "source": """
    for (int i = 0; i < ctx._source['table.tag']['tags'].length; i  )
    {
      if(ctx._source['table.tag']['tags'][i]=='6e7358e2bfc84c34af32a01f6d19e9b2')
      {
            ctx._source['table.tag']['tags'].remove(i);
      }
    }"""
      }
}

CodePudding user response:

Here is a more concise way with implicit list iterations and if conditions ( it's a one-liner

  • Related