嵌套namedfilters需要承诺

4dbbbstv  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(270)

我有一个简单的要求:

var request4 =
                _client.Search<T>(s => s
                    .Aggregations(aggs =>
                        aggs.Filters("FacetedSearch",
                            f => f
                                .NamedFilters(g =>
                                    {
                                            var namedFilters = new NamedFiltersContainer();

                                            foreach (var facet in _facets)
                                        {
                                            namedFilters.Add(facet.Key,
                                                Query<T>.Terms(p => new Nest.TermsQuery
                                                    {Field = facet.Key, Terms = 
                                                            new[] {facet.Value}}));
                                        }

                                        return namedFilters;
                                    })

但是,编译器需要 Nest.IPromise<Nest.INamedFiltersContainer> 我还没有找到任何可以构造给定类型的承诺的构造函数。有没有办法将namedfilterscontainer转换成一个prmosie?

7gyucuyw

7gyucuyw1#

您可以使用传递给委托的参数来组成命名过滤器

var client = new ElasticClient();

var _facets = new Dictionary<string, string>
{
    ["foo"] = "bar",
    ["baz"] = "quux"
};

var request4 = client.Search<object>(s => s
    .Aggregations(aggs =>
        aggs.Filters("FacetedSearch", f => f
            .NamedFilters(g =>
            {
                foreach (var facet in _facets)
                {
                    g.Filter(facet.Key, Query<object>.Terms(p => p
                         .Field(facet.Key)
                         .Terms(new[] { facet.Value })
                        )
                    );
                }

                return g;
            })
        )
    )
);

产量

{
  "aggs": {
    "FacetedSearch": {
      "filters": {
        "filters": {
          "foo": {
            "terms": {
              "foo": ["bar"]
            }
          },
          "baz": {
            "terms": {
              "baz": ["quux"]
            }
          }
        }
      }
    }
  }
}

相关问题