0

So, I have myindex elastic search index with two types type1 and type2. Both the type has two common fields as name and descriptionas below:

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

I want 5 results from type1 and 5 results from result2 if I specify the size as 10 in a single search query?

The below query gives me 10 results from type1 if the matching results are more from type1:

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

I can do this in two different queries as below, but I want to do it in one go.

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"
    }
 }
}'
Rishikesh Darandale
  • 3,222
  • 4
  • 17
  • 35

1 Answers1

0

You can use a multisearch and the results will come back in two separate arrays.

GET /_msearch --data-binary

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