Elasticsearch:当我尝试使用 c# 将动态嵌套对象添加到过滤器时出现问题

问题描述 投票:0回答:1

当我尝试将动态嵌套对象添加到过滤器中时遇到问题。我的查询在 Kibana 中运行良好,但是当我将其转换为代码时(我使用的是

Elastic.Clients.Elasticsearch v8.11.0
),最后一个嵌套对象会覆盖第一个。使用调用
foreach
函数的
GetFacetNestedScope
循环插入对象。

这是我的 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 包的新版本的文档丢失,其他帖子无法解决此问题。 为什么结果不同?我的代码中缺少某些内容吗?

c# asp.net-core elasticsearch kibana .net-8.0
1个回答
0
投票

不要尝试在单个过滤器描述符中初始化多个子查询,而是创建一个过滤器描述符列表:

if (facets != null && facets.Any())
{
    var filterDescriptors = new List<Action<QueryDescriptor<Product>>>();

    foreach (var facet in facets)
    {
        filterDescriptors.Add(f => GetFacetNastedScope(f, facet));
    }

    b.Filter(filterDescriptors.ToArray());
}
© www.soinside.com 2019 - 2024. All rights reserved.