<?php


namespace App\Services\demo;


use Api\PhpUtils\Elastic\ElasticUtil;

class ElasticService
{
    public function createIndex(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $data = [
            'mappings' => [
                'properties' => [
                    'test_field' => [
                        'type' => 'text',
                        "analyzer" => "ik_max_word",
                        "search_analyzer" => "ik_smart"
                    ],
                ]
            ]
        ];
        $res = $elasticClient->createIndex($index, $data);
        var_export($res);
    }

    public function deleteIndex(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $res = $elasticClient->deleteIndex($index);
        var_export($res);
    }

    public function getSettings(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $res = $elasticClient->getSettings([$index]);
        var_export($res);
    }

    public function putSettings(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $data = [
            'settings' => [
                'number_of_replicas' => 0,
                'refresh_interval' => -1
            ]
        ];
        $res = $elasticClient->putSettings($index, $data);
        var_export($res);
    }

    public function getMapping(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $res = $elasticClient->getMapping($index);
        var_export($res);
    }

    public function putMapping(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $data = [
            'properties' => [
                'second_field' => [
                    'type' => 'text',
                    "analyzer" => "ik_max_word",
                    "search_analyzer" => "ik_smart"
                ],
            ]
        ];
        $res = $elasticClient->putMapping($index, $data);
        var_export($res);
    }

    public function indexDoc(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $data = ['test_field' => '美国留给伊拉克的是个烂摊子吗'];
        $res = $elasticClient->indexDoc($index, $data, 'test_id');
        var_export($res);
    }

    public function getDoc(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $res = $elasticClient->getDoc($index, 'test_id');
        var_export($res);
    }

    public function updateDoc(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $data = [
            'doc' => [
                'new_field' => 'abc'
            ]
        ];
        $res = $elasticClient->updateDoc($index, 'test_id', $data);
        var_export($res);
    }

    public function deleteDoc(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $res = $elasticClient->deleteDoc($index, 'test_id');
        var_export($res);
    }

    public function bulk(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $data = [
            ['index' => ['_index' => $index]],
            ['test_field' => '公安部：各地校车将享最高路权'],
            ['index' => ['_index' => $index]],
            ['test_field' => '中韩渔警冲突调查：韩警平均每天扣1艘中国渔船'],
            ['index' => ['_index' => $index]],
            ['test_field' => '中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首'],
        ];
        $res = $elasticClient->bulkIndexDoc($data);
        var_export($res);
    }

    public function searchDoc(): void
    {
        $elasticClient = ElasticUtil::getInstance();
        $index = 'demo';

        $data = [
            'query' => [
                'match' => [
                    'test_field' => '中国'
                ]
            ],
            'highlight' => [
                'pre_tags' => ['<tag1>', '<tag2>'],
                'post_tags' => ['</tag1>', '</tag2>'],
                "fields" => [
                    'test_field' => new \stdClass()
                ]
            ]
        ];
        $res = $elasticClient->searchDoc($index, $data);
        var_export($res);
    }
}