将 Elastic Search 字段转换为数组

问题描述 投票:0回答:3

在弹性搜索中,如果您的文档具有预先存在的数组

"movies": [
     "Back to the Future"
]

然后你更新它以添加更多这样的电影

{
  "script" : "ctx._source.movies += tag",
  "params" : {
    "tag" : "Pulp Fiction"
  }      
}

然后该值将添加到该字段中。这很好用……但是如果这个字段一开始不是一个 arry 而是看起来像这样呢

"movies": "Back to the Future"

如果运行相同的脚本,您将得到以下结果

"movies":"Back to the FuturePulpFiction"

所以我的问题是如何获取这个现有字段并将其“转换”为数组以告诉弹性搜索我想将其视为数组?

json elasticsearch
3个回答
2
投票

您可以改用此脚本。它检查

movies
是否是一个数组,如果不是,则创建一个

{
  "script" : "if (ctx._source.movies.getClass().isArray()) { ctx._source.movies += tag } else { ctx._source.movies = [ctx._source.movies, tag] }",
  "params" : {
    "tag" : "Pulp Fiction"
  }      
}

另一种更简短的方法是始终分配一个数组,然后使用 Groovy 的

Collection.flatten()
方法

“展平”它
{
  "script" : "ctx._source.movies = [ctx._source.movies, tag].flatten()",
  "params" : {
    "tag" : "Pulp Fiction"
  }      
}

2
投票

添加到 Val 的答案中,如果

ctx._source.movies
不存在,它将在结果列表中添加
null
。以下是我用来执行类似操作但不包含
null
的脚本。

{
  "script": "if (ctx._source.movie.getClass().isArray()) {ctx._source.event += tag} else if (ctx._source.movie) {ctx._source.movie = [ctx._source.movie, tag]} else {ctx._source.movie=[tag]}",
  "params" : { 
    "tag" : "Pulp Fiction" 
  }
}

0
投票

文档中的数组是

java.util.List

文件包含:

  1. 空字符串
  2. 空列表
  3. 单项清单
  4. 绳子
  5. 空值
PUT /add_item_to_array/_bulk
{"create":{"_id":1}}
{"movies": ""}
{"create":{"_id":2}}
{"movies": []}
{"create":{"_id":3}}
{"movies": ["Back to the Future"]}
{"create":{"_id":4}}
{"movies": "The Fall"}
{"create":{"_id":5}}
{"movies": null}

通过查询更新

POST /add_item_to_array/_update_by_query
{
    "script" : {
        "source": """
            deffieldValue = ctx._source['movies'];
            if (!(fieldValueinstanceof List)) {
                ctx['_source']['movies'] = [];
                if ((fieldValue != '') && (fieldValue != null)) {
                    ctx['_source']['movies'].add(fieldValue);
                }
            }
            ctx['_source']['movies'].add(params.tag);
        """,
        "params" : {
            "tag" : "Pulp Fiction"
        }
    }
}

查询观看文档内容

GET /add_item_to_array/_search?filter_path=hits.hits

回应

{
    "hits" : {
        "hits" : [
            {
                "_index" : "add_item_to_array",
                "_type" : "_doc",
                "_id" : "1",
                "_score" : 1.0,
                "_source" : {
                    "movies" : [
                        "Pulp Fiction"
                    ]
                }
            },
            {
                "_index" : "add_item_to_array",
                "_type" : "_doc",
                "_id" : "3",
                "_score" : 1.0,
                "_source" : {
                    "movies" : [
                        "Back to the Future",
                        "Pulp Fiction"
                    ]
                }
            },
            {
                "_index" : "add_item_to_array",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 1.0,
                "_source" : {
                    "movies" : [
                        "Pulp Fiction"
                    ]
                }
            },
            {
                "_index" : "add_item_to_array",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 1.0,
                "_source" : {
                    "movies" : [
                        "The Fall",
                        "Pulp Fiction"
                    ]
                }
            },
            {
                "_index" : "add_item_to_array",
                "_type" : "_doc",
                "_id" : "5",
                "_score" : 1.0,
                "_source" : {
                    "movies" : [
                        "Pulp Fiction"
                    ]
                }
            }
        ]
    }
}

您可以自行检查字段是否存在

© www.soinside.com 2019 - 2024. All rights reserved.