2016-12-13 70 views
0

說,我想在我的頁面的特定部分顯示某些結果。有沒有一種方法可以根據我的查詢將彈性搜索結果分組。elasticsearch的羣組搜索結果

E.g.

1.

GET /my_index/_search 
{ 
"query": { 
    "bool": { 
     "must": [ 
      {"match": {"field1": "value1"}}, 
      {"match": {"field7": "value7"}} 
      ] 
     } 
    } 
} 

2.

GET /my_index/_search 
{ 
"query": { 
    "bool": { 
     "must": [ 
      {"match": {"field2": "value2"}} 
      ] 
     } 
    } 
} 

是有可能這些2組合成一個呼叫到彈性和剛組結果嗎?

回答

2

您可以在一個請求發送給多個搜索與Multi Search API

這在NEST暴露

var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200")); 
var defaultIndex = "my_index"; 
var connectionSettings = new ConnectionSettings(pool) 
     .DefaultIndex(defaultIndex) 

var client = new ElasticClient(connectionSettings); 

var multiSearchResponse = client.MultiSearch(ms => ms 
    .Index(defaultIndex) 
    .Search<Document>("search1", s => s 
     .AllTypes() 
     .Query(q => q 
      .Match(m => m 
       .Field(f => f.Field1) 
       .Query("value1") 
      ) && q 
      .Match(m => m 
       .Field(f => f.Field7) 
       .Query("value7") 
      ) 
     ) 
    ) 
    .Search<Document>("search2", s => s 
     .AllTypes() 
     .Query(q => q 
      .Bool(b => b 
       .Must(mu => mu 
        .Match(m => m 
         .Field(f => f.Field2) 
         .Query("value2") 
        ) 
       ) 
      ) 
     ) 
    ) 
); 

// get the search responses for one of the searches by name 
var search1Responses = multiSearchResponse.GetResponse<Document>("search1"); 

這將產生以下搜索

POST http://localhost:9200/my_index/_msearch 
{"index":"my_index"} 
{"query":{"bool":{"must":[{"match":{"field1":{"query":"value1"}}},{"match":{"field7":{"query":"value7"}}}]}}} 
{"index":"my_index"} 
{"query":{"bool":{"must":[{"match":{"field2":{"query":"value2"}}}]}}} 
+0

我只是想'MultiSearchAsync(IMultiSearchRequest請求)'和我的'GetResponse '似乎沒有得到結果。 'var result = response.GetResponse (「search1」)'works but'var result = response.GetResponse (「search1」)'not。我失蹤的任何概念? –

+0

傳遞給'GetResponse ()'的'T'泛型參數類型應該是一個類型,該文檔可以被反序列化爲 –

+0

T是與索引爲'Index ' –