问题描述
我正在尝试将一个大型 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);
它在第一次尝试时就成功了,它可能会帮助像我这样使用高级客户端的人。 (注意:我假设有一种方法可以使用高级客户端来实现我想要做的事情,我只是还没有弄清楚)