简体   繁体   English

使用 NEST 为 elasticsearch 编写 DSL

[英]Write DSL using NEST for elasticsearch

I am new to ElasticSearch.我是 ElasticSearch 的新手。 I want to write a simple aggregation query using NEST in asp.net.我想在asp.net 中使用NEST 编写一个简单的聚合查询。 How to write this simple query如何编写这个简单的查询

GET /_search
{
    "aggs" : {
        "genres" : {
            "terms" : { "field" : "genre" }
        }
    }
}

I started something like this:我开始了这样的事情:

var response = _connection.Get<ElasticClient>().Search<JObject>(s =>
{

}

Thanks谢谢

Started writing a comment, but maybe let's put it as an answer :)开始写评论,但也许让我们把它作为答案:)

Here are the docs for writing aggregations using NEST syntax. 以下是使用 NEST 语法编写聚合的文档。

As the example shows, for this sample aggregation:如示例所示,对于此示例聚合:

{
  "aggs": {
    "name_of_child_agg": {
      "children": {
        "type": "commits"
      },
      "aggs": {
        "average_per_child": {
          "avg": {
            "field": "confidenceFactor"
          }
        },
        "max_per_child": {
          "max": {
            "field": "confidenceFactor"
          }
        },
        "min_per_child": {
          "min": {
            "field": "confidenceFactor"
          }
        }
      }
    }
  }
}

you can write this NEST fluent code:你可以编写这个 NEST 流畅的代码:

s => s
.Aggregations(aggs => aggs
    .Children<CommitActivity>("name_of_child_agg", child => child
        .Aggregations(childAggs => childAggs
            .Average("average_per_child", avg => avg.Field(p => p.ConfidenceFactor))
            .Max("max_per_child", avg => avg.Field(p => p.ConfidenceFactor))
            .Min("min_per_child", avg => avg.Field(p => p.ConfidenceFactor))
        )
    )
)

Also, I don't think you want to use JObject type as POCO, this should put some light on how to use your custom type to map c# class into elasticsearch type.另外,我认为您不想将JObject类型用作 POCO, 应该说明如何使用自定义类型将 c# 类映射到 elasticsearch 类型。 The reasoning behind: to have type responsible only for "communicating" with elasticsearch, so having JObject in this role may not the best option, as it's not a type created by you and it may evolve in the future in the way you don't want.背后的原因:让类型只负责与JObject “通信”,所以让JObject这个角色可能不是最好的选择,因为它不是你创建的类型,它可能会在未来以你没有的方式发展想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM