Hi there,
Sorry for asking, but can anyone tell me if there's a match_all method available to just get 'latest' 10 items in index?
Something like this:
POST /documents/_search
{
"query": {
"match_all": {}
}
}
Yep.
$client = new Elasticsearch\Client();
$params = [
'index' => 'documents',
'body' => [
'match_all' => []
]
];
$results = $client->search($params);
Note, you can also just execute a search with no request body, and it will be the same as a search with match_all:
$client = new Elasticsearch\Client();
$params = [
'index' => 'documents',
];
$results = $client->search($params);
This doesn't necessarily give you the 10 latest results though. It will just give you ten random results. If you want the ten latest, you need to sort the results based on some value. Like this:
$client = new Elasticsearch\Client();
$params = [
'index' => 'documents',
'sort' => [
['my_timestamp' => ['order' => 'desc'] ]
]
];
$results = $client->search($params);
awesome! thanks!
No problem! Let me know if you have any more questions :)
works for me...
$searchParams = array();
$searchParams['index'] = 'YOUR_INDEX';
$searchParams['type'] = 'YOUR_TYPE';
$searchParams['body']['query']['match_all'] = array();
If you are getting the error "[match_all] query malformed, no start_object after query name":
$client = new Elasticsearch\Client();
$params = [
'index' => 'documents',
'body' => [
'match_all' => (object)[] // this cast is necessary!
]
];
$results = $client->search($params);
If you are getting the error "[match_all] query malformed, no start_object after query name":
$client = new Elasticsearch\Client(); $params = [ 'index' => 'documents', 'body' => [ 'query' => [ 'match_all' => (object)[] // this cast is necessary! ] ] ]; $results = $client->search($params);
Excellent!! This works for me!!!
Most helpful comment
If you are getting the error "[match_all] query malformed, no start_object after query name":