【问题标题】:GetById of an index in Elastic search through NEST client通过 NEST 客户端在弹性搜索中获取索引的 GetById
【发布时间】:2020-01-06 21:05:50
【问题描述】:

我有一个模型 用户模型.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。 在此先感谢

【问题讨论】:

    标签: c# .net elasticsearch nest


    【解决方案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
    

    文档类:

    public class Document
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    

    希望对您有所帮助。

    【讨论】:

    • 我不能存储弹性搜索生成的id而不是我隐式指定的id吗?
    • 看看here的答案。
    • 该答案未加载。它弹出500错误。可以给我另一个链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多