logstash 定时同步MySQL 数据,以及es高亮搜索

一、es安装 php

    先安装  elasticsearch-php  (这个对php的版本有要求,推荐composer安装),对应版本传送,安装完成后,搭建JAVA 环境(网上搜索),环境配置完成后(须要添加几个系统配置的环境变量),下载 对应 php版本  的elastic 对应版本传送elastic  下载传送,安装说明在下载的页面有相关文档,推荐下载对应的分词【ik版本传送】,安装比较简单,解压缩,复制/剪切到 elastic 的  plugins 文件夹下(没有就新建),重启elastic,git

二、logstash 安装github

    这个须要的是ruby 环境,因此搭建ruby环境,而后下载安装ruby

推荐参考范例,按照这个操做很快就能够完成,是我找到最好的教程,composer

三、php 实现搜索dom

因为数据尚未处理完成,就以上述案例的数据为准,进行操做elasticsearch

 

    1)所有搜索没有 等价于 MySQL【select * 】post

public function search($key){

        $hosts = [
//            '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',
        ];
        $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
        ->setHosts($hosts)      // Set the hosts
//        ->setRetries(2)  //
        ->build();              // Build the client object
        $params = [
            'index' => 'xc_course',
            'type' => 'doc',
//            'body'  => ['name' => 'js']
        ];
        $response = $client->search($params);
        print_r($response);
}

 

2)根据搜索词进行搜索ui

public function search($key){
//        $key = $request->get('key');


        $hosts = [
//            '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',
        ];
        $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
        ->setHosts($hosts)      // Set the hosts
//        ->setRetries(2)  //
        ->build();              // Build the client object

        $params = [
            'index' => 'xc_course',
//            'type' => 'my_type',
            'body'  => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'name' => '实战' ] ],
//                            [ 'match' => [ 'description' => '课程' ] ],
                            [ 'match' => [ 'description' => '实战' ] ],
                        ]
                    ]
                ],
            ]
        ];
//        $response = $client->index($params);
        $response = $client->search($params);
        print_r($response);

    }

    3) 搜索结果高亮显示.net

public function search($key){
//        $key = $request->get('key');


        $hosts = [
//            '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',
        ];
        $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
        ->setHosts($hosts)      // Set the hosts
//        ->setRetries(2)  //
        ->build();              // Build the client object

        $params = [
            'index' => 'xc_course',
//            'type' => 'my_type',
            'body'  => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'name' => '实战' ] ],
//                            [ 'match' => [ 'description' => '课程' ] ],
                            [ 'match' => [ 'description' => '实战' ] ],
                        ]
                    ]
                ],
                'highlight' => [
                    'pre_tags' => ["<em>"], // 高亮能够本身修改
                    'post_tags' => ["</em>"],
                    'fields' => [
                        "name" => new \stdClass(),
                        "description" => new \stdClass(),
                    ]
                ]
            ]
        ];
        $response = $client->search($params);
        prt($response);

    }

 

固然既然用了es不该该只是一个数据表,多表处理传送门

相关文章
相关标签/搜索