当我试图将动态嵌套对象添加到filter中时,我遇到了一个问题.我的查询在Kibana中运行得很好,但是当我把它翻译成代码(我使用的是Elastic.Clients.Elasticsearch v8.11.0
)时,最后一个嵌套对象覆盖了第一个.对象是使用调用GetFacetNestedScope
函数的foreach
循环插入的.
这是我的Kibana查询:
{
"size": 2000,
"query": {
"bool": {
"should": [
{ "match": { "description.title": { "query": "borsa termica", "prefix_length": 1, "fuzziness": "AUTO", "_name": "title" } } },
{ "match": { "description.longDescription": { "query": "borsa termica", "prefix_length": 1, "fuzziness": "AUTO", "_name": "longDescription" } } },
{ "match": { "options.code": { "query": "borsa termica", "prefix_length": 1, "fuzziness": "AUTO", "_name": "code" } } }
],
"filter": [
{
"nested": {
"_name": "MARCA_guzzini",
"path": "erpAttributes",
"query": {
"bool": {
"must": [
{
"term": {
"erpAttributes.erpId": {
"value": "MARCA"
}
}
},
{
"nested": {
"path": "erpAttributes.values",
"query": {
"term": {
"erpAttributes.values.id": {
"value": "guzzini"
}
}
}
}
}
]
}
}
}
},
{
"nested": {
"_name": "colore_bianco",
"path": "erpAttributes",
"query": {
"bool": {
"must": [
{
"term": {
"erpAttributes.erpId": {
"value": "COL"
}
}
},
{
"nested": {
"path": "erpAttributes.values",
"query": {
"term": {
"erpAttributes.values.id": {
"value": "bianco"
}
}
}
}
}
]
}
}
}
}
]
}
},
"aggs": {
"attributi_unici": {
"nested": {
"path": "erpAttributes"
},
"aggs": {
"nomi_attributi": {
"terms": {
"field": "erpAttributes.name",
"size": 10
},
"aggs": {
"valori_unici": {
"nested": {
"path": "erpAttributes.values"
},
"aggs": {
"id_valori": {
"terms": {
"field": "erpAttributes.values.id",
"size": 10
}
}
}
}
}
}
}
}
}
}
这是我的代码(所有的代码都是GetQueryFunction
):
public static QueryDescriptor<Product> GetQuery(QueryDescriptor<Product> q, IEnumerable<Facet> facets, string searchTerm, string categorySlug)
{
return q
.Bool(b =>
{
if (!string.IsNullOrWhiteSpace(categorySlug))
{
b.Must(mu => mu
.Term(t => t.Field(f => f.Categories.First().Id.Suffix(Constants.ElasticSearch.SuffixKeyword))
.Value(categorySlug)));
}
if (!string.IsNullOrWhiteSpace(searchTerm))
{
b.Should(
sh => GetSearchTermMatchScope(sh, searchTerm,
Constants.ElasticSearch.SearchTermFields.Title),
sh => GetSearchTermMatchScope(sh, searchTerm,
Constants.ElasticSearch.SearchTermFields.LongDescription),
sh => GetSearchTermMatchScope(sh, searchTerm,
Constants.ElasticSearch.SearchTermFields.Code)
);
}
if (facets != null && facets.Any())
{
b.Filter(f =>
{
foreach (var facet in facets)
{
GetFacetNastedScope(f, facet);
}
});
}
});
}
public static QueryDescriptor<Product> GetFacetNastedScope(QueryDescriptor<Product> f, Facet facet)
{
return f.Nested(n => n
.QueryName(facet.Name)
.Path(Constants.ElasticSearch.ProductFacetsNastedFields.ErpAttributes)
.Query(qu => qu
.Bool(nb => nb
.Must(m => m
.Term(t => t
.Field(Constants.ElasticSearch.ProductFacetsNastedFields.ErpAttributesErpId)
.Value(facet.Name)
), m => m
.Nested(nn => nn
.Path(Constants.ElasticSearch.ProductFacetsNastedFields.ErpAttributesValues)
.Query(nnq => nnq
.Term(t => t
.Field(Constants.ElasticSearch.ProductFacetsNastedFields.ErpAttributesValuesId)
.Value(facet.Value)))
)
)
)
)
);
}
这个nuget包的新版本的文档丢失了,其他帖子也没有解决这个问题. 为什么结果不同?我的代码中缺少了什么?