首页 > 代码库 > ElasticSearch全文搜索引擎整合thinkphp

ElasticSearch全文搜索引擎整合thinkphp

下载文件解压到
ThinkPHP\Library\Vendor\

  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class EcController extends Controller
  5. {
  6.     public function _initialize()
  7.     {
  8.         Vendor(‘Elasticsearch.autoload‘);
  9.         $params[‘hosts‘] = array(
  10.             ‘127.0.0.1:9200‘
  11.         );
  12.         $this->client = new \Elasticsearch\Client($params);
  13.     }
  14.     public function create_index()
  15.     {
  16.         $indexParams[‘index‘] = ‘my_index‘;
  17.         $indexParams[‘type‘] = ‘my_index‘;
  18.         $indexParams[‘body‘][‘settings‘][‘number_of_shards‘] = 2;
  19.         $indexParams[‘body‘][‘settings‘][‘number_of_replicas‘] = 0;
  20.         $this->client->create($indexParams);
  21.     }
  22.     public function add_document()
  23.     {
  24.         $params = array();
  25.         $params[‘body‘] = array(
  26.             ‘testField‘ => ‘dfdsfdsf‘
  27.         );
  28.         $params[‘index‘] = ‘my_index‘;
  29.         $params[‘type‘] = ‘my_index‘;
  30.         $params[‘id‘] = ‘w1231313‘;
  31.         $ret = $this->client->index($params);
  32.     }
  33.     public function delete_index()
  34.     {
  35.         $deleteParams[‘index‘] = ‘my_index‘;
  36.         $this->client->indices()->delete($deleteParams);
  37.     }
  38.     public function delete_document()
  39.     {
  40.         $deleteParams = array();
  41.         $deleteParams[‘index‘] = ‘my_index‘;
  42.         $deleteParams[‘type‘] = ‘my_index‘;
  43.         $deleteParams[‘id‘] = ‘AU4Kmmj-WOmOrmyOj2qf‘;
  44.         $retDelete = $this->client->delete($deleteParams);
  45.     }
  46.     public function update_document()
  47.     {
  48.         $updateParams = array();
  49.         $updateParams[‘index‘] = ‘my_index‘;
  50.         $updateParams[‘type‘] = ‘my_index‘;
  51.         $updateParams[‘id‘] = ‘my_id‘;
  52.         $updateParams[‘body‘][‘doc‘][‘asas‘]  = ‘111111‘;
  53.        $response = $this->client->update($updateParams);
  54.          
  55.     }
  56.     public function search()
  57.     {
  58.         $searchParams[‘index‘] = ‘my_index‘;
  59.         $searchParams[‘type‘] = ‘my_index‘;
  60.         $searchParams[‘from‘] = 0;
  61.         $searchParams[‘size‘] = 100;
  62.         $searchParams[‘sort‘] = array(
  63.             ‘_score‘ => array(
  64.                 ‘order‘ => ‘desc‘
  65.             )
  66.         );
  67.         // $searchParams[‘body‘][‘query‘][‘match‘][‘testField‘] = ‘abc‘;
  68.         $retDoc = $this->client->search($searchParams);
  69.         print_r($retDoc);
  70.     }
  71.     public function get_document()
  72.     {
  73.         $getParams = array();
  74.         $getParams[‘index‘] = ‘my_index‘;
  75.         $getParams[‘type‘] = ‘my_index‘;
  76.         $getParams[‘id‘] = ‘AU4Kn-knWOmOrmyOj2qg‘;
  77.         $retDoc = $this->client->get($getParams);
  78.         print_r($retDoc);
  79.     }
  80. }
  81. ?>

技术分享 Elasticsearch.zip

ElasticSearch全文搜索引擎整合thinkphp