如何从 elasticsearch 索引中获取多种类型的搜索结果?

How to get multiple type search results from elasticsearch index?

所以,我有 myindex 弹性搜索索引,有两种类型 type1type2。这两种类型都有两个公共字段 namedescription,如下所示:

{
"name": "",
"description": ""
}

如果我在单个搜索查询中将大小指定为 10,我想要 type1 的 5 个结果和 result2 的 5 个结果?

如果匹配结果更多来自 type1:

,则下面的查询从 type1 中给出了 10 个结果
curl -XPOST 'localhost:9200/myindex/_search?pretty&pretty' -H 'Content-Type: application/json' -d'
{
 "size": 10,
 "query": {
    "match": {
      "name": "xyz"
    }
 }
}'

我可以在下面的两个不同查询中执行此操作,但我想一次性完成。

curl -XPOST 'localhost:9200/myindex/type1/_search?pretty&pretty' -H 'Content-Type: application/json' -d'
{
 "size": 5,
 "query": {
    "match": {
      "name": "xyz"
    }
 }
}'

curl -XPOST 'localhost:9200/myindex/type2/_search?pretty&pretty' -H 'Content-Type: application/json' -d'
{
 "size": 5,
 "query": {
    "match": {
      "name": "xyz"
    }
 }
}'

您可以使用 multisearch,结果将返回到两个单独的数组中。

GET /_msearch --data-binary

{ "index" : "myindex" , "type" : "type1" }
{ "size" : 5, "query" : { "match" : { "name" : "xyz" } } }
{ "index" : "myindex", "type" : "type2" }
{ "size" : 5, "query" : { "match" : { "name" : "xyz" } } }