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

时间:2017-10-10 15:31:42

标签: elasticsearch elasticsearch-5

所以,我有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"
    }
 }
}'

1 个答案:

答案 0 :(得分:0)

您可以使用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" } } }
相关问题