我正在try 将一个JSON文件批量索引到一个新的Elasticsearch索引中,但无法这样做.我在JSON中有以下示例数据

[{"Amount": "480", "Quantity": "2", "Id": "975463711", "Client_Store_sk": "1109"},
{"Amount": "2105", "Quantity": "2", "Id": "975463943", "Client_Store_sk": "1109"},
{"Amount": "2107", "Quantity": "3", "Id": "974920111", "Client_Store_sk": "1109"},
{"Amount": "2115", "Quantity": "2", "Id": "975463798", "Client_Store_sk": "1109"},
{"Amount": "2116", "Quantity": "1", "Id": "975463827", "Client_Store_sk": "1109"},
{"Amount": "648", "Quantity": "3", "Id": "975464139", "Client_Store_sk": "1109"},
{"Amount": "2126", "Quantity": "2", "Id": "975464805", "Client_Store_sk": "1109"},
{"Amount": "2133", "Quantity": "1", "Id": "975464061", "Client_Store_sk": "1109"},
{"Amount": "1339", "Quantity": "4", "Id": "974919458", "Client_Store_sk": "1109"},
{"Amount": "1196", "Quantity": "5", "Id": "974920538", "Client_Store_sk": "1109"},
{"Amount": "1198", "Quantity": "4", "Id": "975463638", "Client_Store_sk": "1109"},
{"Amount": "1345", "Quantity": "4", "Id": "974919522", "Client_Store_sk": "1109"},
{"Amount": "1347", "Quantity": "2", "Id": "974919563", "Client_Store_sk": "1109"},
{"Amount": "673", "Quantity": "2", "Id": "975464359", "Client_Store_sk": "1109"},
{"Amount": "2153", "Quantity": "1", "Id": "975464511", "Client_Store_sk": "1109"},
{"Amount": "3896", "Quantity": "4", "Id": "977289342", "Client_Store_sk": "1109"},
{"Amount": "3897", "Quantity": "4", "Id": "974920602", "Client_Store_sk": "1109"}]

我在用

 curl -XPOST localhost:9200/index_local/my_doc_type/_bulk --data-binary --data @/home/data1.json 

当我试图使用Elasticsearch的标准批量索引API时,我遇到了这个错误

 error: {"message":"ActionRequestValidationException[Validation Failed: 1: no requests added;]"}

Can anyone help with indexing this type of JSON?

推荐答案

What you need to do is to read that JSON file and then build a bulk request with the format expected by the _bulk endpoint, i.e. one line for the command and one line for the document, separated by a newline character... rinse and repeat for each document:

curl -XPOST localhost:9200/your_index/_bulk -d '
{"index": {"_index": "your_index", "_type": "your_type", "_id": "975463711"}}
{"Amount": "480", "Quantity": "2", "Id": "975463711", "Client_Store_sk": "1109"}
{"index": {"_index": "your_index", "_type": "your_type", "_id": "975463943"}}
{"Amount": "2105", "Quantity": "2", "Id": "975463943", "Client_Store_sk": "1109"}
... etc for all your documents
'

Just make sure to replace your_index and your_type with the actual index and type names you're using.

UPDATE

请注意,如果您的URL中指定了_index_type,则可以通过删除它们来缩短命令行.如果您在映射中指定了path to your id field,也可以删除_id(请注意,该功能在ES2.0中将被弃用).对于所有文档,您的命令行至少可以看起来像{"index":{}},但是为了指定您想要执行的操作类型(在本例中是文档index),命令行始终是强制的

UPDATE 2

curl -XPOST localhost:9200/index_local/my_doc_type/_bulk --data-binary  @/home/data1.json

/home/data1.json should look like this:

{"index":{}}
{"Amount": "480", "Quantity": "2", "Id": "975463711", "Client_Store_sk": "1109"}
{"index":{}}
{"Amount": "2105", "Quantity": "2", "Id": "975463943", "Client_Store_sk": "1109"}
{"index":{}}
{"Amount": "2107", "Quantity": "3", "Id": "974920111", "Client_Store_sk": "1109"}

UPDATE 3

You can refer to this answer to see how to generate the new json style file mentioned in UPDATE 2.

UPDATE 4

从ES 7.x开始,doc_type不再是必需的,应该是_doc而不是my_doc_type.从ES 8.x开始,文档类型将被完全删除.你可以阅读更多关于这个here

Json相关问答推荐

为什么terraform不缩小这个策略JSON?'

Jolt将键和值转换为单独的数组集

Vega图表计数聚合如果数据值为空数组则不显示任何内容,如何解决此问题?

德纳比可视化区域图表 Power BI

如何将属性拆分为嵌套的JSON内容?

错误解析错误:意外令牌:在我的 .eslintrc.json 文件中.为什么?

使用 serde 和 csv crates 将嵌套的 json 对象序列化为 csv

JOLT 在 struct 体中间添加一个 JSON 字段

SwiftUI:如何使用 0 索引数组键为 JSON 添加类型

如何在 onClick 事件处理程序中识别在同一 map 上绘制的多个多边形中的哪个(使用 react-leaflet)被单击?

如何在 Eclipse 中安装和使用 JSON 编辑器?

使用 @ResponseBody 自定义 HttpMessageConverter 来做 Json 事情

JSON 语法错误:'unexpected number' 或 'JSON.parse: expected ',' or '}' after property value in object'

在 Django 1.9 中,使用 JSONField(本机 postgres jsonb)的约定是什么?

Golang struct 的 XML 和 JSON 标签?

使用 Python 3 读取 JSON 文件

如何判断 JSON 响应元素是否为数组?

如何安装 json gem - 无法构建 gem 原生扩展(mac 10.10)

在android中使用GSON解析带有动态key和value的JSON

通过 JSON 发送 64 位值的公认方式是什么?