【问题标题】:Nest multiple terms in DSL syntax在 DSL 语法中嵌套多个术语
【发布时间】:2020-10-30 05:59:32
【问题描述】:

我正在尝试将以下查询转换为 NEST 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"))));


按预期工作,但是我需要能够动态提供参数WeightColor,因为可以使用不同的参数集调用该方法。有没有办法使用类似的东西:

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

我想这在使用 Object 初始化器语法时会更好,但我宁愿让它在 dsl 中工作。

【问题讨论】:

    标签: elasticsearch nest dsl


    【解决方案1】:

    类似下面的方法会起作用

    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"
          }
        }
      }
    }
    

    【讨论】:

    • 这看起来正是我想要的,明天将测试它并报告回来。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 2014-12-30
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多