代码之家  ›  专栏  ›  技术社区  ›  nehacharya

如何在elasticsearch中指定要在POST请求正文中索引的2个或更多文档?

  •  0
  • nehacharya  · 技术社区  · 7 年前

    http://localhost:9200/amenities/test/_bulk .

    在这里,内容类型被设置为application/json以及它在执行请求时给出的错误。

    enter image description here

    这里,它显示了JSON中的一个语法错误。

    enter image description here

    我无法理解如何索引多个JSON对象(文档)。当我只指定了一个文档时,它工作得很好,但是当我指定两个或更多文档时,JSON就变得无效了。

    我尝试了以下解决方案:

    {“索引”:{}} {“类型”:“厨房”,“位置”:{“x”:9881.034723869176,“y”:-12942.49413158995},“图标”:“餐具”,“类别”:“便利设施”} {“type”:“垃圾桶”,“location”:{“x”:9170.444649524274,“y”:-12855.890257805067},“icon”:“垃圾”,“category”:“便利设施”}

    {“索引”:{“\u索引”:“便利设施”,“\u类型”:“测试”}} {“类型”:“厨房”,“位置”:{“x”:9881.034723869176,“y”:-12942.49413158995},“图标”:“餐具”,“类别”:“便利设施”} {“索引”:{“\u索引”:“便利设施”,“\u类型”:“测试”}}

    但还是有语法错误。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Tarek Essam    7 年前

    您可以将文档以以下格式(testData.json)放入json文件中:

    {"index": {"_index": "animals", "_type": "_doc", "_id": 1}}
    {"name": "dog"}
    {"index": {"_index": "animals", "_type": "_doc", "_id": 2}}
    {"name": "cat"}
    

    像这样使用卷曲:

    curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@testData.json";
    

    或者,如果要使用不带json文件的curl:

     curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
    { "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
    { "field1" : "value1" }
    { "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
    { "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
    { "field1" : "value3" }
    { "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
    { "doc" : {"field2" : "value2"} }'
    

    _bulk

        2
  •  2
  •   Val    7 年前

    如上所述,使用Postman发送批量查询是完全可能的,只需正确格式化并以原始文本发送即可,如下所示:

    enter image description here

    还要注意,Headers部分包含一个HTTP头:

    ContentType: application/x-ndjson