1PHP中使用Elasticsearch 2 3composer require elasticsearch/elasticsearch 4会自动加载合适的版本!我的php是5.6的,它会自动加载5.3的elasticsearch版本! 5 6Using version ^5.3 for elasticsearch/elasticsearch 7./composer.json has been updated 8Loading composer repositories with package information 9Updating dependencies (including require-dev) 10Package operations: 4 installs, 0 updates, 0 removals 11 - Installing react/promise (v2.7.0): Downloading (100%) 12 - Installing guzzlehttp/streams (3.0.0): Downloading (100%) 13 - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%) 14 - Installing elasticsearch/elasticsearch (v5.3.2): Downloading (100%) 15Writing lock file 16Generating autoload files 17简单使用 18 19<?php 20 21class MyElasticSearch 22{ 23 private $es; 24 // 构造函数 25 public function __construct() 26 { 27 include('../vendor/autoload.php'); 28 $params = array( 29 '127.0.0.1:9200' 30 ); 31 $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build(); 32 } 33 34 public function search() { 35 $params = [ 36 'index' => 'megacorp', 37 'type' => 'employee', 38 'body' => [ 39 'query' => [ 40 'constant_score' => [ //非评分模式执行 41 'filter' => [ //过滤器,不会计算相关度,速度快 42 'term' => [ //精确查找,不支持多个条件 43 'about' => '谭' 44 ] 45 ] 46 47 ] 48 ] 49 ] 50 ]; 51 52 $res = $this->es->search($params); 53 54 print_r($res); 55 } 56} 57<?php 58require "./MyElasticSearch.php"; 59 60$es = new MyElasticSearch(); 61 62$es->search(); 63执行结果 64 65Array 66( 67 [took] => 2 68 [timed_out] => 69 [_shards] => Array 70 ( 71 [total] => 5 72 [successful] => 5 73 [skipped] => 0 74 [failed] => 0 75 ) 76 77 [hits] => Array 78 ( 79 [total] => 1 80 [max_score] => 1 81 [hits] => Array 82 ( 83 [0] => Array 84 ( 85 [_index] => megacorp 86 [_type] => employee 87 [_id] => 3 88 [_score] => 1 89 [_source] => Array 90 ( 91 [first_name] => 李 92 [last_name] => 四 93 [age] => 24 94 [about] => 一个PHP程序员,热爱编程,谭康很帅,充满激情。 95 [interests] => Array 96 ( 97 [0] => 英雄联盟 98 ) 99 100 ) 101 102 ) 103 104 ) 105 106 ) 107 108) 109下面是官方的一些样例整合, 110 111<?php 112 113require '../vendor/autoload.php'; 114use Elasticsearch\ClientBuilder; 115class MyElasticSearch 116{ 117 private $client; 118 // 构造函数 119 public function __construct() 120 { 121 $params = array( 122 '127.0.0.1:9200' 123 ); 124 $this->client = ClientBuilder::create()->setHosts($params)->build(); 125 } 126 127 // 创建索引 128 public function create_index($index_name = 'test_ik') { // 只能创建一次 129 $params = [ 130 'index' => $index_name, 131 'body' => [ 132 'settings' => [ 133 'number_of_shards' => 5, 134 'number_of_replicas' => 0 135 ] 136 ] 137 ]; 138 139 try { 140 return $this->client->indices()->create($params); 141 } catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { 142 $msg = $e->getMessage(); 143 $msg = json_decode($msg,true); 144 return $msg; 145 } 146 } 147 148 // 删除索引 149 public function delete_index($index_name = 'test_ik') { 150 $params = ['index' => $index_name]; 151 $response = $this->client->indices()->delete($params); 152 return $response; 153 } 154 155 // 创建文档模板 156 public function create_mappings($type_name = 'goods',$index_name = 'test_ik') { 157 158 $params = [ 159 'index' => $index_name, 160 'type' => $type_name, 161 'body' => [ 162 $type_name => [ 163 '_source' => [ 164 'enabled' => true 165 ], 166 'properties' => [ 167 'id' => [ 168 'type' => 'integer', // 整型 169 'index' => 'not_analyzed', 170 ], 171 'title' => [ 172 'type' => 'string', // 字符串型 173 'index' => 'analyzed', // 全文搜索 174 'analyzer' => 'ik_max_word' 175 ], 176 'content' => [ 177 'type' => 'string', 178 'index' => 'analyzed', 179 'analyzer' => 'ik_max_word' 180 ], 181 'price' => [ 182 'type' => 'integer' 183 ] 184 ] 185 ] 186 ] 187 ]; 188 189 $response = $this->client->indices()->putMapping($params); 190 return $response; 191 } 192 193 // 查看映射 194 public function get_mapping($type_name = 'goods',$index_name = 'test_ik') { 195 $params = [ 196 'index' => $index_name, 197 'type' => $type_name 198 ]; 199 $response = $this->client->indices()->getMapping($params); 200 return $response; 201 } 202 203 // 添加文档 204 public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') { 205 $params = [ 206 'index' => $index_name, 207 'type' => $type_name, 208 'id' => $id, 209 'body' => $doc 210 ]; 211 212 $response = $this->client->index($params); 213 return $response; 214 } 215 216 // 判断文档存在 217 public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { 218 $params = [ 219 'index' => $index_name, 220 'type' => $type_name, 221 'id' => $id 222 ]; 223 224 $response = $this->client->exists($params); 225 return $response; 226 } 227 228 229 // 获取文档 230 public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { 231 $params = [ 232 'index' => $index_name, 233 'type' => $type_name, 234 'id' => $id 235 ]; 236 237 $response = $this->client->get($params); 238 return $response; 239 } 240 241 // 更新文档 242 public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { 243 // 可以灵活添加新字段,最好不要乱添加 244 $params = [ 245 'index' => $index_name, 246 'type' => $type_name, 247 'id' => $id, 248 'body' => [ 249 'doc' => [ 250 'title' => '苹果手机iPhoneX' 251 ] 252 ] 253 ]; 254 255 $response = $this->client->update($params); 256 return $response; 257 } 258 259 // 删除文档 260 public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { 261 $params = [ 262 'index' => $index_name, 263 'type' => $type_name, 264 'id' => $id 265 ]; 266 267 $response = $this->client->delete($params); 268 return $response; 269 } 270 271 // 查询文档 (分页,排序,权重,过滤) 272 public function search_doc($keywords = "电脑",$index_name = "test_ik",$type_name = "goods",$from = 0,$size = 2) { 273 $params = [ 274 'index' => $index_name, 275 'type' => $type_name, 276 'body' => [ 277 'query' => [ 278 'bool' => [ 279 'should' => [ 280 [ 'match' => [ 'title' => [ 281 'query' => $keywords, 282 'boost' => 3, // 权重大 283 ]]], 284 [ 'match' => [ 'content' => [ 285 'query' => $keywords, 286 'boost' => 2, 287 ]]], 288 ], 289 ], 290 ], 291 'sort' => ['price'=>['order'=>'desc']] 292 , 'from' => $from, 'size' => $size 293 ] 294 ]; 295 296 $results = $this->client->search($params); 297// $maxScore = $results['hits']['max_score']; 298// $score = $results['hits']['hits'][0]['_score']; 299// $doc = $results['hits']['hits'][0]['_source']; 300 return $results; 301 } 302 303} 304<?php 305require "./MyElasticSearch.php"; 306 307$es = new MyElasticSearch(); 308 309$r = $es->delete_index(); 310 311$r = $es->create_index(); 312 313$r = $es->create_mappings(); 314 315$r = $es->get_mapping(); 316print_r($r); 317 318$docs = []; 319$docs[] = ['id'=>1,'title'=>'苹果手机','content'=>'苹果手机,很好很强大。','price'=>1000]; 320$docs[] = ['id'=>2,'title'=>'华为手环','content'=>'荣耀手环,你值得拥有。','price'=>300]; 321$docs[] = ['id'=>3,'title'=>'小度音响','content'=>'智能生活,快乐每一天。','price'=>100]; 322$docs[] = ['id'=>4,'title'=>'王者荣耀','content'=>'游戏就玩王者荣耀,快乐生活,很好很强大。','price'=>998]; 323$docs[] = ['id'=>5,'title'=>'小汪糕点','content'=>'糕点就吃小汪,好吃看得见。','price'=>98]; 324$docs[] = ['id'=>6,'title'=>'小米手环3','content'=>'秒杀限量,快来。','price'=>998]; 325$docs[] = ['id'=>7,'title'=>'iPad','content'=>'iPad,不一样的电脑。','price'=>2998]; 326$docs[] = ['id'=>8,'title'=>'中华人民共和国','content'=>'中华人民共和国,伟大的国家。','price'=>19999]; 327 328foreach ($docs as $k => $v) { 329 $r = $es->add_doc($v['id'],$v); 330 print_r($r); 331} 332 333$r = $es->get_doc(); 334 335$r = $es->update_doc(); 336 337$r = $es->delete_doc(); 338 339$r = $es->exists_doc(); 340 341 342$r = $es->search_doc("手环 电脑"); 343$r = $es->search_doc("玩"); 344$r = $es->search_doc("中华"); 345 346print_r($r);
PHP中使用Elasticsearch
Stella981
2021-10-11
1397 0 0
点赞
收藏
评论区
加载中...