*参考链接

  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. 修改配置文件

     # app/parameters.yml
     elasticsearch_host: '127.0.0.1:9200'
    
     # app/config.yml
     m6web_elasticsearch:
         default_client: search_client
         clients:
             search_client:
                 hosts:
                     - '%elasticsearch_host%'
                 headers:
                     'Accept-Encoding': ['gzip']
    
     # src/SearchBundle/Resources/config/services.yml
     services:
         search_data:
             class: SearchBundle\Services\Search
             arguments: ["@m6web_elasticsearch.client.default"]
    
     # src/SearchBundle/Resources/config/elasticsearch.yml
     search_base: 'search_base'
    
  4. 搜索服务文件路径及代码

    参考链接 ```

    src/SearchBundle/Services/Search.php

    <?php

    namespace SearchBundle\Services;

    class Search {

     private $client;
    
     private $search_base;
    
     private $search_type;
    
     /**
      * 构造
      * @param object $search_client
      */
     public function __construct($search_client)
     {
         $this->client = $search_client;
         $this->search_base = 'search_base';
         $this->search_type = 'base_data';
     }
    
     /**
      * 析构
      */
     public function __destruct()
     {
         unset($this->client);
         unset($this->search_base);
         unset($this->search_type);
     }
    
     /**
      * 创建索引(库)
      * @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' => $this->search_type,
             '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' => $this->search_type,
             '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' => $this->search_type,
             "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' => $this->search_type,
             '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服务传递
``` 
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-05-23 16:25:59

results matching ""

    No results matching ""