在dsl语法中嵌套多个术语

polkgigr  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(347)

我正在尝试将以下查询转换为嵌套dsl语法:

{
  "aggs": {
    "color": {
      "terms": {
        "field": "Color"
      }
    },
    "weight": {
      "terms": {
        "field": "Weight"
      }
    }
  }
}

每个术语都来自包含name和fieldid的列表。到目前为止,我已经做到了:

var request2 = _client.Search<T>(s => s
               .Aggregations(aggs =>
                   aggs.Terms("Weight", x => x.Field("Weight")).Terms("Color", x => x.Field("Weight"))));

这正如预期的工作,但我需要能够提供参数 Weight 以及 Color 动态地,因为方法可以用不同的参数集调用。有没有一种方法可以使用这样的东西:

aggs.Terms(x => x.field( myList.foreach(y.value))));

我想这在对象初始值设定项语法中会更好地工作,但是我更希望在dsl中工作。

qcbq4gxm

qcbq4gxm1#

像下面这样的方法就行了

var client = new ElasticClient();

var fields = new List<string>
{
    "color",
    "weight",
    "foo",
    "bar"
};

var response = client.Search<object>(s => s
    .Aggregations(aggs =>
    {
        foreach (var field in fields)
        {
            aggs.Terms(field, t => t.Field(field));
        }

        return aggs;
    })
);

它生成以下请求

{
  "aggs": {
    "color": {
      "terms": {
        "field": "color"
      }
    },
    "weight": {
      "terms": {
        "field": "weight"
      }
    },
    "foo": {
      "terms": {
        "field": "foo"
      }
    },
    "bar": {
      "terms": {
        "field": "bar"
      }
    }
  }
}

相关问题