thinkphp运用elasticsearch

PHP 收藏 1 317
Roderick VIP 2019-07-23 15:12:59

1.php环境 >5.3.8   使用composer安装elasticsearch-php

2.composer环境安装,注意:php_zip扩展打开会安装错误

3.在 composer.json 文件中增加 elasticsearch-php。如果你是新建项目,那么把以下的代码复制粘贴到 composer.json 就行了。如果是在现有项目中添加 elasticsearch-php,那么把 elasticsearch-php 添加到其它的包名后面即可:

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

4.使用  curl -s http://getcomposer.org/installer | php  命令来安装 composer.phar

 使用 php composer.phar install --no-dev  命令安装依赖并且创建一个 autoloader:

5.实例化使用elasticsearch了

Vendor('elasticsearch.autoload');
        if( config('site.duUrl') ){
            $params = array(
                '192.168.0.98:9200'
            );
        }
        $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build();
查询
$params = [
            'index' => 'visitlog-*',
            'type' => 'logs',
            'body' => [
                'query' => [
                    'bool' => [
                        'must'=> [
                             ['term' => [
                                'srcIp' => '192.168.0.1'   //精确查询
                             ]]

['range' => [

'coef' => ['gte'=>0.8,'lte'=>1.0] //范围查询

]]

['wildcard' => [ 'url'=>'*www.baidu.com*', //模糊查询 ] ] ] ], 'sort' => ['destIp'=>['order'=>'desc']], //排序 'from' => 0, 'size' => 20 //数据范围 ] 'client' => [ 'timeout' => 10, //超时时间设置 'connect_timeout' => 10, ] ]; $params = $this->visitlogParams($where); try { $res = $this->es->search($params); } catch (\Exception $e) { return $res = null; } foreach ($res['hits']['hits'] as $key=>$value){ $res['hits']['hits'][$key] = $value['_source']; } return $res;
增改删php运用不大

评论