gpt4 book ai didi

c# - C#Nest&Elasticsearch 6.x:如何过滤/计数嵌套字段(在内部List 中)
转载 作者:行者123 更新时间:2023-12-02 23:59:28 24 4
gpt4 key购买 nike

这是我的 map

[ElasticsearchType(Name = "Topic")]
public class Topic
{
[Number(NumberType.Integer, Coerce = true)]
public EnumStatus Status { get; set; }

[Nested]
public List<KeywordValue> KeywordValues { get; set; }

}

[ElasticsearchType(Name = "KeywordValue")]
public class KeywordValue
{
[Keyword]
public string KeywordId { get; set; }

}

我在索引中有10个 Topic类型的文档,每个 KeywordValues类型的 List<KeywordValue>属性/字段包含5个 KeywordValue(列表中的5个元素)。

9个文档的状态为“ Enabled”;

我正在尝试计算每个嵌套 KeywordValues字段中的元素总数。返回的结果是9,但我想得到45(9 * 5)

我正在这样做:
var response = Topic.CurrentConnection.Search<Topic>(s => s
.Size(0)
.Aggregations(fa => fa
.Filter("filtered_aggs", f => f
.Filter(fd => fd.Term(t => t.Status, Topic.EnumStatus.Enabled))
.Aggregations(ta => ta
.Nested("kv", n=>n.Path(p => p.KeywordValues)
.Aggregations(aa => aa
.ValueCount("vc", v => v.Field(vf => vf.KeywordValues.First().KeywordId))))
)
)
)
);


if (response.IsValid)
{
var agg = response.Aggregations.Nested("filtered_aggs");
var n = agg.Nested("kv");
var z = n.ValueCount("vc");
result.Object = z.Value;
}

原始查询等效项:
# Request:
{
"size": 0,
"aggs": {
"filtered_aggs": {
"filter": {
"term": {
"Status": {
"value": 0
}
}
},
"aggs": {
"kv": {
"nested": {
"path": "KeywordValues"
},
"aggs": {
"vc": {
"value_count": {
"field": "KeywordValues.KeywordId"
}
}
}
}
}
}
}
}
# Response:
{
"took" : 80,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 10,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"filter#filtered_aggs" : {
"doc_count" : 9,
"nested#kv" : {
"doc_count" : 9,
"value_count#vc" : {
"value" : 9
}
}
}
}
}

任何的想法?非常感谢。

最佳答案

这是一个有效的例子

private static void Main()
{
var defaultIndex = "topics";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);

var client = new ElasticClient(settings);

if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);

client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<Topic>(mm => mm
.AutoMap()
)
)
);

var documents = Enumerable.Range(1, 10)
.Select(i => new Topic
{
Status = i == 1 ? EnumStatus.Disabled : EnumStatus.Enabled,
KeywordValues = Enumerable.Range(1, 5)
.Select(j => new KeywordValue
{
KeywordId = $"keyword {i} {j}"
}).ToList()
});

client.Bulk(b => b
.IndexMany(documents, (d, document) => d
.Document(document)
)
.Refresh(Refresh.WaitFor)
);

client.Search<Topic>(s => s
.Size(0)
.Query(q => +q
.Term(t => t.Status, (int)EnumStatus.Enabled)
)
.Aggregations(ta => ta
.Nested("kv", n => n.Path(p => p.KeywordValues)
.Aggregations(aa => aa
.ValueCount("vc", v => v.Field(vf => vf.KeywordValues.First().KeywordId))))
)
);
}

[ElasticsearchType(Name = "Topic")]
public class Topic
{
[Number(NumberType.Integer, Coerce = true)]
public EnumStatus Status { get; set; }

[Nested]
public List<KeywordValue> KeywordValues { get; set; }

}

[ElasticsearchType(Name = "KeywordValue")]
public class KeywordValue
{
[Keyword]
public string KeywordId { get; set; }
}

public enum EnumStatus
{
Enabled,

Disabled
}

对搜索请求的响应是
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 9,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"nested#kv" : {
"doc_count" : 45,
"value_count#vc" : {
"value" : 45
}
}
}
}

关于c# - C#Nest&Elasticsearch 6.x:如何过滤/计数嵌套字段(在内部List <object>中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50412880/

24 4 0