Elasticsearch 安装及PHP使用
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);
输出结果:
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29