有没有办法将 JSON 查询转换为 Elasticsearch Nest 搜索查询?

Is there any way to convert JSON query to Elasticsearch Nest search query?

我正在尝试将 JSON 查询转换为 elasticsearch 查询,但失败了。 我的查询是分组数据(聚合)。

{
"aggs":{
   "ResultCount":{
    "terms":{
              "field":"type"
            },
    "aggs":{
      "hits":{
        "top_hits":{
                "_source":{
                  "include":[
                                "year",
                                "type"
                            ]
                          }
                    }
                }
            }
        }
    }
}

我试过的代码:

var result = Client.Search<ModelClass>(s => s
            .Index("myIdx")
            .Type("myType")
            .Aggregations(a => a
                .Terms("ResultCount", t => t
                    .Field(p => p.year)
                )
            )
        );

尽可能提供帮助。提前谢谢你。

您的查询应如下所示

client.Search<ModelClass>(s => s
    .Index("myIndex")
    .Type("myType")
    .Aggregations(a => a
        .Terms("ResultCount", t => t.Field(p => p.Type)
            .Aggregations(a1 => a1
                .TopHits("myHits", h => h
                    .Source(d => d
                        .Includes(fd => fd
                            .Fields(
                                f1 => f1.Type, 
                                f2 => f2.Year
                            )
                        )
                    )
                )
            )
        )
    )
);

hits是保留关键字,所以我用myHits代替了它。 另外,在你的 json 查询中你有 include,我认为它应该是 includes

编辑: result.Aggs.Terms("ResultCount").Buckets.ToList() 的项目将具有以下结构

{
    "key": 2000,
    "doc_count": 1,
    "myHits": {
      "hits": {
         "total": 1,
         "max_score": 1,
         "hits": [
            {
               "_index": "myIndex",
               "_type": "myType",
               "_id": "AVupJZbRLWQhMqJPXgXa",
               "_score": 1,
               "_source": {
                  "year": 2000,
                  "type": "some type"
               }
            }
         ]
      }
}