2017-04-26 65 views
0

我想將JSON查詢轉換爲elasticsearch查詢,但我失敗了。 我的查詢是分組數據(聚合)。我試圖有沒有辦法將JSON查詢轉換爲Elasticsearch Nest搜索查詢?

{ 
"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) 
       ) 
      ) 
     ); 

幫助如果可能的話。先進的謝謝你。

回答

1

您的查詢應該是這樣的

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" 
       } 
      } 
     ] 
     } 
} 
+0

首先非常感謝你爲你的@魯本。仍然沒有獲得慾望的輸出。我有幾個查詢如何獲得這兩個字段在每個桶(組)的聚合。 ? –

+0

你想有2個桶,每個屬性一個。在結果中,您希望看到一個「類型」屬性存儲桶和另一個「年份」存儲桶,只是屬性的分組計數。我對嗎? –

+0

是 'var response = result.Aggs.Terms(「ResultCount」)。Buckets.ToList();' 但在這裏我沒有得到包含類型和年份的桶列表。這實際上是需要的。 –

相關問題