使用 NEST 索引大型 JSON

Indexing a large JSON using NEST

我正在尝试将大型 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);

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