使用弹性搜索进行批量索引 [英] Bulk indexing using elastic search
问题描述
到目前为止,我一直在逐个文档地将数据索引到弹性文档中,现在随着数据开始增加,它变得非常缓慢,而不是一种优化的方法。因此,我正在寻找批量插入的东西,并找到了弹性批量API。从他们官方网站上的文件中,我感到困惑。我使用的方法是通过将数据作为WebRequest传递并在弹性服务器中执行它们。因此,在创建批处理/批量插入请求时,API希望我们形成一个模板,例如
Till now i was indexing data to elastic document by document and now as the data started increasing it has become very slow and not an optimized approach. So i was searching for a bulk insert thing and found Elastic Bulk API. From the documents in their official site i got confused. The approach i am using is by passing the data as WebRequest and executing them in the elastic server. So while creating a batch/bulk insert request the API wants us to form a template like
localhost:9200/_bulk as URL and
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
索引ID为1且field1值为值1的文档。API还建议将数据发送为JSON(不太漂亮,用于维护非转义字符)。因此,要传递具有多个属性的多个文档,我该如何构造我的数据。
to index a document with id 1 and field1 values as value 1. Also the API suggests to send the data as JSON (unpretty, to maintain a non escaping character or so). So to pass multiple document with multiple properties how can i structure my data.
我在FF RestClient中这样尝试过,其中POST和标头为JSON,但是RestClient抛出一些错误,我知道它不是有效的JSON
I tried like this in FF RestClient , with POST and header as JSON , but RestClient is throwing some error and i know its not a valid JSON
{ "index" : { "_index" : "indexName", "_type" : "type1", "_id" : "111" },
{ "Name" : "CHRIS","Age" : "23" },"Gender" : "M"}
推荐答案
您的数据格式不正确:
- 您没有第一行后不需要逗号
- 您在第一行中缺少结尾的
}
- 第二行的中间还有一个
}
,您也需要将其删除。
- You don't need the comma after the first line
- You're missing a closing
}
on the first line - You have a closing
}
in the middle of your second line you need to remove it as well.
为大容量插入格式化数据的正确方法如下:
The correct way of formatting your data for a bulk insert look like this:
curl -XPOST localhost:9200/_bulk -d '
{ "index" : { "_index" : "indexName", "_type" : "type1", "_id" : "111" }}
{ "Name" : "CHRIS","Age" : "23" ,"Gender" : "M"}
'
这将起作用。
更新
使用邮递员Chrome看起来像这样。确保在第2行之后添加新行。
Using Postman on Chrome it looks like this. Make sure to add a new line after line 2:
这篇关于使用弹性搜索进行批量索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!