search参考链接

  1. 安装elasticsearch-bundle

     # 修改composer.json
     "require": {
         ...
         "m6web/elasticsearch-bundle": "^1.6"
     }
    
     composer install
    
  2. 注册

     # app/AppKernel.php
     <?php
     public function registerBundles()
     {
         $bundles = [
             new M6Web\Bundle\ElasticsearchBundle\M6WebElasticsearchBundle(),
         ];
     }
    
  3. 修改配置文件

     # parameters.yml
     elasticsearch_host: 127.0.0.1
     elasticsearch_port: 9200
     elasticsearch_base: search_base
    
     # config.yml
     m6web_elasticsearch:
         default_client: client1
         clients:
             client1:
                 hosts:
                     - '%elasticsearch_host%:%elasticsearch_port%'
                 headers:
                     Accept-Encoding: ['gzip']
    
     # services.yml
     services:
         search_data:
             class: SearchBundle\Services\Search
             arguments: ["@m6web_elasticsearch.client.default", "%elasticsearch_base%"]
    
  4. 服务文件代码参考链接

    <?php
    
    namespace SearchBundle\Services;
    
    class Search
    {
        private $client;
        private $search_base;
    
        public function __construct($client, $search_base)
        {
            $this->client = $client;
            $this->search_base = $search_base;
        }
    
        public function __destruct()
        {
            unset($this->client);
            unset($this->search_base);
        }
    
        /**
         * 创建索引(库)
         * @return true
         */
        public function createIndex()
        {
            $params['index'] = $this->search_base;
            if(!$this->client->indices()->exists($params)){
                $this->client->indices()->create($params);
            }
            return true;
        }
    
        /**
         * 删除索引(库)
         * @return true
         */
        public function deleteIndex()
        {
            $params['index'] = $this->search_base;
            //如果存在则删除
            if($this->client->indices()->exists($params)){
                $this->client->indices()->delete($params);
            }
            return true;
        }
        /**
         * 搜索数据
         * @param  string $keywords
         * @param  int $begin
         * @param  int $size
         * @return $result
         */
        public function searchData($keywords, $begin = 0, $size = 1)
        {
            $option = [
                'index' => $this->search_base,
                'type' => 'base_data',
                'q' => $keywords,
                "body" => [
                    "from" => $begin,
                    "size" => $size
                ]
            ];
            $result = $this->search->search($option);
            return $result;
        }
        /**
         * 获取指定数据
         * @param  string $id
         * @return $result
         */
        public function getData($id)
        {
            $option = [
                'index' => $this->search_base,
                'type' => 'base_data',
                'id' => $id,
                "body" => []
            ];
            $result = $this->search->index($option);
            return $result;
        }
        /**
         * 添加/更新数据到搜索
         * @param array $base_data
         * @return $result
         */
        public function addData($base_data)
        {
            $list_data = [
                'index' => $this->search_base,
                'type' => 'base_data',
                "id" => $base_data["key1"],
                'body' => [
                    'key1' => $base_data["key1"],
                    'key2' => $base_data["key2"],
                    'key3' => $base_data["key3"],
                    'key4' => $base_data["key4"],
                    'key5' => $base_data["key5"],
                    "key6" => $base_data["key6"],
                    "key7" => $base_data["key7"]
                ]
            ];
            $result = $this->client->index($list_data);
            return $result;
        }
    
        /**
         * 从搜索删除数据(先判断存在性)
         * @param  array $base_data
         * @return $result
         */
        public function deleteData($base_data)
        {
            $list_data = [
                'index' => $this->search_base,
                'type' => 'base_data',
                'id' => $base_data["key1"]
            ];
            $result = $this->client->delete($list_data);
            return $result;
        }
    }
    
  5. 调用搜索服务 ```

    控制器调用

    $search_data = $this->get('search_data');

    //添加数据到查询 $base_data = [

     'key1' => 123456,
     'key2' => '数据1',
     'key3' => '数据2',
     'key4' => '呵呵1',
     'key5' => '呵呵2',
     "key6" => '参数1',
     "key7" => '参数2'
    

    ]; $search_data->addData($base_data);

    //从查询删除数据 $base_data = [

     'key1' => 123456
    

    ]; $search_data->deleteData($base_data);

    //从查询更新数据 $base_data = [

     'key1' => 123456,
     'key2' => '数据1',
     'key3' => '数据2',
     'key4' => '呵呵3',
     'key5' => '呵呵4',
     "key6" => '参数5',
     "key7" => '参数6'
    

    ]; $search_data->updateData($base_data);

    //带分页查询 $search_data->searchData('数', 0, 10);

# 从Command调用
$search_data = $this->getContainer()->get('search_data');

//添加数据到查询
$base_data = [
    'key1' => 123456,
    'key2' => '数据1',
    'key3' => '数据2',
    'key4' => '呵呵1',
    'key5' => '呵呵2',
    "key6" => '参数1',
    "key7" => '参数2'
];
$search_data->addData($base_data);

//从查询删除数据
$base_data = [
    'key1' => 123456
];
$search_data->deleteData($base_data);

//从查询更新数据
$base_data = [
    'key1' => 123456,
    'key2' => '数据1',
    'key3' => '数据2',
    'key4' => '呵呵3',
    'key5' => '呵呵4',
    "key6" => '参数5',
    "key7" => '参数6'
];
$search_data->updateData($base_data);

//带分页查询
$search_data->searchData('数', 0, 10);

# 从Service调用
//参考Symfony服务传递
``` 

results matching ""

    No results matching ""