用PHP代码查询elasticsearch中的数据。以下展示了从连接到查询一条龙。
- 在桌面bash中登录es服务器(因为es部署在这台服务器,而服务器没设置密码,所以要专门指定某个ip访问,达到高效存取数据)
- 登录方式:ssh:ip地址
- 进入之后找到nginx配置文件: ps -ef | grep nginx
- 注意:这里显示的是系统基础配置文件nginx.conf,该文件中还包含了其它配置文件,例如,我这里的配置文件就放在/etc/nginx/conf.d/中的。你的可能会跟我不一样。
- 修改Nginx配置文件 default.conf,把当前访问的ip写入到allow ip配置下面
- vim /etc/nginx/conf.d/default.conf
- 语法解释:allow 125.66.99.55表示允许某个ip访问该服务器。
- 检查Nginx配置语法:nginx -t
注:这里是检查刚修改的配置语法是否正确 - 平滑重启:nginx -s reload (强行重启Nginx:service nginx restart(慎重,因为会关闭一次然后启动,对外提供的服务会停止))
- 如果上一步遇到这个错误:nginx: [error] invalid PID number "" in "/var/run/nginx.pid" 是没启对,什么原因不清楚。要把nginx的master进程和worker进程杀掉重启。
- 指令:kill -9 7097、kill -9 3575,先杀主进程
- 如图:
- 再重启
- 在代码中连接es
这里说个前提: 先安装PHP版es库
指令:composer require elasticsearch/elasticsearch - 查询所有文档
- 暂时到这,后续看情况续更。。。
==========================[分割线]============================
上面的方式查询数据是可以了,但是通常业务是不可能只查询所有文档数据,而且实际业务也可能不简单。这里我列举一下我的业务常用到的几个查询。
es常用查询:
- 按条件查询一个列表
- 按条件查询一条数据
- 按条件统计数据(此处只列举sum聚合)
- 并且中的或查询
(例如MySQL中的:sex='男' and (age=20 or age=25 or age=30),解释:查询性别为男的年龄为20,25,30岁的用户)
温馨提示:由于更新下来的es库虽然帮助解决了PHP查询es能力,但是我还是觉得有点复杂。所以本人自己编写了一套es查询库,而且也在使用中。
安装代码:
composer require wanghua/general-utility-tools-php dev-master
文档地址:https://packagist.org/packages/wanghua/general-utility-tools-php
建议看源码,因为有些功能文档中可能没体现出来,但是源码中有。
列表查询:
温馨提示:查询一条数据也可以用此方法搞定,只是结果只有一条数据而已。
function get(){
//可按你的需求自己调整一下部分代码
$offset = input('offset', 5);
$limit = input('limit', 5);
$input_data = json_decode(input('filter'), true);
$es = new BaseElasticsearch();
//判断时间查询,动态生成索引
if(!empty($input_data['sign_time'])){
$start_time = explode(' - ', $input_data['sign_time'])[0];
$end_time = explode(' - ', $input_data['sign_time'])[1];
$es->index = $es->setIndexDate($start_time, $end_time);
}else{
$es->index = $es->setDefaultDate();//默认一个月
}
$es->type = 'logs';
$es->current_page = $limit ? intval($offset / $limit) + 1 : 1;
$es->limit = $limit;
//关键步骤
//设置查询条件
$where = [
[
'term' => [
'statType' => 'draw'
]
]
];
//可选
if(!empty($input_data['gameid'])){
$where[] = [
'term' => [
'uid' => $input_data['gameid']
]
];
}
//时间
if(!empty($input_data['sign_time'])){
$date_arr = $es->strtounixtime($input_data['sign_time']);
$where[] = [
'range' => [
'unixtime' => [
'gte' => $date_arr['start_time'],
'lte' => $date_arr['end_time'],
]
]
];
}
//排序
$es->order('unixtime', 'desc');
//设置bool查询模式
$es->setQueryBool();
//必须[setQueryBool & must,must_not, should必须在一起使用]
$es->must($where);
//执行查询
$res = $es->query();
return $res;
}
sum统计总数:
function total(){
$input_data = input();
$es = new BaseElasticsearch();
//判断时间查询,动态生成索引
if(!empty($input_data['conversion_time'])){
$start_time = explode(' - ', $input_data['conversion_time'])[0];
$end_time = explode(' - ', $input_data['conversion_time'])[1];
$es->index = $es->setIndexDate($start_time, $end_time);
}else{
//$es->index = $es->setDefaultDate();//默认一个月
//默认查询所有历史记录
$es->index = $es->setIndexAllDate();
}
$es->type = 'logs';
$es->current_page = 1;
$es->limit = 999999999;
//关键步骤
//设置查询条件
//
$where = [
[
'term' => [
'statType' => 'exchange'//兑换类型
]
],
];
//可选
if(!empty($input_data['gameid'])){
$where[] = [
'term' => [
'uid' => 1*$input_data['gameid']
]
];
}
//昵称
if(!empty($input_data['nickname'])){
$where[] = [
'term' => [
'nickname' => $input_data['nickname']
]
];
}
//时间
if(!empty($input_data['conversion_time'])){
$date_arr = $es->strtounixtime($input_data['conversion_time']);
$where[] = [
'range' => [
'unixtime' => [
'gte' => $date_arr['start_time'],
'lte' => $date_arr['end_time'],
]
]
];
}
$should_where = [
[
"range"=> [
"item2001"=> [
"gte"=> 0
]
]
],
[
"range"=> [
"item2002"=> [
"gte"=> 0
]
]
],
[
"range"=> [
"item2005"=> [
"gte"=> 0
]
]
],
[
"range"=> [
"item2010"=> [
"gte"=> 0
]
]
]
];
//排序
//$es->order('unixtime', 'desc');
//设置bool查询模式
$es->setQueryBool();
//必须[setQueryBool & must,must_not, should必须在一起使用]
$es->must($where);
$es->should($should_where);
$es->setAggs('item1001', 0);
//执行查询
$res = $es->sum();
return $res['aggregations']['sum']['value'];
}
END