0

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?

Community
  • 1
  • 1
user2896120
  • 3,180
  • 4
  • 40
  • 100

1 Answers1

0

It seems you have not started web server, start ES web server to connect with php.

dkv on
  • 9
  • 2
  • Correct me if I am wrong, when you say run the ES web server do you mean running elasticsearch.bat in the command line? – user2896120 Jun 05 '17 at 13:59
  • I am running elasticsearch, however, it's still displaying the same errors. I am running this using WAMP server in localhost:81 – user2896120 Jun 05 '17 at 14:10
  • run elasticsearch.bat. it will show you port number on wich es service is running. open localhost with ES port in browser it will show you ES home page. – dkv on Jun 05 '17 at 14:13
  • The Elasticsearch localhost port number is 9200, should I make port 9200 the port for WAMP server for PHP? – user2896120 Jun 05 '17 at 14:20
  • no you should not change port of WAMP server. Is localhost:9200 working? 9200 is default port of ES. if you can see ES home page then ES is working fine and you should check connection setting to connect with PHP – dkv on Jun 05 '17 at 14:24
  • Yes, the connection to ES is working fine. If I create a normal web page in PHP with no ES, it works fine. When I add this line to execute the query: `$results = $es->search($params);` it gives me errors. – user2896120 Jun 05 '17 at 14:30
  • try $params = [ '_index' => 'pickups', '_type' => 'external', 'body' => $json ]; – dkv on Jun 05 '17 at 14:35
  • It's now saying that _index and _type are not fail parameters – user2896120 Jun 05 '17 at 14:36
  • If I refresh the browser multiple times with my original code, it sometimes gives me the correct output with no errors – user2896120 Jun 05 '17 at 14:38
  • as per doc https://github.com/elastic/elasticsearch-php#search-for-a-document require_once 'init.php'; $json = [query => [match => ["aggs" => [ "group_by_date" => [ "terms"=> [ "field" => "arrivalDate" ]]]]]]; $params = [ 'index' => 'pickups', 'type' => 'external', 'body' => $json ]; $results = $es->search($params); echo $results['hits']['total']; – dkv on Jun 05 '17 at 14:43