这是我的映射
[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个文件的状态为&#34; Enabled
&#34;;
我试图计算每个嵌套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
}
}
}
}
}
有什么想法吗?非常感谢。
答案 0 :(得分:0)
这是一个有效的例子
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
}
}
}
}