从ElasticSearch中的数组中删除元素/对象,然后进行匹配查询 [英] Remove elements/objects From Array in ElasticSearch Followed by Matching Query

查看:428
本文介绍了从ElasticSearch中的数组中删除元素/对象,然后进行匹配查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类型的文件.

{
"_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
    ]
  ]
  }
} 

下面是映射:

   {
        "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 ]
    }}
    }

但是无法从位置删除数组对象.我曾尝试下面的查询,但工作. POST example1/doc/3/_update {

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]
    }
    }
    }

请让我知道我在哪里做错了.我正在使用弹性版本5.5.2

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

推荐答案

add()不同,remove()采用元素的索引并将其删除.

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

您的ctx._source.locations轻松地是ArrayList.它具有List remove() 方法:

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

E remove(int index)

E remove(int index)

删除此列表中指定位置的元素(可选操作). ...

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

请参见

See Painless API - List for other methods.

例如参见此答案代码.

这篇关于从ElasticSearch中的数组中删除元素/对象,然后进行匹配查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆