英文:
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; }
}
当我尝试使用用户的userID进行搜索时,无法获取用户:
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是通过Elasticsearch自动生成的,但它存储在_id
元字段下,而不是_source
下。我无法通过NEST客户端访问_id
元字段。
提前感谢。
英文:
I have a model as
UserModel.cs
public class UserModel
{
public string Id{get; set;}
public string Name{get; set;}
public string Age{get;set;}
}
And I am unable to get the User while Searching with his userID.
var clientProvider = new ElasticClientProvider();
var response = await clientProvider.Client.IndexAsync(UserModel, i => i
.Index("user_index")
.Type("user")
.Id(userModel.Id)
);
return response.IsValid;
When I am creating a record the _id is getting auto-generated through elastic Search but it is stored as _id
a meta field but not under the _source
. And I am unable to access _id
of meta field through NEST client.
Thanks in advance
答案1
得分: 1
您可以使用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
Document类:
public class Document
{
public string Id { get; set; }
public string Name { get; set; }
}
希望这有所帮助。
英文:
You can retrieve document by id with help of Get
method. Here is an example:
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}");
Prints:
Id: 1 Name: name1
Document class:
public class Document
{
public string Id { get; set; }
public string Name { get; set; }
}
Hope that helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论