0
votes

Sorry for the rudimentary question.I'm developing PHP small test class that manipulates AWS Elasticsearch Service.

<?php
namespace MyCompany\Aws;

use Elasticsearch\ClientBuilder;

class ElasticsearchApi {
    private $client;

    function __construct(string $hosts='', string $username='', string $password=''){
        ... //configure $this->client
    }
    /**
     * Create index
     * @param string $index
     * @param array $body
     * @return bool
     */
    public function createIndex(string $index, array $body = []):bool{
        $request = array(
            'index' => $index,
            'body' => $body
        );
        $ret = $this->client->indices()->create($request);
        return $ret['acknowledged'];
    }
}

And call createIndex function specifying alias as described at: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

<?php
require 'vendor/autoload.php';
use MyCompany\Aws\ElasticsearchApi as ES;

$es = new ES();

$body = array('aliases'=>array(
                'actions'=>array(
                    'add'=>array(
                        'alias'=>'sample-alias',
                        'index'=>'test-index'
                    )
                )
        ));
$ret = $es->createIndex('test-index',$body);
var_dump($ret);

This operation normally ends:

PS D:\My_Documents\Proj\Elasticsearch\index-gen> php es-lib-test.php
bool(true)
PS D:\My_Documents\Proj\Elasticsearch\index-gen> 

However the generated index does not seem to have alias.

Curl command-line

What is wrong with createIndex parameter? I'm using version 7.8 of Elasticsearch on AWS.

Addendum

Based on the @Val 's suggestion, I changed the code as follows:

$body = array(
    'aliases'=>array(
        'sample-alias'=>array()
    )
);
$ret = $es->createIndex('test-index',$body);
var_dump($ret);

However I got the following exception. Are there any mistakes?

PS D:\My_Documents\Proj\Elasticsearch\index-gen> php es-lib-test.php PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"No alias is specified"}],"type":"illegal_argument_exception","reason":"No alias is specified"},"status":400} in D:\My_Documents\Proj\Elasticsearch\index-gen\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php:641 Stack trace: #0 D:\My_Documents\Proj\Elasticsearch\index-gen\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php(328): Elasticsearch\Connections\Connection->process4xxError(Array, Array, Array) #2 D:\My_Documents\Proj\Elasticsearch\index-gen\vendor\ezimuel\ringphp\src\Future\CompletedFutureValue.php(55): React\Promise\FulfilledPromise->then(Object(Closure), NULL, NULL) #3 D:\My_Documents\Proj\Elasticsearch\index-gen\vendor\ezimuel\ring in D:\My_Documents\Proj\Elasticsearch\index-gen\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 641

1

1 Answers

0
votes

Your body should simply look like this:

$body = array('aliases' => array('sample-alias' => array()));

The syntax you're using is for the _aliases endpoint not for the index creation one.