简体   繁体   English

使用NEST将文档自动映射到elasticsearch无法正常工作

[英]Automapping document into elasticsearch is not working using NEST

I have a simple Dummy document in C#. 我在C#中有一个简单的虚拟文档。 I am trying to index it into elasticsearch for the first time using NEST client. 我试图使用NEST客户端首次将其索引到elasticsearch中。 But automaping is not working. 但是自动化并不起作用。

My dummy document is: 我的虚拟文件是:

class DummyRecord {
    public string RecordName;
    public int RecordId;
}

And the main program is: 主要计划是:

class Program
{
    static void Main(string[] args)
    {
        var node = new Uri("http://localhost:9200");
        var settings = new ConnectionSettings(node).DefaultTypeName("_doc");
        var client = new ElasticClient(settings);

        var doc = new DummyRecord {
            RecordName = "SOmething",
            RecordId = 1
        };

        var creaeIndexRespone = client.CreateIndex("DummyIndex",c => c.Mappings(ms=> ms.Map<DummyRecord>(m => m.AutoMap())));
        Console.WriteLine(creaeIndexRespone);

        var response = client.Index(doc, idx => idx.Index("DummyIndex"));
        Console.WriteLine(response);
        Console.ReadKey();
    }
}

All I am getting is the following output: 我得到的是以下输出:

Invalid NEST response built from a unsuccessful low level call on PUT: /DummyIndex 从PUT上的不成功低级别调用构建的无效NEST响应:/ DummyIndex

Invalid NEST response built from a unsuccessful low level call on POST: /DummyIndex/_doc 从POST上的不成功的低级别调用构建的无效NEST响应:/ DummyIndex / _doc

How to get this thing working. 如何使这个东西工作。 Is there anything I have to do while creating the settings more than this?? 在创建设置时,我还有什么需要做的吗?

var settings = new ConnectionSettings(node).DefaultTypeName("_doc");

Two things 两件事情

  1. Index name needs to be lowercase ie "DummyIndex" -> "dummyindex" 索引名称必须小写,即"DummyIndex" - > "dummyindex"
  2. DummyRecord members need to be properties and not fields DummyRecord成员需要是属性而不是字段

You can check whether the response to any API call is valid , and take action if needed 您可以检查对任何API调用的响应是否有效 ,并在需要时采取措施

var client = new ElasticClient();

var createIndexResponse = client.CreateIndex(defaultIndex, c => c
    .Mappings(m => m
        .Map<DummyRecord>(mm => mm
            .AutoMap()
        )
    )
);

if (!createIndexResponse.IsValid) {
    Console.WriteLine(createIndexResponse.DebugInformation);
}

Initiate Your final model and just pass that model to elastic to index it. 启动您的最终模型,然后将该模型传递给弹性以对其进行索引。

Based On Elastic's documentation, you can index your data like this. 根据Elastic的文档,您可以像这样索引数据。

    var person = new Person
{
    Id = 1,
    FirstName = "Martijn",
    LastName = "Laarman"
};

var indexResponse = client.IndexDocument(person); 

Updated : Index() vs IndexDocument 更新 :Index()vs IndexDocument

IndexDocument() is used when you want to simply index a single document. 当您只想索引单个文档时,使用IndexDocument()

Index() If you nedd to set additional parameters , you can use this method. Index()如果您需要设置其他参数,则可以使用此方法。

Take a look at Elastic documentation 看一下Elastic文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM