Errors upon searching in Elasticsearch using PHP

I am trying to search through my index using PHP. I have downloaded the vendor files and I have built the connection successfully. However, I am receiving some errors. These errors don't always occur though. If I refresh the browser a couple of times, I will get the correct output. Here's my code:

require_once 'init.php';

$json = '{"aggs": { "group_by_date": { "terms": { "field": "arrivalDate" } } } }';

$params = [
    'index' => 'pickups',
    'type' => 'external',
    'body' => $json 
    ];

$results = $es->search($params);

echo $results['hits']['total'];

The init.php is the connection file to Elasticsearch. There are 2 different outputs that may occur upon refreshing the screen. The first output is the correct output. The other is just errors. Here are the errors:

Fatal error: Uncaught Elasticsearch\Common\Exceptions\Missing404Exception: {"statusCode":404,"error":"Not Found"} in C:\wamp64\www\DataAggregation\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 602

Elasticsearch\Common\Exceptions\Missing404Exception: {"statusCode":404,"error":"Not Found"} in C:\wamp64\www\DataAggregation\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 602

Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: in C:\wamp64\www\DataAggregation\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 610

What are the cause of these errors and how can I fix them?

Is it possible the index is being deleted or doesn't exist when the search happens?

Also, how did you install the ES-PHP library? It's a composer library, meaning you need to install with Composer then include the generated autoloader that composer creates. Can you show the contents of init.php?

Well when I keep on refreshing, the correct output is displayed sometimes with no errors. I did install the ES-PHP library with composer. Here's my init code:
require_once 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;

/*Build connection*/
$hosts = [
'XXXX:0000',                // IP + Port
'XXXX',                     // Just IP
'XXXX:0000',                // Domain + Port
'XXXX.XXXX.COM',             // Just Domain
'https://XXXX',             // SSL to localhost
'https://XXXX.XXXX.COM:0000'  // SSL to IP + Port
];

$es = ClientBuilder::create()    // Instantiate a new ClientBuilder
        ->setHosts($hosts)      // Set the hosts
        ->build();             // Build the client object

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.