在 Nest 上处理 IGetResponse

Handling IGetResponse on Nest

我正在使用 Nest 的 Get API,但我不知道如何将响应 (IGetResponse) 转换为文档的特定类型,如下所示:

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

此外,有没有办法获取文档的另一个唯一字段或只接受 Id?

response.Source 持有 MyDocument.

类型的文档

As documentation says,您可以使用 get api 仅通过其 id 获取文档。

您可以告诉 elasticsearch 将文档中的其他字段视为 Id。 使用 NEST,您可以按如下方式执行此操作:

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"));

我的文档class:

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