预览
+
diff --git a/control/weibo.php b/control/weibo.php index 92467e4..d1bdf70 100755 --- a/control/weibo.php +++ b/control/weibo.php @@ -59,17 +59,32 @@ class weibo extends publicBase { $cur_page = $this->post('currentPage') ? $this->post('currentPage') : 1; $page_size = $this->post('pageSize') ? $this->post('pageSize') : 20; - $condition = array(); - if ($spider_comment_status) { - $condition['spider_comment_status'] = $spider_comment_status; - $condition['uid'] = WEIBO_USER_ZHANG; + $es_search = $this->post('es_search') + 0; + $obj = new mWeibo(); + + if($es_search) { + $total = 0; + $list = array(); + $eobj = new mEs('weibo'); + $page_size = 50; + $keyword = trim($this->post('keyword')); + if (!empty($keyword)) { + $weibo_list = $eobj->searchWeibo($keyword); + $total = $weibo_list['total']; + $list = $weibo_list['list']; + } }else{ - $condition['status'] = $status; - } + $condition = array(); + if ($spider_comment_status) { + $condition['spider_comment_status'] = $spider_comment_status; + $condition['uid'] = WEIBO_USER_ZHANG; + }else{ + $condition['status'] = $status; + } - $obj = new mWeibo(); - $total = $obj->getWeiboTotal($condition); - $list = $obj->getWeiboList($condition, $cur_page, $page_size); + $total = $obj->getWeiboTotal($condition); + $list = $obj->getWeiboList($condition, $cur_page, $page_size); + } $rdata = array( 'total' => $total, @@ -209,4 +224,10 @@ class weibo extends publicBase { $this->ajax_json(true, '333'); } + + public function search() { + $keyword = trim($this->get('keyword')); + if(!empty($keyword)) $this->view['keyword'] = urldecode($keyword); + } + } diff --git a/model/mEs.php b/model/mEs.php new file mode 100644 index 0000000..058e40f --- /dev/null +++ b/model/mEs.php @@ -0,0 +1,107 @@ +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, ''.$val.'', $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; + } +} \ No newline at end of file diff --git a/model/mWeibo.php b/model/mWeibo.php index c0eef2c..60da466 100755 --- a/model/mWeibo.php +++ b/model/mWeibo.php @@ -123,4 +123,15 @@ class mWeibo extends mBase { public function getWeiboByWids($wids) { return $this->obj->selectIn($this->tbl, array('wid'=>$wids)); } + + public function getWeiboByIds($ids) { + $res = $this->obj->selectIn($this->tbl, array('id'=>$ids)); + if (empty($res)) return array(); + + $list = array(); + foreach ($res as $v) { + $list[$v['id']] = $v; + } + return $list; + } } \ No newline at end of file diff --git a/tools/es.php b/tools/es.php new file mode 100644 index 0000000..3d114f0 --- /dev/null +++ b/tools/es.php @@ -0,0 +1,326 @@ +client = ClientBuilder::create()->setHosts($params)->build(); + $this->index_name = $index; + + return $this->client; + } + + /** + * 判断索引是否存在 + * @param string $index_name + * @return bool|mixed|string + */ + public function exists_index($index_name = 'test_ik') + { + $params = [ + 'index' => $index_name + ]; + try { + return $this->client->indices()->exists($params); + } catch (\Exception $e) { + return false; + } + } + + /** + * 创建索引 + * @param string $index_name + * @return array|mixed|string + */ + public function create_index($index_name = 'test_ik') + { + $params = [ + 'index' => $index_name, + 'body' => [ + 'settings' => [ + 'number_of_shards' => 3, + 'number_of_replicas' => 0 + ] + ] + ]; + try { + return $this->client->indices()->create($params); + } catch (\Exception $e) { + return false; + } + } + + /** + * 删除索引 + * @param string $index_name + * @return array + */ + public function delete_index($index_name = 'test_ik') + { + $params = ['index' => $index_name]; + $response = $this->client->indices()->delete($params); + return $response; + } + + /** + * 添加文档 + * @param $params + * $params = [ + * 'index' => "es", + * 'type' => "article", + * "body" => [ + * "title" => "", + * ] + * ]; + * @return array + */ + public function add_doc($params) + { + return $this->client->index($params); + } + + /** + * 判断文档存在 + * @param int $id + * @param string $index_name + * @param string $type_name + * @return array|bool + */ + public function exists_doc($id = 1, $index_name = 'test_ik', $type_name = 'goods') + { + $params = [ + 'index' => $index_name, + 'type' => $type_name, + 'id' => $id + ]; + + $response = $this->client->exists($params); + return $response; + } + + /** + * 获取文档 + * @param int $id + * @param string $index_name + * @param string $type_name + * @return array + */ + public function get_doc($id = 1, $index_name = 'test_ik', $type_name = 'goods') + { + $params = [ + 'index' => $index_name, + 'type' => $type_name, + 'id' => $id + ]; + + $response = $this->client->get($params); + return $response; + } + + /** + * 修改文档 + *$params = [ + 'index' => "es", + 'type' => "article", + 'id' => "OIwzxXgBzF70K-DobSSC", + "body" => [ + "doc" => [ + "title" => "6100万颗心的共同记忆 再次C位亮相,闪耀全球!", + "desn" => "刚刚过去的这个清明节,与往年一样,有人凭寄哀思,有人缅怀忠魂。但也有一些瞬间,让人记起久久不能释怀,给这个特殊节气增添了一些格外不同的味道。" + ] + ] + ]; + * @param array $params + * @return array + */ + public function update_doc($params = []) + { + $response = $this->client->update($params); + return $response; + } + + /** + * 删除文档 + * @param int $id + * @param string $index_name + * @param string $type_name + * @return array + */ + public function delete_doc($id = 1, $index_name = 'test_ik', $type_name = 'goods') + { + $params = [ + 'index' => $index_name, + 'type' => $type_name, + 'id' => $id + ]; + + $response = $this->client->delete($params); + return $response; + } + + /** + * 搜索文档 (分页,排序,权重,过滤) + * @param string $index_name + * @param string $type_name + * @param array $body + $body = [ + 'query' => [ + 'bool' => [ + 'should' => [ + [ + 'match' => [ + 'cate_name' => [ + 'query' => $keywords, + 'boost' => 4, // 权重大 + ] + ] + ], + [ + 'match' => [ + 'goods_name' => [ + 'query' => $keywords, + 'boost' => 3, + ] + ] + ], + [ + 'match' => [ + 'goods_introduce' => [ + 'query' => $keywords, + 'boost' => 2, + ] + ] + ] + ], + ], + ], + 'sort' => ['id'=>['order'=>'desc']], + 'from' => $from, + 'size' => $size + ]; + * @return array + */ + public function search_doc($index_name = "test_ik", $type_name = "goods", $body = []) + { + $params = [ + 'index' => $index_name, + 'type' => $type_name, + 'body' => $body + ]; + + $results = $this->client->search($params); + return $results; + } + + /** + * @param string $index_name 搜索列表 + * @param string $type_name + * @return array|callable + * + */ + public function select_doc($index_name = 'test_ik', $type_name = 'goods'){ + $params=[ + 'index' => $index_name, + 'type' => $type_name, + ]; + $response = $this->client->search($params); + return $response; + } + + /** + * @param string $index_name // 高亮显示 + * @param string $type_name + * @param array $body + * @return array|callable + * + * + $body=[ + 'query' => [ + 'match' => [ + 'desn' => '董事长' + ] + ], + 'highlight' => [ + 'pre_tags' => [""], + 'post_tags' => [""], + 'fields' => [ + "desn" => new \stdClass() + ] + ], + ]; + */ + public function highlight_doc($index_name = 'test_ik', $type_name = 'goods' , $body = []){ + $params=[ + 'index' => $index_name, + 'type' => $type_name, + 'body' => $body + + ]; + $response = $this->client->search($params); + return $response; + } + + public function create_weibo_index() { + $res = $this->exists_index($this->index_name); + if($res) return true; + + $params = [ + 'index' => $this->index_name, + 'body' => [ + 'settings' => [ + 'number_of_shards' => 2, + 'number_of_replicas' => 1, + 'analysis' => [ + 'analyzer' => [ + 'ik_analyzer' => [ + 'type' => 'custom', + 'tokenizer' => 'ik_max_word' + ] + ] + ] + ], + 'mappings' => [ + 'properties' => [ + 'id' => ['type' => 'long'], + 'uid' => ['type' => 'long'], + 'wid' => ['type' => 'long'], + 'content' => [ + 'type' => 'text', + 'analyzer' => 'ik_max_word', + // 'fields' => [ + // 'semantic' => [ + // 'type' => 'dense_vector', + // 'dims' => 768 + // ] + // ] + ], + 'created_at' => ['type' => 'date'], + 'comments_count' => ['type' => 'long'], + 'attitudes_count' => ['type' => 'long'], + 'status' => ['type' => 'long'] + ] + ] + ] + ]; + + try { + return $this->client->indices()->create($params); + } catch (\Exception $e) { + return false; + } + } + +} \ No newline at end of file diff --git a/tools/es_add_comments.php b/tools/es_add_comments.php new file mode 100644 index 0000000..51ffbb5 --- /dev/null +++ b/tools/es_add_comments.php @@ -0,0 +1,92 @@ +connect_error) { + die("连接失败: " . $conn->connect_error); +} +$conn->set_charset("utf8mb4"); + +$limit = 5000; +$min_id = 0; + +$log_path = '/datacenter/zhishiku/es_comment.log'; +$log_path_success = '/datacenter/zhishiku/es_success_comment.log'; +$log_path_err = '/datacenter/zhishiku/es_error_comment.log'; + +for($page=0;;$page++){ + $sql = "SELECT * FROM spider_weibo_comments where is_search=1 and id>".$min_id." order by id asc limit ".$limit; + $result = $conn->query($sql); + if ($result->num_rows > 0) { + while($row = $result->fetch_assoc()) { + $min_id = $row['id']; + + if(str_replace(" ", "", $row["content"])=="") continue; + + $data['id'] = $row['id']+0; + + $data['uid'] = $row['uid']+0; + $data['weibo_id'] = $row['weibo_id']+0; + $data['content'] = $row['content']; + + if (!empty($row['comment_time'])) { + $data['comment_time'] = date('c', strtotime($row['comment_time'])); + } else { + $data['comment_time'] = null; + } + + $params['index'] = 'comments'; + $params['type'] = 'doc'; + $params['id'] = $data['id']; + $params['body'] = $data; + + try { + $resc = $obj->add_doc($params); + if($resc['result'] != 'created') { + error_log('error:'.json_encode($data)."\n", 3, $log_path_err); + }else{ + error_log($resc['_id']."|{$min_id}\n", 3, $log_path_success); + } + } catch (\Exception $th) { + error_log('excption:'.$th->getMessage().'|'.json_encode($data).'|'.$min_id."\n", 3, $log_path_err); + } + } + error_log((($page*$limit)+$result->num_rows)."|".$page."\n", 3, $log_path); + if($result->num_rows<$limit) break; + } else { + error_log((($page*$limit)+$result->num_rows)."|".$page."\n", 3, $log_path); + break; + } +} +$conn->close(); +?> diff --git a/tools/es_search.php b/tools/es_search.php new file mode 100644 index 0000000..b7d1d96 --- /dev/null +++ b/tools/es_search.php @@ -0,0 +1,66 @@ + [ + 'bool' => [ + 'should' => [ + [ + 'match' => [ + 'content' => [ + 'query' => $keywords, + 'boost' => 4, + ] + ] + ], + ], + ], + ], + 'from' => $from, + 'size' => $size +]; +$res = $obj->search_doc('weibo', 'doc', $body); +$hits = $res['hits']['hits']; +if(empty($hits)){ + $data['info'] = '数据为空'; + echo json_encode($data, true); + exit; +} + +$rdata = []; +foreach($hits as $val) { + $rdata[] = $val['_source']; +} +$data['status'] = true; +$data['data'] = $rdata; +echo json_encode($data, true); +exit; diff --git a/tools/es_setting.php b/tools/es_setting.php new file mode 100644 index 0000000..836f8eb --- /dev/null +++ b/tools/es_setting.php @@ -0,0 +1,95 @@ +connect_error) { + die("连接失败: " . $conn->connect_error); +} + +$conn->set_charset("utf8mb4"); + +$limit = 5000; +$min_id = 0; + +$log_path = '/datacenter/zhishiku/es.log'; +$log_path_success = '/datacenter/zhishiku/es_success.log'; +$log_path_err = '/datacenter/zhishiku/es_error.log'; + +for($page=0;;$page++){ + $sql = "SELECT * FROM spider_weibo where uid=2282201403 and id>".$min_id." order by id asc limit ".$limit; + $result = $conn->query($sql); + if ($result->num_rows > 0) { + while($row = $result->fetch_assoc()) { + $min_id = $row['id']; + + if(str_replace(" ", "", $row["text"])=="") continue; + + $data['id'] = $row['id']+0; + $data['uid'] = $row['uid']+0; + $data['wid'] = $row['wid']+0; + $data['content'] = $row['text']; + + if (!empty($row['created_at'])) { + $data['created_at'] = date('c', strtotime($row['created_at'])); + } else { + $data['created_at'] = null; + } + $data['comments_count'] = $row['comments_count']+0; + $data['attitudes_count'] = $row['attitudes_count']+0; + $data['reposts_count'] = $row['reposts_count']+0; + $data['status'] = $row['status']+0; + + $params['index'] = 'weibo'; + $params['type'] = 'doc'; + $params['id'] = $data['id']; + $params['body'] = $data; + try { + $resc = $obj->add_doc($params); + if($resc['result'] != 'created') { + error_log('error:'.json_encode($data)."\n", 3, $log_path_err); + }else{ + error_log($resc['_id']."|{$min_id}\n", 3, $log_path_success); + } + } catch (\Exception $th) { + error_log('excption:'.$th->getMessage().'|'.json_encode($data).'|'.$min_id."\n", 3, $log_path_err); + } + } + error_log((($page*$limit)+$result->num_rows)."|".$page."\n", 3, $log_path); + if($result->num_rows<$limit) break; + } else { + error_log((($page*$limit)+$result->num_rows)."|".$page."\n", 3, $log_path); + break; + } +} +$conn->close(); +?> diff --git a/tools/nohup.out b/tools/nohup.out new file mode 100644 index 0000000..e69de29 diff --git a/view/js/index.js b/view/js/index.js index d5b0df3..51577cb 100755 --- a/view/js/index.js +++ b/view/js/index.js @@ -4,6 +4,7 @@ $(document).ready(function () { var cur_editdata = {}; //当前编辑数据 var vediourl = '' var hidecomments = $('#hidecomments').length + var hidesearch = $('#hidesearch').length $('.radio-wrap').on('click', '.radio_box input', function () { var radio_val = $(this).attr('value') @@ -464,6 +465,16 @@ $(document).ready(function () { } } + if(hidesearch == 1) { + var param = { + currentPage, + pageSize, + spider_comment_status:2, + es_search:1, + keyword:$('#hidekeyword').text() + } + } + $.ajax({ type: "post", dataType: 'json', @@ -473,7 +484,6 @@ $(document).ready(function () { if(!res.status){ alert(`请求失败`); } - renderData(res.data) } }) @@ -490,6 +500,25 @@ $(document).ready(function () { renderList() var html1 = ''; + if(pageData.length == 0 && hidesearch == 1) { + var desctxt = '请输入症状进行查询' + + if($('#hidekeyword').text().trim().length>0) desctxt = '暂无数据' + + html1 = `