JSON批量导入到Elasticstearch [英] JSON Bulk import to Elasticstearch

查看:123
本文介绍了JSON批量导入到Elasticstearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Elasticsearch批量导入。

Elasticsearch Bulk import.

我需要将产品导入为单独的项目。

I need to import the Products as individual items.

我有一个json文件看起来类似于以下内容:

I have a json file that looks similar to the following:

{
   "Products":[
      {
         "Title":"Product 1",
         "Description":"Product 1 Description",
         "Size":"Small",
         "Location":[
            {
               "url":"website.com",
               "price":"9.99",
               "anchor":"Prodcut 1"
            }
         ],
         "Images":[
            {
               "url":"product1.jpg"
            }
         ],
         "Slug":"prodcut1"
      },
      {
         "Title":"Product 2",
         "Description":"Prodcut 2 Desctiption",
         "Size":"large",
         "Location":[
            {
               "url":"website2.com",
               "price":"99.94",
               "anchor":"Product 2"
            },
            {
               "url":"website3.com",
               "price":"79.95",
               "anchor":"discount product 2"
            }
         ],
         "Images":[
            {
               "url":"image.jpg"
            },
            {
               "url":"image2.jpg"
            }
         ],
         "Slug":"product2"
      }
   ]
}

我尝试了以下(我是新手) :

I've tried the following (I'm a novice at this):

curl -s -XPOST 'http://localhost:9200/_bulk' --data-binary @products.json
curl -s -XPOST 'http://localhost:9200/_bulk' -d @products.json
curl -XPOST http://localhost:9200/cp/products -d "@products.json"
curl -XPOST http://localhost:9200/products -d "@products.json"

有些人给了他人没有的错误。我需要做什么?

Some gave an error others didn't. What do I need to do?

推荐答案

遵循批量API文档。您需要提供非常具体格式的文件的批量操作:

Following the Bulk API documentation. You need to supply the bulk operation with a file formatted very specifically:


注意:最后一行数据必须以换行符\\ \\ n。

NOTE: the final line of data must end with a newline character \n.

可能的操作是索引,创建,删除和更新。索引和创建期望下一行的源,并且具有与标准索引API的op_type参数相同的语义(即,如果具有相同索引和类型的文档已经存在,则create将失败,而索引将添加或替换文档有必要的)。删除并不期望下列行的源,并且具有与标准delete API相同的语义。更新期望在下一行指定部分文档,upsert和脚本及其选项。

The possible actions are index, create, delete and update. index and create expect a source on the next line, and have the same semantics as the op_type parameter to the standard index API (i.e. create will fail if a document with the same index and type exists already, whereas index will add or replace a document as necessary). delete does not expect a source on the following line, and has the same semantics as the standard delete API. update expects that the partial doc, upsert and script and its options are specified on the next line.

如果要提供文本文件输入以卷曲,则必须使用--data-binary标志,而不是plain -d。后者不保留换行符。

If you’re providing text file input to curl, you must use the --data-binary flag instead of plain -d. The latter doesn’t preserve newlines.

所以您需要将products.json文件的内容更改为以下内容: / p>

So you will need to change the contents of your products.json file to the following:

 {"index":{"_index":"cp", "_type":"products", "_id": "1"}}
 { "Title":"Product 1", "Description":"Product 1 Description", "Size":"Small", "Location":[{"url":"website.com", "price":"9.99", "anchor":"Prodcut 1"}],"Images":[{ "url":"product1.jpg"}],"Slug":"prodcut1"}
 {"index":{"_index":"cp", "_type":"products", "_id":"2"}}
 {"Title":"Product 2", "Description":"Prodcut 2 Desctiption", "Size":"large","Location":[{"url":"website2.com", "price":"99.94","anchor":"Product 2"},{"url":"website3.com","price":"79.95","anchor":"discount product 2"}],"Images":[{"url":"image.jpg"},{"url":"image2.jpg"}],"Slug":"product2"}

在你的curl命令中使用 - data-binary (像你的第一个命令)。还要注意,如果使用索引和类型特定的端点,可以省略索引类型。你的第三卷曲命令是 / cp / products

And be sure to use --data-binary in your curl command (like your first command). Also note the index and type can be omitted if you use the index and type specific endpoint. Yours is /cp/products like your 3rd curl command.

这篇关于JSON批量导入到Elasticstearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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