【问题标题】:ElasticSearch Index/Insert with NEST failElasticSearch 索引/插入 NEST 失败
【发布时间】:2016-06-17 13:17:43
【问题描述】:

我正在尝试将一些 JSON 数据插入到弹性搜索中进行测试。

代码如下:

    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 收到以下错误: {通过对 PUT 的不成功的低级别调用构建的无效 NEST 响应:/FormId/resp/1?refresh=true}

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

以上错误信息来自 NEST。 Elastic 回复(并在日志中写入)以下消息: MapperParsingException[解析失败]; nested- NotXContentException[压缩器检测只能在某些xcontent字节或压缩xcontent字节上调用];

解析失败 { ""hello"" : ""world"" } ????

【问题讨论】:

    标签: c# json elasticsearch kibana nest


    【解决方案1】:

    一些观察:

    • 索引名称需要小写
    • 索引将在您将文档编入索引时自动创建,尽管此功能可以在配置中关闭。如果您还想控制文档的映射,最好先创建索引。
    • 使用匿名类型来表示您希望发送的 json(如果您愿意,可以使用 低级客户端client.LowLevel 发送 json 字符串,但使用匿名类型可能是更容易)。
    • 响应中的.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"
       }
    }
    

    【讨论】:

    • 非常感谢您的解决方案。我使用 Newtonsoft 反序列化 json var stuff = JsonConvert.DeserializeObject(json);
    • 如果我们已经将它定义为默认索引,您能否告诉我为什么要检查索引是否存在(formid)?
    • formid 是默认索引的名称,如果没有指定或可以推断调用的索引,但这不会检查名称传递给默认索引的索引是否存在.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2014-11-12
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多