You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

107 lines
3.2 KiB

<?php
/**
*
*/
include_once(SERVER_ROOT . "/model/mBase.php");
class mEs extends mBase {
public $es_host = '8.154.43.224';
public $es_port = 9200;
public $es_index = '';
public function __construct($index) {
$this->es_index = $index;
}
public function search($body) {
$es_url = "http://" . $this->es_host . ":" . $this->es_port . "/{$this->es_index}/_search";
return $this->postCUrl($es_url, $body, 10, false, true);
}
public function searchWeibo($keywords) {
if(empty($keywords)) return false;
$from = 0;
$size = 50;
$body = [
'query' => [
'bool' => [
'should' => [
[
'match' => [
'content' => [
'query' => $keywords,
'boost' => 4,
]
]
],
],
],
],
'highlight' => [
'fields' => [
'content' => [
'fragment_size' => 2,
'number_of_fragments' => 20,
'pre_tags' => [''],
'post_tags' => ['']
]
]
],
'from' => $from,
'size' => $size
];
$res = $this->search(json_encode($body));
$list = json_decode($res, true);
if(empty($res) || empty($list['hits'])) {
$this->setError('获取失败');
return false;
}
$rdata['total'] = $list['hits']['total']+0;
$data['list'] = $list['hits']['hits'];
if($rdata['total'] <= 0) {
$rdata['list'] = array();
return $rdata;
}
$rdata['total'] = $size;
$weibo_ids = array_column($data['list'], '_id');
$obj = new mWeibo();
$list = $obj->getWeiboByIds($weibo_ids);
$rlist = array();
foreach($data['list'] as $v) {
$weibo_id = $v['_id'];
$weibo = $list[$weibo_id];
if(empty($weibo)) continue;
$highlights = $v['highlight']["content"];
if(!empty($highlights)) {
foreach($highlights as $k=>$val) {
$weibo['text'] = str_replace($val, '<span class="highlight" style="color:red;backgroud:yellow;">'.$val.'</span>', $weibo['text']);
}
}
$weibo['name'] = '张宝旬';
$pic_ids = json_decode($weibo['pic_ids'], 1);
$pic_arr = array();
if ($pic_ids) {
foreach ($pic_ids as $pic_id) {
$pic_arr[] = $obj->getPicOssUrl($pic_id, $weibo['created_at']);
}
}
$weibo['pic_arr'] = $pic_arr;
if ($weibo['video_url']) $weibo['video_url'] = $obj->getVideoOssUrl($weibo['wid'], $weibo['created_at']);
if ($weibo['video_cover']) $weibo['video_cover'] = $obj->getPicOssUrl($weibo['video_cover'], $weibo['created_at']);
$rdata['list'][] = $weibo;
}
return $rdata;
}
}