1

I have a simple Dummy document in C#. I am trying to index it into elasticsearch for the first time using NEST client. 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

Invalid NEST response built from a unsuccessful low level call on POST: /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");
3
  • Why do you want to map your data? you just need to pass your model Commented Apr 30, 2019 at 6:06
  • Initially ElasticSearch doesn't contain a mapping . So if you try to index a document , ElasticSearch will refuse . Commented Apr 30, 2019 at 6:09
  • @AdityaNadig By default, Elasticsearch will allow you to index a document into a non-existent index, and will (a) create the index and (b) infer the mapping from the first document indexed. You can turn off both auto index creation and change inferred mapping, but both are enabled by default. Where defining an explicit mapping is useful is in search use cases and other cases where you know how you'll want to search documents Commented Apr 30, 2019 at 7:42

2 Answers 2

3

Two things

  1. Index name needs to be lowercase i.e. "DummyIndex" -> "dummyindex"
  2. DummyRecord members need to be properties and not fields

You can check whether the response to any API call is valid, and take action if needed

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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

var indexResponse = client.IndexDocument(person); 

Updated: Index() vs IndexDocument

IndexDocument() is used when you want to simply index a single document.

Index() If you nedd to set additional parameters , you can use this method.

Take a look at Elastic documentation

4 Comments

what is the difference between client.Index() and client.IndexDocument() ??
@AdityaNadig Updated
Thank you very much osman . But , Initially creating the index and mapping is required . And there is no much difference between Index() and IndexDocument() relevant to this context .
@AdityaNadig Yeah, I was confused. You wan to create an index not index a document. I got it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.