JSON 文档索引失败

标签 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

{"error":"RemoteTransportException[[Klaatu][inet[/192.168.1.127:9300]][indices:data/write/index]]; nested: MapperParsingException[failed to parse, document is empty]; ","status":400}

我也试过这样----

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

{"error":"ElasticsearchParseException[Failed to derive xcontent]","status":400}

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

最佳答案

首先确保你的 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

关于JSON 文档索引失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32311365/

相关文章:

javascript - 无法在 Angular.js 中使用检索到的 JSON 对象

ElasticSearch:聚合一组收集的结果

elasticsearch - 如何从Kafka Connect在Elastic Search中创建多个索引

amazon-web-services - 在 AWS elasticsearch 中索引 pdf 文件

javascript - 将 json 对象中每个深度级别的节点存储在单独的数组中

java - JAX-RS,Map<String,String> 到 JSON 没有开销?

elasticsearch - 查找空对象的Elasticsearch值

elasticsearch - elasticsearch:文档TTL <indexs.ttl.interval

mysql - 使用 Mule ESB 在 mysql 数据库中插入 json 对象

android - 类型不匹配 : inferred type is String but Charset was expected in kotlin