如何使用 Nest Fluent DSL 生成过滤嵌套聚合?
How can I generate filtered nested aggregation using Nest Fluent DSL?
我有以下 ElasticSearch 查询:
{
"size": 0,
"aggs": {
"product_attribute_nested_agg": {
"nested": {
"path": "productAttributes"
},
"aggs": {
"inner": {
"filter": {
"fuzzy": {
"productAttributes.name": {
"value": "SS",
"fuzziness": 3
}
}
},
"aggs": {
"terms_nested_agg": {
"terms": {
"field": "productAttributes.name"
}
}
}
}
}
}
}
}
我使用 Nest 作为客户端库,在这里我如何使用 Fluent DSL 生成嵌套聚合查询:
var searchResponse = elasticClient.Search<ProductType>(s => s
.Index(indices)
.Type(Types.Type(typeof(ProductType)))
.Size(0)
.Aggregations(a => a
.Nested("product_attribute_nested_agg", n => n
.Path(Infer.Field<ProductType>(ff => ff.ProductAttributes))
.Aggregations(aa => aa
.Terms("terms_nested_agg", t => t
.Field(p => p.ProductAttributes.Suffix("name"))
)
)
)
)
);
但是如何使用 Fluent DSL 语法生成内部过滤器?
经过一些调查和搜索,我能够使用 Nest fluent DSL 生成过滤嵌套聚合:
var searchResponse = elasticClient.Search<ProductType>(s => s
.Index(indices)
.Type(Types.Type(typeof(ProductType)))
.Size(0)
.Aggregations(a => a
.Nested("product_attribute_nested_agg", n => n
.Path(Infer.Field<ProductType>(ff => ff.ProductAttributes))
.Aggregations(a1 => a1
.Filter("inner", ia => ia
.Filter(f => f
.Fuzzy(fuzzy => fuzzy
.Field(Infer.Field<ProductType>(ff => ff.ProductAttributes.First().Name))
.Value(productAttribute)
.Fuzziness(Fuzziness.EditDistance(3))
)
).Aggregations(na => na
.Terms("terms_nested_agg", t => t
.Field(p => p.ProductAttributes.Suffix("name"))
)
)
)
)
)
)
);
var result = new List<ProductAttributesSuggestionResult>();
var nestedAgg = searchResponse.Aggregations.Nested("product_attribute_nested_agg");
var inner = (SingleBucketAggregate)nestedAgg.Values.FirstOrDefault();
var termsAgg = inner.Terms("terms_nested_agg");
foreach (var bucket in termsAgg.Buckets)
{
result.Add(new ProductAttributesSuggestionResult
{
Name = bucket.Key,
ProductsCount = bucket.DocCount ?? 0
});
}
希望我的回答对遇到同样问题的人有所帮助。
我有以下 ElasticSearch 查询:
{
"size": 0,
"aggs": {
"product_attribute_nested_agg": {
"nested": {
"path": "productAttributes"
},
"aggs": {
"inner": {
"filter": {
"fuzzy": {
"productAttributes.name": {
"value": "SS",
"fuzziness": 3
}
}
},
"aggs": {
"terms_nested_agg": {
"terms": {
"field": "productAttributes.name"
}
}
}
}
}
}
}
}
我使用 Nest 作为客户端库,在这里我如何使用 Fluent DSL 生成嵌套聚合查询:
var searchResponse = elasticClient.Search<ProductType>(s => s
.Index(indices)
.Type(Types.Type(typeof(ProductType)))
.Size(0)
.Aggregations(a => a
.Nested("product_attribute_nested_agg", n => n
.Path(Infer.Field<ProductType>(ff => ff.ProductAttributes))
.Aggregations(aa => aa
.Terms("terms_nested_agg", t => t
.Field(p => p.ProductAttributes.Suffix("name"))
)
)
)
)
);
但是如何使用 Fluent DSL 语法生成内部过滤器?
经过一些调查和搜索,我能够使用 Nest fluent DSL 生成过滤嵌套聚合:
var searchResponse = elasticClient.Search<ProductType>(s => s
.Index(indices)
.Type(Types.Type(typeof(ProductType)))
.Size(0)
.Aggregations(a => a
.Nested("product_attribute_nested_agg", n => n
.Path(Infer.Field<ProductType>(ff => ff.ProductAttributes))
.Aggregations(a1 => a1
.Filter("inner", ia => ia
.Filter(f => f
.Fuzzy(fuzzy => fuzzy
.Field(Infer.Field<ProductType>(ff => ff.ProductAttributes.First().Name))
.Value(productAttribute)
.Fuzziness(Fuzziness.EditDistance(3))
)
).Aggregations(na => na
.Terms("terms_nested_agg", t => t
.Field(p => p.ProductAttributes.Suffix("name"))
)
)
)
)
)
)
);
var result = new List<ProductAttributesSuggestionResult>();
var nestedAgg = searchResponse.Aggregations.Nested("product_attribute_nested_agg");
var inner = (SingleBucketAggregate)nestedAgg.Values.FirstOrDefault();
var termsAgg = inner.Terms("terms_nested_agg");
foreach (var bucket in termsAgg.Buckets)
{
result.Add(new ProductAttributesSuggestionResult
{
Name = bucket.Key,
ProductsCount = bucket.DocCount ?? 0
});
}
希望我的回答对遇到同样问题的人有所帮助。