在php上使用,Elasticsearch,以及安装Elasticsearch-php

在php上使用需要安装Elasticsearch-php

安装需求:

PHP 7.0.0或更高版本.

需要安装: Composer.

ext-curl : PHP的Libcurl扩展 (我也不知道是啥php的一个扩展)

反正我没安装过 Libcurl 是可替代的...不知道.

原生 JSON 扩展 (ext-json) 1.3.7或更高版本(也没安装过).

Elasticsearch-php的版本需要和Elasticsearch版本适配(这个自己百度,安装6.0应该就可以了)

好了可以上面都是废话可以来安装了

在你的composer.json文件中增加这个

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

 

然后执行php composer.phar update

  • 基础配置   文件 config/app.php
// 地址端口   //这个必须配置正确不然没法用

'elasticsearch_host'          => ['127.0.0.1:9200'],
(一下默认配置即可)

//分片

'number_of_shards'            => 2,

//副本,如果只有一台机器,设置为0

'number_of_replicas'          => 0,

// 开启即可,否则某些功能不可用

'enabled'                     => 'true',
(这个现在只有一个索引不需要配置,以后可能需要)

// 索引名

'params_index'                => 'zb_log',

//type

'params_mappings'             => 'tp_log',

 

  • 基础使用
  1. 实例化

$Elas = new \app\common\controller\Elasticsearch();

会自动创建索引 与索引字段

索引字段:

name  日志名称

content 日志内容

addtime 创建时间

 

  1. 添加日志操作

$Elas ->addSingleElastics($name, $content)

参数:

$name  =  日志名称

$content  =  日志内容

返回数据:自动生成的id (暂时没啥用)

 

  1. 日志查询操作

$res = $Elas->getElastice(true);

参数:  默认false

说明:  该方法不传参数则视为不分页 传true 分页

返回数据:  与tp方法:select()、paginate() 返回一样、

id该id是自动生成的id只在删除时需要

 

  1. 查询条件方法  只能添加一个查询条件

$Elas->WhereElastice($where);

参数:

$where  =  [ 索引字段  =>  值 ]

说明: 在需要条件查询的时候 在查询方法(getElastice)前调用即可

  1. 删除某条日志

$res = $Elas->delElastice($id);

参数:

$id 添加时自动生成的id  查询时会返回出来

 

返回数据: 成功true 、失败false

你可能感兴趣的:(配置)