<?php


namespace Api\PhpUtils\Elastic;

use Api\PhpUtils\Elastic\Manager\DocumentManager;
use Api\PhpUtils\Elastic\Manager\IndexManager;
use Elasticsearch\ClientBuilder;

final class ElasticUtil
{
    use IndexManager, DocumentManager;

    /**
     * @var ElasticUtil
     */
    private static $instance;

    /**
     * ElasticUtil constructor.
     * @param $hosts
     */
    public function __construct()
    {
        $hostStr = config("elasticsearch", "goods.host");
        $hosts = explode(",", $hostStr);
        
        $this->client = ClientBuilder::create()->setHosts($hosts)->build();
    }

    /**
     * @param $hosts
     * @return ElasticUtil
     */
    public static function getInstance(): ElasticUtil
    {
        if (empty(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}