ElasticSearch
在laravel中安裝
composer require elasticsearch/elasticsearch
創建索引
類似於創建數據庫,創建的時候只需要指定索引名稱就可以了,同時需要使用indices()
//創建es對象 $client = ClientBuilder::create()->setHosts(config('es.host'))->build(); //設置參數(索引名稱=數據庫) $params = [ 'index'=>'house_index' ]; //創建索引 return $client->indices()->create($params);
給索引添加文檔
類似於給某個數據庫下的某張數據表添加內容
- index類似於庫名
- type類似於表名
- id類似於數據的主鍵id
- body類似於主鍵id對應的內容
//創建es對象 $client = ClientBuilder::create()->setHosts(config('es.host'))->build(); $params = [ 'index'=>'house_index',//類似於選擇給哪個數據庫添加 'type'=>'house',//類似於選擇給哪張數據表添加 'id'=>1,//類似於給某條數據指定了主鍵id 'body'=>[ 'h_name'=>'某某小區', 'h_money'=>2000 ]//類似於某個主鍵id對應的數據 ]; //添加 $res = $client->index($params); dd($res);
修改索引的文檔
相當於在某個數據庫下的某張數據表中,根據id修改某個文檔的內容
注意:因為是修改文檔的內容,所以body增加了下標doc
$client = ClientBuilder::create()->setHosts(config('es.host'))->build(); $params = [ //在某個數據庫下的某張數據表中,根據id修改某個文檔的內容 'index'=>'house_index', 'type'=>'house', 'id'=>1, 'body'=>[ //因為修改的是文檔內容,所以需要指定下標為doc 'doc'=>['h_money'=>3000] ] ]; dd($client->update($params));
刪除索引的文檔
類似於根據id刪除某個數據庫下的某張數據表的信息
$client = ClientBuilder::create()->setHosts(config('es.host'))->build(); $params = [ //在某個數據庫下的某張數據表中,根據id修改某個文檔的內容 'index'=>'house_index', 'type'=>'house', 'id'=>1 ]; dd($client->delete($params));
中文分詞器
主要適用於關鍵字搜索的時候,用來提高查詢效率的。其中number_of_shards 每個索引的主分片數,默認值是5。這個配置在索引創建后不能修改。number_of_replicas每個主分片的副本數,默認值是1。對於活動的索引庫,這個配置可以隨時修改
Elasticsearch里面有2份內容,一份是原始文檔,也就是_source字段里的內容,我們在Elasticsearch中搜索文檔,查看的文檔內容就是_source中的內容;另一份是倒排索引,倒排索引中的數據結構是倒排記錄表,記錄了詞項和文檔之間的對應關系
analyzed:字段被索引,會做分詞,可搜索。反過來,如果需要根據某個字段進搜索,index屬性就應該設置為analyzed
//生成es對象 $es = ClientBuilder::create()->setHosts(config('es.host'))->build(); //配置參數 $params = [ //在哪個庫下面進行內容的配置 'index'=>'users_index', 'body'=>[ 'settings'=>[ 'number_of_shards'=>5,//主分片數(分區) 'number_of_replicas'=>2//副本 ], 'mappings'=>[ //_source表示原文檢索 '_source'=>[ 'enabled'=>true ], //檢索的字段 'properties'=>[ //字段名稱 'truename'=>[ 'type'=>'text',//類型 'analyzer' => 'ik_max_word',//分詞器 'search_analyzer' => 'ik_max_word'//分詞器 ] ] ] ] ]; //創建索引 dd($es->indices()->create($params));
批量給索引中添加數據
$data = User::all()->toArray();
//創建es對象 $client = ClientBuilder::create()->setHosts(config('es.host'))->build(); foreach ($data as $k=>$v){ $params = [ 'index'=>'users_index',//類似於選擇給哪個數據庫添加 'type'=>'_doc', //當添加類型比較多時,值應該設置為_doc 'id'=>$v['id'],//類似於給某條數據指定了主鍵id 'body'=>$v//類似於某個主鍵id對應的數據 ]; $client->index($params); }
普通檢索(不考慮分頁的情況)
$word = $request['word']; if(empty($word)){ return ['code'=>1,'msg'=>'不能為空','data'=>null]; } $client = ClientBuilder::create()->setHosts(config('es.host'))->build();//創建es實例 //設置查詢的條件 $params = [ 'index' => 'users_index',//索引(類似於庫) 'type' => '_doc', 'body' => [ //查詢內容 'query' => [ 'match' => [//匹配 'truename' => $word//匹配字段 ] ] ] ]; $res = $client->search($params); print_r($res);
分頁檢索
$word = input('word');//接收關鍵字 $page = input('page',1);//接收當前頁(如果沒接收到,默認是1) $size = 5;//每頁顯示條數 $limit = ($page-1)*$size;//偏移量 $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();//創建es實例 //設置查詢的條件 $params = [ 'index' => 'goodslists_index',//索引(類似於庫) 'type' => '_doc',//根據需要決定寫不寫 'body' => [ //查詢內容 'query' => [ 'match' => [//匹配 'goods_name' => $word//匹配字段 ] ], 'highlight' => [//高亮 'pre_tags' => ["<em style='color: red'>"],//樣式自己寫 'post_tags' => ["</em>"], 'fields' => [ "goods_name" => new \stdClass() ] ] ] ]; //分頁限制(可以直接寫在$params里面) $params["size"] = $size;//每頁顯示條數 $params["from"] = $limit;//偏移量 $results = $client->search($params);//es搜索 foreach ($results['hits']['hits'] as $k=>$v){ $results['hits']['hits'][$k]['_source']['goods_name'] = $v['highlight']['goods_name'][0]; } $data = array_column($results['hits']['hits'],'_source'); $arr['data'] = $data;//數據 $arr['page'] = $page;//當前頁 $arr['total'] = $results['hits']['total']['value'];//總條數 $arr['last_page'] = ceil($results['hits']['total']['value']/$size);//總頁數 print_r($arr);//剩下的就是前端展示的事情了