logstash 定時同步MySQL 數據,以及es高亮,搜索分頁

今天不想寫了,明天更新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);
    }
}

以上,完結。

相關文章
相關標籤/搜索