Use ES batch query
Es batch query can use mget and msearch. Where mget requires us to know its ID, we can specify a different index, or we can specify the return value source. Msearch can perform a batch lookup through field queries.
_mget
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1"
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2"
}
]
}
Copy the code
This is the official example. If we know index, type, ID then we can use MGET to get bulk data. You can also specify the returned data using source.
_mget API
_msearch
GET twitter/_msearch
{}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
{}
{"query" : {"match_all" : {}}}
{"index" : "twitter2"}
{"query" : {"match_all" : {}}}
Copy the code
In _msearch, the request format is similar to bulk. Querying a piece of data requires two objects, the first set index and type, and the second set the query statement. The query statement is the same as search. If you’re just looking for an index, you can put an index in the URL, so that if you’re looking for an index, you can use an empty object. Ps: There must be two objects, otherwise the next query statement will be the index statement of index. The return value of _msearch is an object
{
"responses":[{Object},{Object}]
}
Copy the code
Each result corresponds to the pair of queries above. If a query statement has an error, only the Object with the error is reported.
Ps: Use kibana query without format query statement. It’s going to be unrecognizable. Because the _msearch format is header\n body\n header\n body\n format followed by \n, es cannot be recognized