# Errors upon searching in Elasticsearch using PHP

**URL:** <https://discuss.elastic.co/t/errors-upon-searching-in-elasticsearch-using-php/88292>\
**Category:** Elasticsearch\
**Created:** [June 5, 2017, 3:51pm UTC](https://discuss.elastic.co/t/errors-upon-searching-in-elasticsearch-using-php/88292 "2017-06-05T15:51:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SamA](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@SamA](https://discuss.elastic.co/u/SamA)\
**Post date:** [June 5, 2017, 3:51pm UTC](https://discuss.elastic.co/t/errors-upon-searching-in-elasticsearch-using-php/88292/1 "2017-06-05T15:51:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [June 5, 2017, 7:36pm UTC](https://discuss.elastic.co/t/errors-upon-searching-in-elasticsearch-using-php/88292/2 "2017-06-05T19:36:17Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![SamA](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@SamA](https://discuss.elastic.co/u/SamA)\
**Post date:** [June 5, 2017, 7:40pm UTC](https://discuss.elastic.co/t/errors-upon-searching-in-elasticsearch-using-php/88292/3 "2017-06-05T19:40:34Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 3, 2017, 7:40pm UTC](https://discuss.elastic.co/t/errors-upon-searching-in-elasticsearch-using-php/88292/4 "2017-07-03T19:40:36Z")

</div>

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