通过 NEST 客户端获取弹性搜索中索引的 GetById
GetById of an index in Elastic search through NEST client
我有一个模型
UserModel.cs
public class UserModel
{
public string Id{get; set;}
public string Name{get; set;}
public string Age{get;set;}
}
而且我在使用他的用户 ID 搜索时无法获取用户。
var clientProvider = new ElasticClientProvider();
var response = await clientProvider.Client.IndexAsync(UserModel, i => i
.Index("user_index")
.Type("user")
.Id(userModel.Id)
);
return response.IsValid;
当我创建记录时,_id 是通过弹性搜索自动生成的,但它存储为 _id
元字段但不在 _source
下。而且我无法通过 NEST 客户端访问元字段的 _id
。
提前致谢
您可以借助 Get
方法通过 ID 检索文档。这是一个例子:
await client.IndexManyAsync(new []
{
new Document{Id = "1", Name = "name1"},
new Document{Id = "2"},
new Document{Id = "3"},
new Document{Id = "4"},
new Document{Id = "5"}
});
await client.Indices.RefreshAsync();
var getResponse = await client.GetAsync<Document>("1");
System.Console.WriteLine($"Id: {getResponse.Source.Id} Name: {getResponse.Source.Name}");
打印:
Id: 1 Name: name1
文档 class:
public class Document
{
public string Id { get; set; }
public string Name { get; set; }
}
希望对您有所帮助。
我有一个模型 UserModel.cs
public class UserModel
{
public string Id{get; set;}
public string Name{get; set;}
public string Age{get;set;}
}
而且我在使用他的用户 ID 搜索时无法获取用户。
var clientProvider = new ElasticClientProvider();
var response = await clientProvider.Client.IndexAsync(UserModel, i => i
.Index("user_index")
.Type("user")
.Id(userModel.Id)
);
return response.IsValid;
当我创建记录时,_id 是通过弹性搜索自动生成的,但它存储为 _id
元字段但不在 _source
下。而且我无法通过 NEST 客户端访问元字段的 _id
。
提前致谢
您可以借助 Get
方法通过 ID 检索文档。这是一个例子:
await client.IndexManyAsync(new []
{
new Document{Id = "1", Name = "name1"},
new Document{Id = "2"},
new Document{Id = "3"},
new Document{Id = "4"},
new Document{Id = "5"}
});
await client.Indices.RefreshAsync();
var getResponse = await client.GetAsync<Document>("1");
System.Console.WriteLine($"Id: {getResponse.Source.Id} Name: {getResponse.Source.Name}");
打印:
Id: 1 Name: name1
文档 class:
public class Document
{
public string Id { get; set; }
public string Name { get; set; }
}
希望对您有所帮助。