有什么方法可以将JSON查询转换为Elasticsearch Nest搜索查询



我正在尝试将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"
               }
            }
         ]
      }
}

最新更新