Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove arraylist value in elastic search using curl?

How to remove arraylist values in Elasticsearch using sense console or curl?

i want to remove any array element.?

POST /q/q/
{
    "a": [
    "z", "q", "1"
    ]
}

it doesnt work for me:

POST /q/q/AV4sjk40mWHLgYFNkmNd/_update
{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.a -=newsupp",
        "params": {
            "newsupp": "p" 
        }
     }
}

or

POST /q/q/AV4sjk40mWHLgYFNkmNd/_update
{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.a.remove("1")"
    }
}
like image 622
bala s Avatar asked Aug 31 '17 11:08

bala s


1 Answers

If you want to remove all occurrences in the list, you can do this:

{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.a.removeAll(Collections.singleton('1'))"
  }
}

or if you want to remove just the first, like this:

{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.a.remove(ctx._source.a.indexOf('1'))"
  }
}

Also note that if you want to use double-quotes, it's fine, but you need to escape them, like ctx._source.a.indexOf(\"1\")).

Or with params:

{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.a.remove(ctx._source.a.indexOf(yourParamName))",
    "params": {
      "yourParamName": "1"
    }
  }
}
like image 192
dshockley Avatar answered Nov 19 '22 12:11

dshockley