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"
}
}
}
} 首先看看以下代码:代码1:<?php $a=0.1; $b=0.7; if($a+$b==0.8) { echo "1"; } else{ echo "2"; } ?>代码2:<?php &n...
(1).学习的目标:学会创建父子进程,并且能够区分当前进程是父还是子;了解父进程执行过程,子进程执行过程;能够用多进程执行任务(2).相关函数学习: (2.1)pcntl_fork()执行时: &nbs...
当我想在一个进程中监听kill 和 kill -9命令报了这个错误。//监听kill pcntl_signal(SIGTERM, function () { posix_kill(0, SIGTERM); });...
最近在项目中处理一个关于商品数据重复需要删除多余的商品记录,但是删除一条商品必然要把关联的其他表商品的id和其他商品信息更换为正确的,删除一个商品记录,同时要去修改100多张表的关联商品数据,在项目中引用了tp orm 1.2版本,由于项目是php5.6版本,没法使用最新orm,在代码中每处理1个商...
为什么使用队列?因为pop取队列具有原子性。假如我们需要秒杀一个商品id,我们先将商品的库存保存到一个队列。例如:<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6...
文章篇幅较长,如果不喜欢看文章的,此处抛出韩顺丰老师的位运算视频,韩老师应该是全网讲php位运算符最详细的一个老师了。链接:https://pan.baidu.com/s/14xj7er8eVSUcJ-jYXyA0GA 提取码:731m 链接:https://pan.baidu.com...