Yasel
Yasel

Reputation: 3120

Handling IGetResponse on Nest

I am using the Get API of Nest, but I don't know how to typecast the respone (IGetResponse) to the specific type of the document, something like this:

var response = client.Get<MyDocument>(documentId);
return response.Document(); // Or something like this that returns a MyDocument type

Also, is there a way to get the document for another unique field or only the Id is accepted?

Upvotes: 0

Views: 231

Answers (1)

Rob
Rob

Reputation: 9979

response.Source holds document of type MyDocument.

As documentation says, you can use get api to get documents only by their ids.

You can tell elasticsearch to treat other field from document as Id. With NEST you can do this as follows:

var indicesOperationResponse = client.CreateIndex(descriptor => descriptor
    .Index(indexName)
    .AddMapping<Document>(m => m.IdField(f => f.Path("uniqueValue"))));

client.Index(new Document{UniqueValue = "value1"});

var getResponse = client.Get<Document>(g => g.Id("value1"));

My document class:

public class Document
{
    public string UniqueValue { get; set; }
}

Upvotes: 1

Related Questions