作者 邓超

es

... ... @@ -144,7 +144,6 @@ class MailListV2Es extends Base {
}
}
// 搜索标题
$where['subject'] = [];
$keyword = app()->request('keyword');
if($keyword){
$where['subject'] = $keyword;
... ... @@ -193,12 +192,15 @@ class MailListV2Es extends Base {
];
foreach ($where as $k=>$v){
if($k=='subject'){
if($v){
$query['bool']['must'][] = [
'query_string' => [
'query'=>'"'.addcslashes($keyword,'"').'"',
'query'=>'"'.addcslashes($v,'"').'"',
"default_field"=> "subject"
]
];
}
}else{
$query['bool']['must'][] = [
'term' => [
... ... @@ -216,33 +218,41 @@ class MailListV2Es extends Base {
$query['bool']['must'][] = $this->assignSql2();
}
logs('请求es前');
$result = $this->es->search(['query'=>$query],($page-1) * $limit,$limit,['udate'=>"desc"]);
app()->_json($result);
logs('请求es后');
$total = $result['hits']['total']['value']??0;
$lists = $result['hits']['hits']??[];
// map
// $lists = array_map(function ($v){
// $v['uuid'] = md5($v['email_id'].'-'.$v['folder_id'].'-'.$v['uid']);
// if(!empty($v['description'])){
// $v['description'] = @html_entity_decode($v['description'], ENT_COMPAT, 'UTF-8');
// }
// $v['to_name'] = @json_decode($v['to_name'],true);
// $v['to_name'] = $v['to_name']?:[];
// if($v['to_name']){
// if(!empty($v['to_name'][0]['email'])){
// $v['to'] = $v['to_name'][0]['email'];
// }
// $v['to_name'] = MailFun::mb_coding($v['to_name'][0]['name']??'');
// }
// if(is_array($v['to_name'])){
// $v['to_name'] = '';
// }
// return $v;
// },$lists?:[]);
$lists = array_map(function ($v){
$v = $v['_source'];
$v['id'] = $v['uuid'];
$v['from_name'] = $v['from']['name']??'';
$v['from'] = $v['from']['email']??'';
$v['uuid'] = md5($v['email_id'].'-'.$v['folder_id'].'-'.$v['uid']);
if(!empty($v['description'])){
$v['description'] = @html_entity_decode($v['description'], ENT_COMPAT, 'UTF-8');
}
$v['to_name'] = @json_decode($v['to_name'],true);
$v['to_name'] = $v['to_name']?:[];
if($v['to_name']){
if(!empty($v['to_name'][0]['email'])){
$v['to'] = $v['to_name'][0]['email'];
}
$v['to_name'] = MailFun::mb_coding($v['to_name'][0]['name']??'');
}
if(is_array($v['to_name'])){
$v['to_name'] = '';
}
return $v;
},$lists?:[]);
// app()->_json(listsPage($lists,$total,$page,$limit));
app()->_json(listsPage($lists,$total,$page,$limit));
}
... ... @@ -401,14 +411,20 @@ class MailListV2Es extends Base {
}
// 默认
return dbWhere(['email_id'=>$this->getEmails('id')]).' and ';
return [
'query'=>[
'bool'=> [
'must'=>[['terms'=>['email_id'=> $this->getEmails('id')]]]
]
]
];
}
private function assignSql3($folder){
// 被分配的
$assign = app()->request('assign');
$sql = '';
$fids = [];
if(!empty($assign['email_id'])){
// 此处请求中的
$email = array_diff($this->getEmails('id'),$assign['email_id']);
... ... @@ -416,15 +432,39 @@ class MailListV2Es extends Base {
$fids = $this->getFolderId($folder,$email);
// 有目录id和from
if($fids){
return dbWhere(['folder_id'=>$fids]).' and ';
return [
'query'=>[
'bool' =>[
'must' =>[
['terms'=>['folder_id'=>$fids]]
]
]
]
];
}
}
return '`folder_id` = -1 and ';
return [
'query'=>[
'bool' =>[
'must' =>[
['term'=>['folder_id'=>-1]]
]
]
]
];
}
return [
'query'=>[
'bool' =>[
'must' =>[
['terms'=>['folder_id'=> $this->getFolderId($folder)]]
]
]
]
];
return dbWhere(['folder_id'=>$this->getFolderId($folder)]).' and ';
}
... ... @@ -432,9 +472,133 @@ class MailListV2Es extends Base {
public function count(){
return [];
if(!$this->getEmails('id')){
app()->e('email_request_required');
}
$body = [];
$body['query'] = [
'bool'=>[
'must'=>[
['term'=>['deleted'=>0]]
]
]
];
// 时间 必须是数组
$udate = app()->request('udate');
if($udate && is_array($udate) && count($udate) == 2){
$udate = array_values($udate);
$udate = array_map(function ($v){
if(is_numeric($v)) return $v;
return strtotime($v);
},$udate);
// 时间范围搜索
$body['query']['bool']['must'][] = [
'query' => [
'range' => [
'udate' => [
'gte' => $udate[0], // 大于等于开始日期
'lte' => $udate[1] // 小于等于结束日期
]
]
]
];
}
// show_count_filed
$show_count_filed = app()->requestArr('show_count_filed',['inbox', 'send', 'unseen', 'flagged', 'junk', 'hot_inbox', 'hot_send']);
// 发件箱
if(in_array('hot_send',$show_count_filed)){
// 预热发件箱
$fCount = $this->countHot($body,'发件箱');
}
// 预热收件箱
if(in_array('hot_inbox',$show_count_filed)) {
$sCount = $this->countHot($body,'收件箱');
}
if(in_array('send',$show_count_filed)) {
$faCount = $this->countMail($body,'发件箱');
}
// 垃圾箱
if(in_array('junk',$show_count_filed)) {
$lajiCount = $this->countMail($body,'垃圾箱');
}
// 收件箱
$lajiCount = $this->countMail($body,'收件箱');
// 收件箱
if(in_array('inbox',$show_count_filed) || in_array('replied',$show_count_filed)) {
$repliedCount = $shouCount = $this->countMail($body,'收件箱');
}
// 未读
if(in_array('unseen',$show_count_filed)) {
$seenCount = $shouCount = $this->countMail($body,'收件箱', 0);
}
// 星标
if(in_array('flagged',$show_count_filed)) {
$flaggedCount = $this->countFlagged($body);
}
$data = [];
if(isset($shouCount)) $data['inbox'] = $shouCount;
if(isset($repliedCount)) $data['replied'] = $repliedCount;
if(isset($faCount)) $data['send'] = $faCount;
if(isset($seenCount)) $data['unseen'] = $seenCount;
if(isset($flaggedCount)) $data['flagged'] = $flaggedCount;
if(isset($lajiCount)) $data['junk'] = $lajiCount;
if(isset($sCount)) $data['hot_inbox'] = $sCount;
if(isset($fCount)) $data['hot_send'] = $fCount;
app()->_json($data);
}
/**
* 统计预热邮件
* @param $body
* @param $folder
* @author:dc
* @time 2025/3/6 15:24
*/
private function countHot($body,$folder){
$body['query']['bool']['must'][] = ['term'=>['is_hots'=>1]];
$body['query']['bool']['must'][] = $this->assignSql3($folder);
return $this->es->count($body);
}
private function countMail($body,$folder,$seen=null){
if($folder == '收件箱'){
$body['query']['bool']['must'][] = ['term'=>['is_auto'=>0]];
}
if($seen!==null){
$body['query']['bool']['must'][] = ['term'=>['seen'=>$seen]];
}
$body['query']['bool']['must'][] = ['term'=>['is_hots'=>0]];
$body['query']['bool']['must'][] = $this->assignSql($folder);
return $this->es->count($body);
}
/**
* 统计星标
* @param $body
* @return int|mixed
* @author:dc
* @time 2025/3/6 16:01
*/
private function countFlagged($body){
$body['query']['bool']['must'][] = ['term'=>['flagged'=>1]];
$body['query']['bool']['must'][] = $this->assignSql2();
return $this->es->count($body);
}
}
... ...
... ... @@ -105,10 +105,10 @@ class Es {
$response = $this->client->count($params);
}catch (\Throwable $e) {
logs("搜索数据es:".$e->getMessage());
return [];
return 0;
}
return $response->asArray();
return $response->asArray()['count']??0;
}
... ...