<?php


namespace Api\PhpUtils\Elastic\Manager;

use Elasticsearch\Client;

trait IndexManager
{
    /**
     * @var Client
     */
    protected $client;

    /**
     * 创建一个索引
     * @param $index
     * @param $data
     * @return array
     */
    public function createIndex($index, $data): array
    {
        $params = [
            'index' => $index,
            'body' => $data
        ];

        return $this->client->indices()->create($params);
    }

    /**
     * 删除一个索引
     * @param $index
     * @return array
     */
    public function deleteIndex($index): array
    {
        $params = [
            'index' => $index,
        ];

        return $this->client->indices()->delete($params);
    }

    /**
     * 获取一个或多个索引的当前配置参数
     * @param array $indices
     * @return array
     */
    public function getSettings(array $indices): array
    {
        $params = [
            'index' => $indices
        ];
        return $this->client->indices()->getSettings($params);
    }

    /**
     * 更改索引的配置参数
     * @param $index
     * @param $data
     * @return array
     */
    public function putSettings($index, $data): array
    {
        $params = [
            'index' => $index,
            'body' => $data
        ];

        return $this->client->indices()->putSettings($params);
    }

    /**
     * 获取索引和类型的映射细节
     * @param $index
     * @return array
     */
    public function getMapping($index): array
    {
        $params = [
            'index' => $index,
        ];
        return $this->client->indices()->getMapping($params);
    }

    /**
     * 更改或增加一个索引的映射
     * @param $index
     * @param $data
     * @return array
     */
    public function putMapping($index, $data): array
    {
        $params = [
            'index' => $index,
            'body' => $data
        ];

        return $this->client->indices()->putMapping($params);
    }
}