public functionindex(){$es= ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();$params=['index'=>'movies',//類似于庫名'body'=>['settings'=>['number_of_shards'=>3,'number_of_replicas'=>2],'mappings'=>['_source'=>['enabled'=>true],'properties'=>['title'=>['type'=>'text',"analyzer"=>"ik_max_word","search_analyzer"=>"ik_max_word"]]]]];//執行創建$r=$es->indices()->create($params);dump($r);}
二、索引中新增數據
public functionadd_movies_data(){$data= Movies::select();//查詢數據$res=(new Collection($data))->toArray();$es= ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();foreach ($res as $k=>$v){$params=['index'=>'movies',//索引'type'=>'_doc',//表(額外需要注意的,這里是固定的寫法)'id'=>$v['id'],//主鍵'body'=>$v//數據];$r=$es->index($params);}echo'success';}
三、實現在Elasticsearch中數據的搜索(使得搜索的關鍵字高亮)
public functionsearch_movies(){$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'=>'movies',//索引(類似于庫)//'type'=>'_doc','body'=>[//查詢內容'query'=>['match'=>[//匹配'title'=>$word//匹配字段]],'highlight'=>[//高亮'pre_tags'=>["<em style='color: red'>"],//樣式自己寫'post_tags'=>["</em>"],'fields'=>["title"=> new \stdClass()]]]];//分頁限制$params["size"]=$size;//每頁顯示條數$params["from"]=$limit;//偏移量$results=$client->search($params);//es搜索foreach ($results['hits']['hits'] as $k=>$v){$results['hits']['hits'][$k]['_source']['title']=$v['highlight']['title'][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);//剩下的就是前端展示的事情了}
前端頁面展示:
elasticsearch索引展示:
我將代碼復制到此處: Controller中的代碼:
public functionfirst(){$es= ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();$params=['index'=>'movies01',//類似于庫名'body'=>['settings'=>['number_of_shards'=>3,'number_of_replicas'=>2],'mappings'=>['_source'=>['enabled'=>true],'properties'=>['title'=>['type'=>'text',"analyzer"=>"ik_max_word","search_analyzer"=>"ik_max_word"],'goods_name'=>[//goods_name 文檔名稱 相當于mysql中的字段'type'=>'text',//text文本'analyzer'=>'ik_max_word',//analyzer 指定分詞器為ik'search_analyzer'=>'ik_max_word',//指定搜索時的分詞器],'goods_logo'=>['type'=>'text'],'goods_price'=>['type'=>'text',//text文本],'goods_desc'=>['type'=>'text',//text文本'analyzer'=>'ik_max_word',//analyzer 指定分詞器為ik'search_analyzer'=>'ik_max_word'//指定搜索時的分詞器],'goods_remark'=>['type'=>'text',//text文本'analyzer'=>'ik_max_word',//analyzer 指定分詞器為ik'search_analyzer'=>'ik_max_word'//指定搜索時的分詞器],"news_source"=>["type"=>"text","fielddata"=>true]]]]];//執行創建$r=$es->indices()->create($params);dump($r);}public functionadd_movies_data(){$data= Goods::select();//查詢數據$res=(new Collection($data))->toArray();$es= ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();foreach ($res as $k=>$v){$params=['index'=>'movies01',//索引'type'=>'_doc',//表(額外需要注意的,這里是固定的寫法)'id'=>$v['id'],//主鍵'body'=>$v//數據];$r=$es->index($params);}echo'success';}public function search_movies($word){header("Access-Control-Allow-Origin:*");header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");// $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'=>'movies01',//索引(類似于庫)//'type'=>'_doc','body'=>[//查詢內容'query'=>['match'=>[//匹配'title'=>$word//匹配字段]],'highlight'=>[//高亮'pre_tags'=>["<em style='color: red'>"],//樣式自己寫'post_tags'=>["</em>"],'fields'=>["title"=> new \stdClass()]]]];//分頁限制$params["size"]=$size;//每頁顯示條數$params["from"]=$limit;//偏移量$results=$client->search($params)->asArray();//es搜索foreach ($results['hits']['hits'] as $k=>$v){$results['hits']['hits'][$k]['_source']['title']=$v['highlight']['title'][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);//剩下的就是前端展示的事情了return json($arr);
// return view('index/index',compact('arr',$arr));}