ElasticSearch需要基於Java下運行,故首先保證已安裝java
此處 mac 使用brew
brew cask install java
安裝ElasticSearch
brew 安裝
brew install ElasticSearch
終端輸入elasticsearch
運行, 瀏覽器訪問127.0.0.1:9200,出現下圖,OK安裝完成
composer 安裝引用 elasticsearch
composer.json require中 加入
"elasticsearch/elasticsearch": "~6.0"
PHP 實現elasticsearch基本操作類
<?php use Elasticsearch\ClientBuilder; class ES { private $client; public function __construct(){ $ci = get_instance(); $params = array( '127.0.0.1:9200' ); $this->client = ClientBuilder::create()->setHosts($params)->build(); } //創建索引 function createIndex($index_name = 'aki'){ $params = [ 'index' => $index_name, 'body' => [ 'settings' => [ 'number_of_shards' => 5, 'number_of_replicas' => 0 ] ] ]; return $this->client->indices()->create($params); } // 刪除索引 public function delIndex($index_name = 'aki') { $params = ['index' => $index_name]; return $this->client->indices()->delete($params); } // 添加文檔 public function addDoc($id,$doc,$index_name = 'aki',$type_name = 'form') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => $doc ]; return $this->client->index($params); } // 判斷文檔存在 public function existsDoc($id = 1,$index_name = 'aki',$type_name = 'form') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; return $this->client->exists($params); } // 獲取文檔 public function getDoc($id = 1,$index_name = 'aki',$type_name = 'form') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; return $this->client->get($params); } // 更新文檔 public function updateDoc($id,$doc,$index_name = 'aki',$type_name = 'form') { // 可以靈活添加新字段,最好不要亂添加 $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => [ 'doc' =>$doc ] ]; return $this->client->update($params); } // 刪除文檔 public function delDoc($id,$index_name = 'aki',$type_name = 'form') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; return $this->client->delete($params); }// 查詢文檔 (分頁,排序,權重,過濾) public function searchDoc($keywords = "競賽",$index_name = "aki",$type_name = "form",$sort = [],$from = 0,$size = 10) { $params = [ 'index' => $index_name, 'type' => $type_name, 'body' => [ 'query' => [ 'bool' => [ 'should' => [ [ 'match' => [ 'title' => [ 'query' => $keywords, 'boost' => 3, // 權重大 ]]], [ 'match' => [ 'content' => [ 'query' => $keywords, 'boost' => 2, ]]], ], ], ], 'sort' => ['price'=>['order'=>'desc']] , 'from' => $from, 'size' => $size ] ]; $results = $this->client->search($params); // $maxScore = $results['hits']['max_score']; // $score = $results['hits']['hits'][0]['_score']; // $doc = $results['hits']['hits'][0]['_source']; return $results['hits']['hits']; } }
測試使用:
ll('es');//加載ES類 $r = $this->es->delIndex(); $r = $this->es->createIndex(); print_r($r); $docs = []; $docs[] = ['id'=>1,'title'=>'李白','content'=>'酒仙刺客','price'=>100]; $docs[] = ['id'=>2,'title'=>'孫悟空','content'=>'騰雲駕霧的辜負了紫霞的刺客。','price'=>300]; $docs[] = ['id'=>3,'title'=>'胡歌','content'=>'盡職勵志,不錯哦。','price'=>1000000]; $docs[] = ['id'=>4,'title'=>'王者榮耀','content'=>'游戲就玩王者榮耀。','price'=>998]; $docs[] = ['id'=>5,'title'=>'魯班','content'=>'小短腿,誰都想滅。','price'=>98]; $docs[] = ['id'=>6,'title'=>'妲己','content'=>'禍國殃民。','price'=>998]; $docs[] = ['id'=>7,'title'=>'呂布','content'=>'方天畫戟,后手放大','price'=>2998]; $docs[] = ['id'=>8,'title'=>'水晶','content'=>'保護我方水晶,進攻地方水晶。','price'=>19999]; foreach ($docs as $k => $v) { $this->es->addDoc($v['id'],$v); }
$r = $this->es->searchDoc("李白 歌 水");
echo "<pre>";
print_r($r);
輸出結果:
linux
java jdk: yum install java-1.8.0-openjdk-devel.x86_64
安裝 es https://www.jianshu.com/p/1bf398735dd4
注意修改 /etc/security/limits.conf,追加以下內容;
* soft nofile 65536
* hard nofile 65536 后退出終端重新登錄
守護進程啟動 加 -d