在Nest上处理IGetResponse

时间:2015-05-19 15:01:52

标签: elasticsearch nest

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

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

此外,有没有办法让文档获得另一个唯一字段或只接受ID?

1 个答案:

答案 0 :(得分:1)

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

我的文档类:

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