Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎。当然 Elasticsearch 并不仅仅是 Lucene 那么简单,它不仅包括了全文搜索功能,还可以进行以下工作:


分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索。

实时分析的分布式搜索引擎。

可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。


你往 ES 里写的数据,实际上都写到磁盘文件里去了,查询的时候,操作系统会将磁盘文件里的数据自动缓存到 Filesystem Cache 里面去。

整个过程,如下图所示:

ES 的搜索引擎严重依赖于底层的 Filesystem Cache,你如果给 Filesystem Cache 更多的内存,尽量让内存可以容纳所有的 IDX Segment File 索引数据文件,那么你搜索的时候就基本都是走内存的,性能会非常高。

提升性能的小技巧:

  1. 仅在 ES 中就存少量的数据,无关检索的数据使用HBase等数据库存储,让Filesystem Cache可以容纳更多的数据量。

  2. 数据预热,经常会有人访问的数据,每隔一段时间,就提前访问一下,让数据进入 Filesystem Cache 里面去。


1、安装Elastaic


官网下载地址:https://www.elastic.co/downloads/elasticsearch


# 解压到非root目录,运行时使用非root账号且必须安装java环境


yum install java


wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz


tar zxvf elasticsearch-6.2.3.tar.gz


useradd elasticsearch


password elasticsearch


chown elasticsearch:elasticsearch elasticsearch-6.2.3


cd elasticsearch-6.2.3


nohup ./bin/elasticsearch  &   #设置成常驻进程


php扩展库引入  composer.json

{  "require": 
    {    
        "elasticsearch/elasticsearch": "~6.0"    
        
    } 
}


Client使用示例:

prepare($sql);
            $query->execute();
            $lists = $query->fetchAll();
            print_r($lists);
        } catch (Exception $e) {
            echo $e->getMessage();
        }

        $client = ClientBuilder::create()->build();
        foreach ($lists as $row) {
            $params = [
                'body' => [
                    'id' => $row['id'],
                    'title' => $row['title'],
                    'content' => $row['content']
                ],
                'id' => 'article_' . $row['id'],
                'index' => 'articles_index',
                'type' => 'articles_type'
            ];
            $client->index($params);
        }


    }

    /*
     * 功能:获取索引
     * return
     */
    public function getIndex(){
        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'articles_index',
            'type' => 'articles_type',
            'id' => 'article_1'
        ];
        $res = $client->get($params);
        print_r($res);
    }
    
    
    /*
     * 功能:从索引中删除文档
     * return 
     */
    public function delIndex(){
        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'articles_index',
            'type' => 'articles_type',
            'id' => 'article_1'
        ];
        $res = $client->delete($params);
        print_r($res);

    }


    /*
      * 功能:设置索引
      * return
      */
    public function createIndex(){
        $client = ClientBuilder::create()->build();
        $params['index'] = 'articles_index';
        $params['body']['settings']['number_of_shards'] = 2;
        $params['body']['settings']['number_of_replicas'] = 0;
        $client->indices()->create($params);

    }


    /*
     * 功能:查询条件
     * return 
     */
    public function search(){

        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'articles_index',
            'type' => 'articles_type',
        ];

        //多字段匹配
//        $params['body']['query']['multi_match']['query'] = '我的宝马发动机多少';
//        $params['body']['query']['multi_match']['fields'] = ["title","content"];
//        $params['body']['query']['multi_match']['type'] ="most_fields"; // most_fields 多字段匹配度更高   best_fields  完全匹配占比更高
//
//        //单个字段匹配
//        $params['body']['query']['match']['content'] =  '我的宝马多少马力';

        //完全匹配
//        $params['body']['query']['match_phrase']['content'] =  '我的宝马多少马力';


        //联合搜索  must,should,must_not
        $params['body']['query']["bool"]['must']["match"]['content'] = "宝马";
        $params['body']['query']["bool"]['must_not']["match"]['title'] = "宝马";

        $res = $client->search($params);
        print_r($res);

    }

}