gpt4 book ai didi

c# - 使用 NEST 的 ElasticSearch 索引/插入失败

转载 作者:行者123 更新时间:2023-11-30 13:36:19 26 4
gpt4 key购买 nike

我正在尝试将一些 JSON 数据插入 Elasticsearch 以进行测试。

代码如下:

    var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DefaultIndex("FormId");

var client = new ElasticClient(settings);

var myJson = @"{ ""hello"" : ""world"" }";
var response = client.Index(myJson, i => i.Index("FormId")
.Type("resp")
.Id((int)r.id)
.Refresh()
);

没有插入任何内容,我从 ES 收到以下错误:{无效的 NEST 响应由 PUT 上不成功的低级别调用构建:/FormId/resp/1?refresh=true}

我试图找到一些例子,但都使用预定义的数据结构,而不是我想使用 JSON 数据和非结构化数据。

以上错误消息来自 NEST。Elastic 回复(并写入日志)以下消息:MapperParsingException[解析失败]; nested- NotXContentException[Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes];

无法解析 { ""hello"": ""world""} ????

最佳答案

一些观察:

  • 索引名称需要小写
  • 索引将在您将文档索引到其中时自动创建,尽管可以在配置中关闭此功能。如果您还想控制文档的映射,最好先创建索引。
  • 使用匿名类型来表示您希望发送的 json(您可以使用低级客户端发送 json 字符串,即 client.LowLevel 如果您愿意,但使用匿名类型可能更容易)。
  • 响应中的 .DebugInformation 应该包含请求失败原因的所有详细信息

下面是一个演示如何开始的例子

void Main()
{
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node)
// lower case index name
.DefaultIndex("formid");

var client = new ElasticClient(settings);

// use an anonymous type
var myJson = new { hello = "world" };

// create the index if it doesn't exist
if (!client.IndexExists("formid").Exists)
{
client.CreateIndex("formid");
}

var indexResponse = client.Index(myJson, i => i
.Index("formid")
.Type("resp")
.Id(1)
.Refresh()
);
}

现在,如果我们向 http://localhost:9200/formid/resp/1 发出 GET 请求,我们会取回文档

{
"_index": "formid",
"_type": "resp",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"hello": "world"
}
}

关于c# - 使用 NEST 的 ElasticSearch 索引/插入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35785305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com