elasticsearch的操作都是基于http协议的,已经有现成的php类库,composer安装即可。
{ "require": { "elasticsearch/elasticsearch": "^6.7" } }
(1).先封装一个简单的操作
class Es { /** * Es_Hosts * @var array */ private $hosts = ['127.0.0.1:9200']; /** * Es_hander * @var null */ private $hander = null; /** * Es constructor. */ public function __construct() { $this->hander = \Elasticsearch\ClientBuilder::create()->setHosts($this->hosts)->build(); } /** * 索引一个文档(相当于mysql新增) * @param array $data 索引的数据 * @return array */ public function add($data) { $inputData = [ 'index' => 'hq', //索引,相当于Mysql的数据库 'type' => 'hq_member', //索引,相当于Mysql的表 'body' => $data //添加的文档,相当于Mysql的记录 ]; return $this->hander->index($inputData); } /** * 批量索引文档(相当于mysql批量新增) * @param array $allData 批量索引的数据 * @return array */ public function addAll($allData) { $inputData = []; foreach ($allData as $data) { $inputData['body'][] = [ 'index' => [ '_index' => 'hq', //索引,相当于Mysql的数据库 '_type' => 'hq_member', //索引,相当于Mysql的表 ] ]; $inputData['body'][] = $data; } return $this->hander->bulk($inputData); } /** * 获取一个文档(相当于mysql查询记录) * @param string $id 文档id * @return array|bool */ public function get($id) { $inputData = [ 'index' => 'hq', //索引,相当于Mysql的数据库 'type' => 'hq_member', //索引,相当于Mysql的表 'id' => $id //文档的id ]; try { $result = $this->hander->get($inputData); } catch (\Elasticsearch\Common\Exceptions\Missing404Exception $exception) { //不存在的文档竟然抛出异常,有点2 return false; } return $result; } /** * 搜索一个文档(相当于mysql搜索记录) * @param string $word 搜索词 * @return array */ public function search($word) { $inputData = [ 'index' => 'hq', //索引,相当于Mysql的数据库 'type' => 'hq_member', //索引,相当于Mysql的表 ]; $inputData['body']['query']['match']['desc'] = $word; return $this->hander->search($inputData); } /** * 删除一个文档(相当于mysql删除记录) * @param string $id 文档id * @return array */ public function delete($id) { $inputData = [ 'index' => 'hq', //索引,相当于Mysql的数据库 'type' => 'hq_member', //索引,相当于Mysql的表 'id' => $id, //文档id ]; return $this->hander->delete($inputData); } /** * 创建索引名称(相当于mysql创建库) * @param string $indexName 索引名 * @return array */ public function createIndex($indexName) { return $this->hander->indices()->create([ 'index' => $indexName ]); } /** * 删除索引名称(相当于mysql删库) * @param string $indexName 索引名 * @return array */ public function deleteIndex($indexName) { return $this->hander->indices()->delete([ 'index' => $indexName ]); } }
(2).索引一个文档记录(相当于mysql的insert)
$oneData = ['name' => '张清喜', 'age' => 21, 'desc' => '江西的小伙子']; $ret = $es->add($oneData);
返回内容:
{ ["_index"]=> string(2) "hq" ["_type"]=> string(9) "hq_member" ["_id"]=> string(20) "HIuebG4B8k6m0qLHMJmc" ["_version"]=> int(1) ["result"]=> string(7) "created" ["_shards"]=> array(3) { ["total"]=> int(2) ["successful"]=> int(1) ["failed"]=> int(0) } ["_seq_no"]=> int(3) ["_primary_term"]=> int(1) }
(3).获取一个文档记录,传入的是文档id
$oneData = $es->get('GYuebG4B8k6m0qLHL5nR'); var_dump($oneData);
返回内容:
{ ["_index"]=> string(2) "hq" ["_type"]=> string(9) "hq_member" ["_id"]=> string(20) "GYuebG4B8k6m0qLHL5nR" ["_version"]=> int(1) ["_seq_no"]=> int(0) ["_primary_term"]=> int(1) ["found"]=> bool(true) ["_source"]=> array(3) { ["name"]=> string(9) "高久峰" ["age"]=> int(21) ["desc"]=> string(24) "陕西安康的小伙子" }
(4).搜索文档
$word = '江西'; $ret = $es->search($word); var_dump($ret);
返回内容:
{ ["took"]=> int(2) ["timed_out"]=> bool(false) ["_shards"]=> array(4) { ["total"]=> int(1) ["successful"]=> int(1) ["skipped"]=> int(0) ["failed"]=> int(0) } ["hits"]=> array(3) { ["total"]=> array(2) { ["value"]=> int(2) ["relation"]=> string(2) "eq" } ["max_score"]=> float(2.6002216) ["hits"]=> array(2) { [0]=> array(5) { ["_index"]=> string(2) "hq" ["_type"]=> string(9) "hq_member" ["_id"]=> string(20) "HYunbG4B8k6m0qLHMpmu" ["_score"]=> float(2.6002216) ["_source"]=> array(3) { ["name"]=> string(9) "张清喜" ["age"]=> int(21) ["desc"]=> string(18) "江西的小伙子" } } [1]=> array(5) { ["_index"]=> string(2) "hq" ["_type"]=> string(9) "hq_member" ["_id"]=> string(20) "GYuebG4B8k6m0qLHL5nR" ["_score"]=> float(0.9092851) ["_source"]=> array(3) { ["name"]=> string(9) "高久峰" ["age"]=> int(21) ["desc"]=> string(24) "陕西安康的小伙子" } } } } }
(5).批量索引文档
$allData = [ ['name' => '谢丽芬', 'age' => 25, 'desc' => '广东富婆'], ['name' => '韩星星', 'age' => 25, 'desc' => '江西老表'] ]; $ret = $es->addAll($allData); var_dump($ret);
返回内容:
{ ["took"]=> int(341) ["errors"]=> bool(false) ["items"]=> array(2) { [0]=> array(1) { ["index"]=> array(9) { ["_index"]=> string(3) "1hq" ["_type"]=> string(9) "hq_member" ["_id"]=> string(20) "pi_zbG4BdMVi8p37iZ_K" ["_version"]=> int(1) ["result"]=> string(7) "created" ["_shards"]=> array(3) { ["total"]=> int(2) ["successful"]=> int(1) ["failed"]=> int(0) } ["_seq_no"]=> int(0) ["_primary_term"]=> int(1) ["status"]=> int(201) } } [1]=> array(1) { ["index"]=> array(9) { ["_index"]=> string(3) "1hq" ["_type"]=> string(9) "hq_member" ["_id"]=> string(20) "py_zbG4BdMVi8p37iZ_K" ["_version"]=> int(1) ["result"]=> string(7) "created" ["_shards"]=> array(3) { ["total"]=> int(2) ["successful"]=> int(1) ["failed"]=> int(0) } ["_seq_no"]=> int(1) ["_primary_term"]=> int(1) ["status"]=> int(201) } } } }
(6).删除文档
$ret = $es->delete('GouebG4B8k6m0qLHMJlx'); var_dump($ret);
返回内容:
{ ["_index"]=> string(2) "hq" ["_type"]=> string(9) "hq_member" ["_id"]=> string(20) "GouebG4B8k6m0qLHMJlx" ["_version"]=> int(2) ["result"]=> string(7) "deleted" ["_shards"]=> array(3) { ["total"]=> int(2) ["successful"]=> int(1) ["failed"]=> int(0) } ["_seq_no"]=> int(7) ["_primary_term"]=> int(2) }
(7).创建索引
$ret = $es->createIndex('chen'); var_dump($ret);
返回内容:
{ ["acknowledged"]=> bool(true) ["shards_acknowledged"]=> bool(true) ["index"]=> string(4) "chen" }
(8).删除索引
$ret = $es->deleteIndex('chen'); var_dump($ret);
返回内容:
{ ["acknowledged"]=> bool(true) }
(9).设置索引的配置
$ret = $es->setIndexConfig([ 'number_of_replicas' => 0, //数据备份数,如果只有一台机器,设置为0,设置了好像要重启才生效,醉了 ]); var_dump($ret);
返回内容:
{ ["acknowledged"]=> bool(true) }
(10).获取索引的配置
$ret = $es->getIndexConfig('hq'); var_dump($ret);
返回内容:
{ ["hq"]=> array(1) { ["settings"]=> array(1) { ["index"]=> array(6) { ["creation_date"]=> string(13) "1573780336188" ["number_of_shards"]=> string(1) "1" ["number_of_replicas"]=> string(1) "0" ["uuid"]=> string(22) "VsJeaujeQdeiPsbQ9ZAPAg" ["version"]=> array(1) { ["created"]=> string(7) "7040299" } ["provided_name"]=> string(2) "hq" } } } }
因为一个TP项目中客户需要全部网页分享支持自定义图片和描述信息,于是自己封装了下 //share()微信分享链接 //参数1 appid //参数2 appsert //参数3 nonceStr随机码 //参数4 timestamp时间戳 public&nb...
前面的文章对于高并发下单商品导致商品库存为负值的问题请先阅读再阅读本篇文章一定对您有帮助,建议亲手测试较好。加上文件锁后的下单处理代码:【一】.阻塞模式:(如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行)<?php //连接数据库 $con=mysqli_connect(...
源码:特别适用于微信支付中通知微信支付网关function array2xml($arr, $level = 1) { $s = $level == 1 ? "<xml&g...
php官方已经提供了Iterator(迭代器)接口,通过网上资料的学习,目前看适合超大集合或者数组提取使用。学习一个函数的实现对比内存占用差距.使用迭代器和普通循环实现range()函数。(1).普通循环实现range()函数。function newrange($low, $h...
xmlrpc协议是通过http请求xml数据进行通信。webservice中和它相同的是soap。soap调用的确很简单,但是创建wsdl太繁琐,效率低下。xmlrpc很好的解决这个问题。(1).创建xmlrpc服务端(求和函数api)function getSum($method,$ar...
ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...