JSON文档无法索引

时间:2015-08-31 12:32:34

标签: json elasticsearch

我有一个json文档,我想索引。

但是当我尝试索引文件时,我总是会收到错误。

我的json文档名为file.json ---

{
  "_index": "myIndex",
  "_type": "myType",
  "_id": "p1486f499782beb828d870f4a92831720e048514f",
  "_score": 14.609365,
  "_source": {
    "content": "When we hear the word summer we think of beaches, sun tans and tiny bikinis. What we ",
    "source": "hello.com",
    "type": "sports",
    "guid": "p1486f499782beb828d870f4a92831720e048514f",
    "language": "en"
  }
}

我试着索引json文件,就像这样---

curl -XPOST 'localhost:9200/myIndex/myType' -d @file.json
  

{"错误":" RemoteTransportException [[克拉图] [INET [/192.168.1.127:9300] [指数:数据/写/索引]];   嵌套:MapperParsingException [无法解析,文档为空];   ""状态" 400}

我也是这样试过----

curl -s -XPOST localhost:9200/_bulk --data-binary @file.json
  

{"错误":" ElasticsearchParseException [无法派生   xcontent]""状态" 400}

我如何索引我的文档,任何人都知道如何解决这个问题!

1 个答案:

答案 0 :(得分:2)

首先确保你的file.json只包含_source内容,而不是_index_type等。所以在file.json中你应该只有这个:

{
    "content": "When we hear the word summer we think of beaches, sun tans and tiny bikinis. What we ",
    "source": "hello.com",
    "type": "sports",
    "guid": "p1486f499782beb828d870f4a92831720e048514f",
    "language": "en"
}

然后你可以像这样索引

curl -XPOST 'localhost:9200/myIndex/myType/p1486f499782beb828d870f4a92831720e048514f' --data-binary @file.json

如果你想使用_bulk endpoint,那么你的file.json需要略有不同:

{"index":{"_index":"myIndex", "_type":"myType", "_id": "p1486f499782beb828d870f4a92831720e048514f"}}
{"content": "When we hear the word summer we think of beaches, sun tans and tiny bikinis. What we ", "source": "hello.com", "type": "sports", "guid": "p1486f499782beb828d870f4a92831720e048514f", "language": "en" }

注意:请务必使用换行符结束文件。换行符未出现在上面的示例中。

然后你可以像这样发送它

curl -XPOST 'localhost:9200/_bulk' --data-binary @file.json

所以最重要的是,当通过文件发送文档内容时,您需要使用--data-binary而不是-d