3

I am new to Elasticsearch so I know I'm forgetting something but I don't know what.

I ran this code:

error_reporting(E_ALL);
ini_set('display_errors', 1);

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();
$params = [
  'index' => 'my_index'
];

// Create the index
$response = $client->indices()->create($params);

But got this error:

{
  "error": {
    "root_cause": [
      {
        "type":"index_not_found_exception",
        "reason":"no such index",
        "resource.type":"index_or_alias",
        "resource.id":"elasticsearch-i.php",
        "index_uuid":"_na_",
        "index":"elasticsearch-i.php"
      }
    ],
    "type":"index_not_found_exception",
    "reason":"no such index",
    "resource.type":"index_or_alias",
    "resource.id":"elasticsearch-i.php",
    "index_uuid":"_na_",
    "index":"elasticsearch-i.php"
  },
  "status":404
}
8
  • What have you tried to debug the problem? What makes you think that it is related to PHP after all? Commented Feb 26, 2019 at 14:49
  • @NicoHaase How do I debug it? Commented Feb 26, 2019 at 14:51
  • Can you tell us what /_cat/indices/ print? Commented Feb 26, 2019 at 14:53
  • Well, you could read that error message and then check whether the requested index exists in the ES instance Commented Feb 26, 2019 at 14:53
  • @NicoHaase which index instance? Commented Feb 26, 2019 at 14:54

2 Answers 2

2

I had not yet created an index on the elasticsearch server itself.

I have to create it first before I can access it on the php-api

To do that,I run:

curl -X PUT -x  "" "http://127.0.0.1:9200"/test

on the command prompt

Sign up to request clarification or add additional context in comments.

Comments

1

There are couple of reasons not saving the index. First check your elasticsearch cluster status then try to create the index from any client.

Check Status of Elasticsearch

Health or state of the cluster

curl -X GET "localhost:9200/_cluster/health"

OR

curl -X GET "localhost:9200/_cluster/state"

OR

curl -X GET "localhost:9200/_nodes/stats"

Create Index using curl

curl -X PUT "localhost:9200/twitter"

Here is the simple code to create using php:

$client = ClientBuilder::create()->build();
$params = [
    'index' => 'twitter_2'
];

$response = $client->indices()->create($params);

For complete details check with https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_index_management_operations.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.