微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 NEST 索引大型 JSON

如何解决使用 NEST 索引大型 JSON

我正在尝试将一个大型 JSON(大约 22k 行)文件添加到我的 ElasticSearch 索引,但我无法这样做,因为无论我尝试什么,我都收到来自 ElasticSearch 的错误响应。我使用的是 ElasticSearch 7.6.2,我使用 nesT 与 ElasticSearch 通信。

我只会添加与问题相关的代码

首先我创建这样的连接设置:

_settings = new ConnectionSettings()
                               .DefaultMappingFor<string>(m => m  // I've put string here because that is the return type of File.ReadAllText() function
                               .IndexName("jsonindex")
                               );

然后我创建我的 Elasticclient:

_elasticclient = new Elasticclient(_settings);

我已经成功地创建了我的索引:

_elasticclient.Indices.Create("jsonindex",c => c
                       .Settings(s => s
                           .NumberOfShards(1)));

一切设置完毕后,我会执行以下操作:

var json = File.ReadAllText("path\to\file.json"); //read the json file 
var indexResponse = _elasticclient.IndexDocument(jsonToSend); //try to index it,but this is where I get the error message

我收到以下错误

Request Failed to execute. Call: Status code 400 from: POST /jsonindex/_doc. ServerError: Type: mapper_parsing_exception Reason: "Failed to parse" CausedBy: "Type: not_x_content_exception Reason: "Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes""

我也尝试过调用 .Index() 方法并将索引名称指定为第二个参数,但结果是一样的。

解决方法

看起来我在使用高级客户端时没有朝着正确的方向前进,我像这样访问了低级客户端:

_elasticClient.LowLevel.Index<StringResponse>("jsonindex",json);

它在第一次尝试时就成功了,它可能会帮助像我这样使用高级客户端的人。 (注意:我假设有一种方法可以使用高级客户端来实现我想要做的事情,我只是还没有弄清楚)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?