ElasticSearch全文搜索引擎整合thinkphp

Posted 企久数据

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 30Spring Boot整合全文搜索引擎Elasticsearch

Spring Boot整合Elasticsearch

一篇解决Springboot 整合 Elasticsearch

一篇解决Springboot 整合 Elasticsearch

八.全文检索ElasticSearch经典入门-深入理解ElasticSearch核心原理

Elasticsearch+Logstash+Kibana日志采集服务搭建并简单整合应用