Remove elements/objects From Array in ElasticSearch Followed by Matching Query

感情迁移 提交于 2020-06-28 09:45:21

问题


I have below types documents.

{
"_index": "example1",
"_type": "doc",
"_id": "8036",
"_score": 1,
"_source": {
  "user": "kimchy8036",
  "postDate": "2009-11-15T13:12:00",
  "locations": [
    [
      72.79887719999999,
      21.193036000000003
    ],
    [
      -1.8262150000000001,
      51.178881999999994
    ]
  ]
  }
} 

And below is mapping:

   {
        "example1": {
        "mappings": {
        "doc": {
        "properties": {
        "locations": {
        "type": "geo_point"
        },
        "postDate": {
        "type": "date"
        },
        "status": {
        "type": "long"
        },
        "user": {
        "type": "text",
        "fields": {
        "keyword": {
        "type": "keyword",
        "ignore_above": 256
        }
        }
        }
        }
        }
        }
        }
            }

I can add multiple locations using below query.

POST /example1/_update_by_query
    {
    "query": {
    "match": {
    "_id": "3"
    }
    },
    "script" : {
    "lang":"painless",
    "inline": "ctx._source.locations.add(params.newsupp)",
    "params":{
    "newsupp":[-74.00,41.12121 ]
    }}
    }

But not able remove array objects from locations. I have tried below query but working. POST example1/doc/3/_update {

    "script" :
    {
    "lang":"painless",
    "inline":"ctx._source.locations.remove(params.tag)",
    "params":{
    "tag":[-74.00,41.12121]
    }
    }
    }

Kindly let me know where i am doing wrong here. I am using elastic version 5.5.2


回答1:


Unlike add(), remove() takes the index of the element and remove it.

Your ctx._source.locations in painless is an ArrayList. It has List's remove() method:

E remove(int index)

Removes the element at the specified position in this list (optional operation). ...

See Painless API - List for other methods.

See this answer for example code.



来源:https://stackoverflow.com/questions/49384199/remove-elements-objects-from-array-in-elasticsearch-followed-by-matching-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!