说,我想在我的页面的特定部分显示某些结果。有没有办法根据我的查询将弹性搜索结果分组。
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"}}
]
}
}
}
是否可以将这两个组合成一个弹性调用,并将结果分组?
答案 0 :(得分: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"}}}]}}}