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" } } } }
先在centos安装openssl,然后开始://生成私钥openssl genrsa -out rsa_private_key.pem 1024//生成公钥openssl rsa -in rsa_private_key.pem&...
上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?PHP项目中大部分的框架都...
Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者(publisher),订阅者(subscriber)和频道(channel)。 发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个...
(1).config.php 配置文件<?php /** * RabbitMQ_Config */ $config = [ 'host' => ...
(1)swoole启动的主进程是master进程负责全局管理,然后master进程会再fork一个manager进程。(2)manager进程开始统一管理进程创建回收管理。(3)manager进程根据设置的worker_num和task_worker_num来创建work进程和task进程因此启动s...
当我想在一个进程中监听kill 和 kill -9命令报了这个错误。//监听kill pcntl_signal(SIGTERM, function () { posix_kill(0, SIGTERM); });...