提问者:小点点

使用别名创建索引


抱歉,我的问题很简单。我正在开发PHP小型测试类来操作AWSElasticsearch 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'];
    }
}

并调用createIndex函数指定别名,如以下所述:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.htmlhttps://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);

此操作通常结束:

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

但是生成的索引似乎没有别名。

createIndex参数有什么问题?我在AWS上使用Elasticsearch 7.8版。

根据@Val的建议,我将代码更改如下:

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

但是我得到了以下例外。有什么错误吗?

PSD:\My_Documents\Proj\Elasticsearch\index-gen


共1个答案

匿名用户

你的身体应该看起来像这样:

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

您使用的语法用于_aliases终结点,而不是索引创建终结点。