今天不想写了,明天更新php
更新啦…………sql
基本操做请看这里缓存
如今更新一下,项目中使用的dom
整理以前的代码,把一些能够封装的代码进行简单封装代码以下post
须要引入 es 的文件,ui
class ES extends Model { public function esSearch($searchInfo, $hostInfo = []){ if(!$hostInfo){ // $hostInfo = [ //// '192.168.1.1:9200', // IP + Port //// '192.168.1.2', // Just IP //// 'mydomain.server.com:9201', // Domain + Port //// 'mydomain2.server.com', // Just Domain //// 'https://localhost', // SSL to localhost //// 'https://192.168.1.3:9200', // SSL to IP + Port // '127.0.0.1:9200', // ]; $envEsHost = env('ES_HOST'); if (mb_strpos($envEsHost,',') !== false){ $hostArr = explode(',',$envEsHost); foreach ($hostArr as $key => &$hostVal){ $hostVal = str_replace(":",":",$hostVal); $hostVal = str_replace("。",".",$hostVal); if(!checkIp4Address($hostVal)){ unset($hostArr[$key]); } } $hostInfo = $hostArr; }elseif ($envEsHost){ $envEsHost = str_replace(":",":",$envEsHost); $envEsHost = str_replace("。",".",$envEsHost); if(checkIp4Address($envEsHost)){ $hostInfo = [$envEsHost]; }else{ $hostInfo = [ '127.0.0.1:9200']; } }else{ $hostInfo = [ '127.0.0.1:9200']; } } $esClient = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hostInfo) // Set the hosts // ->setRetries(2) // ->build(); // Build the client object // $params = [ // 'index' => 'xc_course', // 'type' => 'doc', // ]; return $esClient->search($searchInfo); } public function esDel(array $searchInfo, $hostInfo = []){ if(empty($searchInfo) OR !isset($searchInfo['index'])){return false;} if(!$hostInfo){ $hostInfo = [ // '192.168.1.1:9200', // IP + Port // '192.168.1.2', // Just IP // 'mydomain.server.com:9201', // Domain + Port // 'mydomain2.server.com', // Just Domain // 'https://localhost', // SSL to localhost // 'https://192.168.1.3:9200', // SSL to IP + Port '127.0.0.1:9200', ]; } $esClient = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hostInfo) // Set the hosts // ->setRetries(2) // ->build(); // Build the client object // $params = [ // 'index' => 'xc_course', // 'type' => 'doc', // ]; return $esClient->indices()->delete($searchInfo); } }
IP校验编码
function checkIp4Address($ipAddress) { if (mb_strpos($ipAddress,':') !== false){ $ipAddressArr = explode(':',$ipAddress); if(!isset($ipAddressArr[1]) OR !is_numeric($ipAddressArr[1]) OR $ipAddressArr[1] > 65535 or $ipAddressArr[1] < 0){return false;} $ipAddress = $ipAddressArr[0]; } $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/"; if (preg_match($preg, $ipAddress) ){ return true; }else{ return false; } }
调用封装(其实这个也能够写在model,可是为了调试方便就放在控制器了)spa
private function esParam_comm($searchs){ return [ // 'scroll'=>'60s', // 'size'=>5, // 'from'=>0, // 'index' => [], 'client' => [ 'ignore' => [400, 404] ], 'body' => [ 'size'=>150, // 通常搜索没人会把全部的内容都读取了,有10 页数据足够了 'min_score'=>1, // 最低分数,能够过滤一些不是很匹配的数据 'query' => [ "bool"=>[ // 至关于sql 的where "filter"=> [ "term"=>[ "status"=> 1, ], ], "should"=>[ "multi_match"=>[ "query" => strToUtf8($searchs), // "analyzer"=>"ik_smart", //搜索方式 // "type"=>"most_fields", "type"=>"best_fields", "operator"=> "and", // 这个参数会把一些没有添加自定义词典的词也能够搜索 "fields" => [ // 搜索词须要匹配, ^999 表示这个词的权重,默认为1 'title^99999', 'content', 'keyword', ], ], ], ], ], // "search_after"=> [6.738054, 570], // es 分页,可是不能跳页,如第一页跳第五页 'sort'=>[ '_score'=>'DESC', // '_id'=>'DESC', ], 'highlight' => [ 'pre_tags' => ["<span class='colorreddark'>"], // 高亮能够本身修改 'post_tags' => ["</span>"], 'fields' => [ // 解决高亮时,显示不完整 "title" => ["fragment_size" => 150, "number_of_fragments" => 0, "no_match_size"=> 300 ], "intro" => ["fragment_size" => 150, "number_of_fragments" => 0, "no_match_size"=> 300 ], ] ], // 只显示 es 的主要字段 // '_source'=>false, '_source'=>[ // 本身添加 ], ] ]; }
剩余的就是把查到的结果本身分页,为了提升访问的速度,能够把结果存入缓存.net
字符编码转化调试
function strToUtf8($str){ $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5')); if($encode == 'UTF-8'){ return $str; }else{ return mb_convert_encoding($str, 'UTF-8', $encode); } }
以上,完结。