Reputation: 827
I'm trying to return multiple "buckets" of results from Elasticsearch in one HTTP request.
I'm using the _msearch API.
I'm using the following query:
POST /_msearch
{"index" : "[INDEXNAME]", "type":"post"}
{"query" : {"match" : {"post_type":"team-member"}}, "from" : 0, "size" : 10}
{"index" : "[INDEXNAME]", "type": "post"}
{"query" : {"match" : {"post_type": "article"}}, "from" : 0, "size" : 10}
The query executes without error, but the results only return one object, where it seems it shoul be two (one for the 10 team-members, and one for the 10 articles):
{
"responses": [
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 4,
"successful": 4,
"failed": 0
},
"hits": {
"total": 191,
"max_score": 3.825032,
"hits": [
{...}
]
}
}, // second query should be here, no?
]
}
Is my query construction wrong, or am I misunderstanding how this should work?
Upvotes: 2
Views: 836
Reputation: 7072
The format of a _msearch
request must follow the bulk API format. It must look something like this:
header\n
body\n
header\n
body\n
The header part includes which index / indices to search on, optional (mapping) types to search on, the search_type
, preference
, and routing
. The body includes the typical search body request (including the query
, aggregations
, from
, size
, and so on).
NOTE: the final line of data must end with a newline character \n
.
Make sure your query follows this format (from your code example, depending on the environment, as you've added two new lines after POST /_msearch
, your query may or may not work; you should only add one new line) . If the responses
array only has one result, then, in your case, the last query is somehow discarded - again, check its format.
Upvotes: 2
Reputation: 19
I don't see any problem actually, but you should check "Bulk API", it's similar. https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
Upvotes: 1