2015-01-05 42 views
0

我想返回NEST查詢結果作爲控制檯輸出。如何呈現NEST查詢結果?

我的查詢是:

private static void PerformTermQuery(string query) 
{ 
    var result = 
     client.Search<Post>(s => s 
     .Query(p => p.Term(q => q.PostText, query))); 
} 

我所得到的是對象,具有2個文件。如何「解壓縮」它顯示文件爲json(全部或部分)到控制檯?

回答

1

假設你正在使用NEST的1.3.1版本中,您可以:

  • 得到使用result.RequestInformation.ResponseRaw.Utf8String()
  • 解析JSON生JSON響應得到_source
  • 包含/排除在SearchDescriptor使用SearchSourceDescriptor_source性質 var result = client.Search<Post>(s => s .Query(p => p.Term(q => q.PostText, query)).Source(...));
+0

result.RequestInformation.ResponseRaw.Utf8String()不可用。爲什麼? –

+0

[此方法是內部的](https://github.com/elasticsearch/elasticsearch-net/blob/develop/src/Nest/Extensions/Extensions.cs)。當我檢查這個時,我在NEST單元測試環境中。我的錯。 'Encoding.UTF8.GetString(...)'會爲你工作。 – Rob

0

對於NE ST/Elasticsearch 5.x,result.RequestInformation不再可用。相反,你可以先訪問原始請求和響應數據的請求,禁止直接流:

var results = elasticClient.Search<MyObject>(s => s 
    .Index("myindex") 
    .Query(q => q 
     ... 
    ) 
    .RequestConfiguration(rc => rc 
     .DisableDirectStreaming() 
    ) 
); 

在您停用直接流,你可以訪問results.ApiCall.ResponseBodyInBytes(如果你看看這個屬性沒有禁用直接流,這將是空)

string rawResponse = Encoding.UTF8.GetString(results.ApiCall.ResponseBodyInBytes); 

這可能對性能有影響,所以我會盡量避免使用它生產。如果您需要在所有查詢中使用它,您還可以在連接/客戶端級別禁用直接流式傳輸。 Take a look at the documentation for more information