Elasticsearch:Curl不起作用

时间:2016-02-03 15:58:16

标签: curl elasticsearch elasticsearch-2.0

我有ES 2.2.0,我正在尝试

curl -XPOST "http://localhost:9200" -d @jnk.json

但是我得到了

Warning: Couldn't read data from file "jnk.json", this makes an empty POST.
No handler found for uri [/] and method [POST]

这里是jnk.json文件的内容

PUT junktest 
{
  "mappings": {
    "test": {"properties": {
        "DocumentID": {
          "type": "string"
        },
        "Tags":{
            "type" : "string",
          "index" : "not_analyzed"
        },
        "Summary": {
          "type": "string",
          "index" : "not_analyzed"
        },
        "Status": {
          "type": "string",
          "index" : "not_analyzed"
        },
        "Location": {
          "type": "string",
          "index" : "not_analyzed"
        },
        "Error": {
          "type": "string",
          "index" : "not_analyzed"
        },
        "Author": {
          "type": "string",
          "index" : "not_analyzed"
        },
        "Sector": {
          "type": "string",
          "index" : "not_analyzed"
        }
        "Created Date": {
          "type":   "date",
          "format": "yyyy-MM-dd"
        }
      }
    }
  }
}

POST /junktest/test/
{

             "DocumentID":"555661",
             "Tags":["A","B","C","D"],
             "Summary":"Summary Text",
             "Status":"Review",
             "Location":"HDFS",
             "Error":"None",
             "Author":"Poi KLj",
             "Sector":"Energy",
             "Created Date":"2013-04-23"
}

POST /junktest/test/
{

             "DocumentID":"555662",
             "Tags":["B","C","D"],
             "Summary":"Summary Text",
             "Status":"Review",
             "Location":"HDFS",
             "Error":"None",
             "Author":"Abc Mnb",
             "Sector":"Energy",
             "Created Date":"2013-05-23"
}

所以我正在创建一个映射,然后发布一个文档。我做错了什么?

我得到-XPUT

的相同结果

修改

非常感谢@Bahaaldine Azarmi!有缺少的逗号,我能够单独创建映射:)但我尝试了批量命令

curl -XPOST "http://localhost:9200/_bulk" --data-binary @post.json

根据api,它给了我错误

    {"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unexpected char
acter (':' (code 58)): expected a valid value (number, String, array, object, 't
rue', 'false' or 'null')\n at [Source: [B@2f1a62ab; line: 1, column: 27]"}],"typ
e":"json_parse_exception","reason":"Unexpected character (':' (code 58)): expect
ed a valid value (number, String, array, object, 'true', 'false' or 'null')\n at
 [Source: [B@2f1a62ab; line: 1, column: 27]"},"status":500}

这是我的post.json

{ "index" : { "_index" : "junktest", "_type" : "test"} }
{

             "DocumentID":"555661",
             "Tags":["A","B","C","D"],
             "Summary":"Summary Text",
             "Status":"Review",
             "Location":"HDFS",
             "Error":"None",
             "Author":"Poi KLj",
             "Sector":"Energy",
             "Created Date":"2013-04-23"
}

我的语法有问题吗?哪个:字符不合适?

固定

批量api中不允许换行符,因为这些换行符被视为分隔符。所以正确的文件格式是

{"index":{"_index":"junktest","_type":"test"}}
{"DocumentID":"555661","Tags":["A","B","C","D"],"Summary":"Summary Text","Status":"Review","Location":"HDFS","Error":"None","Author":"Poi KLj","Sector":"Energy","Created Date":"2013-04-23"}

输入文件必须以换行符结束

1 个答案:

答案 0 :(得分:3)

这些查询语法需要在Sense(https://www.elastic.co/blog/found-sense-a-cool-json-aware-interface-to-elasticsearch)中复制和粘贴。使用Sense,您将能够按顺序运行每个查询。

如果您想使用curl,请将工作拆分为两个:

使用以下内容创建映射

curl -XPUT "http://localhost:9200/junktest" -d @mapping.json

顺便说一句,你的映射在这里缺少逗号

},
"Created Date": {

然后进行第二次调用,使用批量API在单个查询中索引您的Json对象,例如:

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html