验证失败:1:批量索引ElasticSearch中未添加任何请求

ric*_*ick 16 json elasticsearch elasticsearch-bulk-api

我有一个JSON文件,我需要在ElasticSearch服务器上对其进行索引.

JSOIN文件如下所示:

{
    "sku": "1",
    "vbid": "1",
    "created": "Sun, 05 Oct 2014 03:35:58 +0000",
    "updated": "Sun, 06 Mar 2016 12:44:48 +0000",
    "type": "Single",
    "downloadable-duration": "perpetual",
    "online-duration": "365 days",
    "book-format": "ePub",
    "build-status": "In Inventory",
    "description": "On 7 August 1914, a week before the Battle of Tannenburg and two weeks before the Battle of the Marne, the French army attacked the Germans at Mulhouse in Alsace. Their objective was to recapture territory which had been lost after the Franco-Prussian War of 1870-71, which made it a matter of pride for the French. However, after initial success in capturing Mulhouse, the Germans were able to reinforce more quickly, and drove them back within three days. After forty-three years of peace, this was the first test of strength between France and Germany. In 1929 Karl Deuringer wrote the official history of the battle for the Bavarian Army, an immensely detailed work of 890 pages; First World War expert and former army officer Terence Zuber has translated this study and edited it down to more accessible length, to produce the first account in English of the first major battle of the First World War.",
    "publication-date": "07/2014",
    "author": "Deuringer, Karl",
    "title": "The First Battle of the First World War: Alsace-Lorraine",
    "sort-title": "First Battle of the First World War: Alsace-Lorraine",
    "edition": "0",
    "sampleable": "false",
    "page-count": "0",
    "print-drm-text": "This title will only allow printing of 2 consecutive pages at a time.",
    "copy-drm-text": "This title will only allow copying of 2 consecutive pages at a time.",
    "kind": "book",
    "fro": "false",
    "distributable": "true",
    "subjects": {
      "subject": [
        {
          "-schema": "bisac",
          "-code": "HIS027090",
          "#text": "World War I"
        },
        {
          "-schema": "coursesmart",
          "-code": "cs.soc_sci.hist.milit_hist",
          "#text": "Social Sciences -> History -> Military History"
        }
      ]
    },   
   "pricelist": {
      "publisher-list-price": "0.0",
      "digital-list-price": "7.28"
    },
    "publisher": {
      "publisher-name": "The History Press",
      "imprint-name": "The History Press Ireland"
    },
    "aliases": {
      "eisbn-canonical": "1",
      "isbn-canonical": "1",
      "print-isbn-canonical": "9780752460864",
      "isbn13": "1",
      "isbn10": "0750951796",
      "additional-isbns": {
        "isbn": [
          {
            "-type": "print-isbn-10",
            "#text": "0752460862"
          },
          {
            "-type": "print-isbn-13",
            "#text": "97807524608"
          }
        ]
      }
    },
    "owner": {
      "company": {
        "id": "1893",
        "name": "The History Press"
      }
    },
    "distributor": {
      "company": {
        "id": "3658",
        "name": "asc"
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用命令索引此JSON文件时

curl -XPOST 'http://localhost:9200/_bulk' -d @1.json
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

{"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation Failed: 1: no requests added;"}],"type":"action_request_validation_exception","reason":"Validation Failed: 1: no requests added;"},"status":400}
Run Code Online (Sandbox Code Playgroud)

我不知道我在哪里弄错了.

dav*_*ide 30

Elasticsearch的批量API使用特殊语法,该语法实际上由json单行写入的文档组成.看一下文档.

语法非常简单.对于索引,创建和更新,您需要2个单行json文档.第一行告诉操作,第二行给文档索引/创建/更新.要删除文档,只需要操作行.例如(来自文档):

{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }   
{ "doc" : {"field2" : "value2"} }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
Run Code Online (Sandbox Code Playgroud)

不要忘记用新行结束文件. 然后,要调用批量api,请使用以下命令:

curl -s -XPOST localhost:9200/_bulk --data-binary "@requests"
Run Code Online (Sandbox Code Playgroud)

从文档:

如果要为curl提供文本文件输入,则必须使用--data-binary标志而不是plain-d

  • **不要忘记用新的线路结束你的文件**..早上3点在笔记本电脑上发誓,你刚刚救了我的生命哈哈... (7认同)
  • “不要忘记以新行结束文件。” 谢谢你!这里为我节省了一个小时。 (2认同)