【问题标题】:Creating index with alias使用别名创建索引
【发布时间】:2021-03-19 19:57:22
【问题描述】:

很抱歉这个基本问题。我正在开发 PHP 小测试类来操作 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'];
    }
}

并调用 createIndex 函数指定别名,如下所述: 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);

此操作正常结束:

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 上使用 7.8 版的 Elasticsearch。

附录

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

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

但是我得到了以下异常。有错误吗?

PS D:\My_Documents\Proj\Elasticsearch\index-gen> php es-lib-test.php PHP 致命错误:未捕获 Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","re​​ason":"未指定别名"}],"type": "illegal_argument_exception","re​​ason":"未指定别名"},"status":400} 在 D:\My_Documents\Proj\Elasticsearch\index-gen\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection 中。 php:641 堆栈跟踪: #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 在 D:\My_Documents\Proj\Elasticsearch\index-gen\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php 上第 641 行

【问题讨论】:

    标签: amazon-web-services elasticsearch


    【解决方案1】:

    你的身体应该是这样的:

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

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

    【讨论】:

    • 您能看一下建议解决方案的结果吗?我得到了BadRequest400Exception。由于我对 ES 很陌生,所以可能会有错误。
    • 你看我写成附录的结果了吗?
    • 我已经看到了错误,但这很奇怪,因为别名 sample-alias 已正确指定...仍在尝试了解您的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多