gpt4 book ai didi

c# - NEST ElasticSearch C#如何在嵌套对象上进行过滤

转载 作者:行者123 更新时间:2023-12-02 23:03:11 24 4
gpt4 key购买 nike

我们有一个“联系人”类型结构,其中包含“关系”嵌套结构。我正在尝试使用NEST ElasticSearch .Net客户端进行搜索,并获取工作人员已保存的联系人数量的计数。但是不能降低语法。

这是“联系”结构的内容:

    public class FieldName : FieldNameBase
{
public const string IndexTypeName = "contact";

public const string Id = @"Id";
public const string Name = @"name";
public const string Status = @"status";
public const string FirstName = @"firstName";
public const string LastName = @"lastName";
public const string Email = @"email";
public const string AvatarUrl = @"avatarUrl";

public class Relationships
{
public const string StaffID = @"staffID";
public const string ContactID = @"contactID";
public const string FacebookUid = @"facebookUid";
public const string Name = @"name";
public const string Email = @"email";
public const string Level = @"level";
public const string AvatarUrl = @"avatarUrl";
}
}

[ElasticType(Name = FieldName.IndexTypeName, IdProperty = FieldName.Id)]
public class Data : AccountData
{
[ElasticProperty(Name = FieldNameBase.Id, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)]
public string Id { get; set; }

[ElasticProperty(Name = FieldNameBase.AccountID)]
public int AccountID { get; set; }

[ElasticProperty(Name = FieldName.FirstName, IncludeInAll = false, Index = FieldIndexOption.Analyzed)]
public string FirstName { get; set; }

[ElasticProperty(Name = FieldName.LastName, IncludeInAll = false, Index = FieldIndexOption.Analyzed)]
public string LastName { get; set; }

[ElasticProperty(Name = FieldName.Name, IncludeInAll = true, Index = FieldIndexOption.Analyzed)]
public string Name { get; set; }

[ElasticProperty(Name = FieldName.AvatarUrl, IncludeInAll = true, Index = FieldIndexOption.NotAnalyzed)]
public string AvatarUrl { get; set; }

[ElasticProperty(Name = FieldName.Email, IncludeInAll = true, Index = FieldIndexOption.NotAnalyzed)]
public string Email { get; set; }

[ElasticProperty(Name = FieldName.Phone, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)]
public string Phone { get; set; }

[ElasticProperty(Type = FieldType.Nested)]
public List<Relationship> Relationships { get; set; }

public class Relationship
{
[ElasticProperty(Name = FieldName.Relationships.StaffID)]
public int? StaffID { get; set; }

[ElasticProperty(Name = FieldName.Relationships.ContactID, IncludeInAll=false, Index = FieldIndexOption.NotAnalyzed)]
public string ContactID { get; set; }

[ElasticProperty(Name = FieldName.Relationships.FacebookUid, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)]
public string FacebookUid { get; set; }

[ElasticProperty(Name = FieldName.Relationships.Name, IncludeInAll = false, Index = FieldIndexOption.Analyzed)]
public string Name { get; set; }

[ElasticProperty(Name = FieldName.Relationships.Email, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)]
public string Email { get; set; }

[ElasticProperty(Name = FieldName.Relationships.Level, IncludeInAll = false, Index = FieldIndexOption.Analyzed)]
public string Level { get; set; }

[ElasticProperty(Name = FieldName.Relationships.AvatarUrl, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)]
public string AvatarUrl { get; set; }
}

}

现在,我尝试将其用作返回结果或仅返回计数的代码(请注意,由于我仍在尝试找出如何计数的方法,因此我将结果留给了更多来自试用版的列表,这是一个错误。以及):
    public int GetFriendCount(int staffID)
{
List<Data> list = new List<Data>();

base.ConnectStatus = null;

var results = this.Client.Search<Data>(s => s
//.SearchType(Elasticsearch.Net.SearchType.Count)
.Query(q => q.MatchAll())
.Filter(f =>
f.Nested(n => n
.Path("relationships")
.Filter(f2 => f2 .Term("relationships.staffID", staffID))
)
)
// .Nested
);

if (results != null)
{
base.ConnectStatus = results.ConnectionStatus;

if (results.Documents.Count<Data>() > 0)
list = results.Documents.ToList<Data>();
}

return list.Count;
}

最佳答案

        var results = this.Client.Search<Data>(s => s
.Query(q => q
.Nested(n => n
.Path("relationships")
.Filter(f => f.Term("relationships.staffID", staffID))
)
)
);

关于c# - NEST ElasticSearch C#如何在嵌套对象上进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com