将具有动态映射值的字段添加到所有文档中

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

有一个索引shakespeare,其文档如下所示:

{
        "_index" : "shakespeare",
        "_type" : "line",
        "_id" : "24",
        "_score" : 1.0,
        "_source" : {
          "play_name" : "Henry IV",
          "speech_number" : 1,
          "line_number" : "1.1.22",
          "text_entry" : "Forthwith a power of English shall we levy;",
          "speaker" : "KING HENRY IV",
          "line_id" : 1
        }
      },

我有一个文件/表/等,如下所示

+---------+--------------+
| line_id | is_a_good_line |
+---------+--------------+
|       1 |            1 |
|       2 |            1 |
|       3 |            0 |
|       4 |            0 |
|       5 |            1 |
+---------+--------------+

我希望shakespeare索引中的每个文档都有一个新字段is_a_good_line,其值与表中的映射相匹配(基于line_id)。因此,对于上面的示例,is_a_good_line的值应为1,如下所示:

{
        "_index" : "shakespeare",
        "_type" : "line",
        "_id" : "24",
        "_score" : 1.0,
        "_source" : {
          "play_name" : "Henry IV",
          "speech_number" : 1,
          "line_number" : "1.1.22",
          "text_entry" : "Forthwith a power of English shall we levy;",
          "speaker" : "KING HENRY IV",
          "is_a_good_line" : 1,
          "line_id" : 1
        }
      },

[我找到了一种向所有具有静态值的文档中简单添加新字段的方法:

POST shakespeare/_update_by_query
{
  "script": {
    "inline": "ctx._source.is_a_good_line = 0",
    "lang": "painless"
  }
}

如何添加具有由上面的映射表定义的值(基于line_id值)的新字段?

elasticsearch
1个回答
0
投票

有多种选择,从编写一个简单的程序来分析文件/表并相应地更新相关文档,到定义一个Logstash管道,通过使用适当的输入/输出插件来完成相同的工作。当然,您不能仅依靠Elasticsearch来解析外部文件。

如果要使用程序选项,可以像bash脚本一样简单,它可以解析文件并使用类似的查询来卷曲_update_by_query

{
  "query": {
    "match": { "line_id": <the_line_id_fetched_from_the_file>
  },
  "script": {
    "inline": "ctx._source.is_a_good_line = <the_is_a_good_line_value_from_the_file>",
    "lang": "painless"
  }
}

或者您可以使用许多官方的Elasticsearch客户端库之一以您喜欢的语言开发相同的程序:https://www.elastic.co/guide/en/elasticsearch/client/index.html

[相反,如果您想使用Logstash管道选项,则可能需要查看file input pluginelasticsearch output plugin(尤其是script选项),也许还可以查看csv filter plugin(取决于文件/表中数据的结构方式。]

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