ScipioAfricanus ScipioAfricanus - 5 months ago 10
PHP Question

PHP API to get Documents

I am using a PHP API to post documents into elastic but I need to retrieve the last document posted according to it's timestamp.

The sense query I am currently using is this:

GET index-*/type/_search
{
"query": {
"match_all": {}
},
"size": 1,
"sort": [
{
"timestamp": {
"order": "desc"
}
}
]
}


I have translated it to my PHP api

$params = [
'index' => 'index-*',
'type' => 'type',
'custom' => [
'query'=> [
'match_all'=> []
],
'size'=> 1,
'sort'=> [
[
'timestamp'=> [
'order'=> 'desc'
]
]
]
]
];
$response = $client->get($params);


But it unfortunately keeps throwing errors and asking for 'id' but my ids are eleastic generated. I cant do it any other way. Is there a way around thsi? Thanks

Val Val
Answer

You need to use the search method:

$params = [
    'index' => 'index-*',
    'type' => 'type',
    'body' => [
        'query'=> [
            'match_all'=> []
        ],
        'size'=> 1,
        'sort'=> [
           [
            'timestamp'=> [
            'order'=> 'desc'
                ]
            ]
        ]
    ]
];
$response = $client->search($params);
Comments