一、es安裝 php
先安裝 elasticsearch-php (這個對php的版本有要求,推薦composer安裝),對應版本傳送,安裝完成後,搭建JAVA 環境(網上搜索),環境配置完成後(須要添加幾個系統配置的環境變量),下載 對應 php版本 的elastic 對應版本傳送, elastic 下載傳送,安裝說明在下載的頁面有相關文檔,推薦下載對應的分詞【ik版本傳送】,安裝比較簡單,解壓縮,複製/剪切到 elastic 的 plugins 文件夾下(沒有就新建),重啓elastic,git
二、logstash 安裝github
這個須要的是ruby 環境,因此搭建ruby環境,而後下載安裝ruby
推薦參考範例,按照這個操做很快就能夠完成,是我找到最好的教程,composer
三、php 實現搜索dom
因爲數據尚未處理完成,就以上述案例的數據爲準,進行操做elasticsearch
1)所有搜索沒有 等價於 MySQL【select * 】post
public function search($key){ $hosts = [ // '192.168.1.1:9200', // IP + Port // '192.168.1.2', // Just IP // 'mydomain.server.com:9201', // Domain + Port // 'mydomain2.server.com', // Just Domain // 'https://localhost', // SSL to localhost // 'https://192.168.1.3:9200', // SSL to IP + Port '127.0.0.1:9200', ]; $client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts // ->setRetries(2) // ->build(); // Build the client object $params = [ 'index' => 'xc_course', 'type' => 'doc', // 'body' => ['name' => 'js'] ]; $response = $client->search($params); print_r($response); }
2)根據搜索詞進行搜索ui
public function search($key){ // $key = $request->get('key'); $hosts = [ // '192.168.1.1:9200', // IP + Port // '192.168.1.2', // Just IP // 'mydomain.server.com:9201', // Domain + Port // 'mydomain2.server.com', // Just Domain // 'https://localhost', // SSL to localhost // 'https://192.168.1.3:9200', // SSL to IP + Port '127.0.0.1:9200', ]; $client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts // ->setRetries(2) // ->build(); // Build the client object $params = [ 'index' => 'xc_course', // 'type' => 'my_type', 'body' => [ 'query' => [ 'bool' => [ 'should' => [ [ 'match' => [ 'name' => '實戰' ] ], // [ 'match' => [ 'description' => '課程' ] ], [ 'match' => [ 'description' => '實戰' ] ], ] ] ], ] ]; // $response = $client->index($params); $response = $client->search($params); print_r($response); }
3) 搜索結果高亮顯示.net
public function search($key){ // $key = $request->get('key'); $hosts = [ // '192.168.1.1:9200', // IP + Port // '192.168.1.2', // Just IP // 'mydomain.server.com:9201', // Domain + Port // 'mydomain2.server.com', // Just Domain // 'https://localhost', // SSL to localhost // 'https://192.168.1.3:9200', // SSL to IP + Port '127.0.0.1:9200', ]; $client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts // ->setRetries(2) // ->build(); // Build the client object $params = [ 'index' => 'xc_course', // 'type' => 'my_type', 'body' => [ 'query' => [ 'bool' => [ 'should' => [ [ 'match' => [ 'name' => '實戰' ] ], // [ 'match' => [ 'description' => '課程' ] ], [ 'match' => [ 'description' => '實戰' ] ], ] ] ], 'highlight' => [ 'pre_tags' => ["<em>"], // 高亮能夠本身修改 'post_tags' => ["</em>"], 'fields' => [ "name" => new \stdClass(), "description" => new \stdClass(), ] ] ] ]; $response = $client->search($params); prt($response); }
固然既然用了es不該該只是一個數據表,多表處理傳送門