2016-11-18 82 views
0

使用上Elasticsearch的C#NEST API:Elasticsearch NEST API - 查詢右手食指

var searchResults = client.Search<Product>(s => s 
       .Index(Constants.ElasticSearchIndex) 
       .Query(q => q 
        .Raw(jsonRequest) 
       ) 
      ); 

查詢應該在/ sc_all /指數運行,但它運行在/ sc_all /產品/指數(這不存在 -/product /似乎因爲T =產品的搜索而被添加)。

如果我不喜歡這樣,/產品的更換/與常數,即/ sc_all /產品價值/ =>/sc_all/constant_value /:

var searchResults = client.Search<Product>(s => s 
       .Index(Constants.ElasticSearchIndex) 
       .Type(Constants.ElasticSearchType) 
       .Query(q => q 
        .Raw(jsonRequest) 
       ) 
      ); 

我應該怎麼做,如果我只是想要查詢/ sc_all /而沒有別的?

謝謝!


JSON請求:

「{\」 過濾\ 「:{\」 查詢\ 「:{\」 MATCH_ALL \ 「:{}},\」 過濾器\ 「:{\」 嵌套\「:{\」path \「:\」products \「,\」filter \「:{\」nested \「:{\」path \「:\」products.da \「,\」filter \「: {\「bool \」:{\「must \」:[{\「query \」:{\「query_string \」:{\「default_field \」:\「products.da.content \」,\「query \ 「:\」kildemoes \「}}}}}}}}}}}},\」from \「:0,\」size \「:100」

回答

1

您只需指定跨所有類型與.AllTypes()

var jsonRequest = "{ \"match_all\": {} }"; 

var searchResults = client.Search<Product>(s => s 
         .Index(Constants.ElasticSearchIndex) 
         .AllTypes() 
         .Query(q => q 
          .Raw(jsonRequest) 
         ) 
        ); 

這將產生以下的請求

POST http://localhost:9200/sc_all/_search 
{ 
    "query": { "match_all": {} } 
} 

記住,返回的所有文件會嘗試反序列化到的Product情況下,因此,如果您將針對多種不同類型,您可能需要使用一個共同的基本類型或dynamic另外,利用covariant search results

+0

謝謝,但我的Json請求已經包含「match_all」。我已經將它添加到上面的問題中。 – Louisa

+0

@Louisa我認爲你錯過了一點 - 如果你在搜索請求的主體上指定了'.AllTypes()',那麼你將在索引Constants.ElasticSearchIndex中搜索所有類型。我爲'jsonRequest'添加了一個示例值,以便您編譯的代碼。 –

+0

好點。感謝您的澄清。 – Louisa