初次接触分布式是全文搜索引擎,之前都是spinx+coreseek,先简单实现初步了解先
官方文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/logging.html
一.安装
es依赖java8,java8的具体安装就不做记录了,网上很多 看这里
我是在windows下安装使用,linux下可以看这篇博文 看这里
软件下载 看这里
下完解压后,打开cmd在bin目录执行elasticsearch
报错,因为写好前没有保存,电脑莫名关机了,所以错误具体内容忘记了,唉...做好记录并保存真的很重要
[2019-04-02T10:33:01,491][INFO ][o.e.c.r.a.DiskThresholdMonitor] [5wkvRes] low disk watermark [85%] exceeded on [5wkvResTRia0NvQqno0Rjw][5wkvRes][D:\App_self\elasticsearch\elasticsearch-6.7.0\data\nodes\0] free: 9.4gb[13.7%], replicas will not be assigned to this node
在config下的elasticsearch.yml文件中添加了以下代码,ok了
cluster.routing.allocation.disk.threshold_enabled: false #cluster.routing.allocation.disk.watermark.low: 30gb #cluster.routing.allocation.disk.watermark.high: 20gb
排出后在执行之前命令,started安装完成。可以另起窗口使用curl 127.0.0.1:9200命令测试下,输出
{ "name" : "5wkvRes", "cluster_name" : "elasticsearch", "cluster_uuid" : "DEQGhogJQ3mKRQpKv-OedQ", "version" : { "number" : "6.7.0", "build_flavor" : "default", "build_type" : "zip", "build_hash" : "8453f77", "build_date" : "2019-03-21T15:32:29.844721Z", "build_snapshot" : false, "lucene_version" : "7.7.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
因为es默认占用的是9200节点,所以也可以使用tcping 127.0.0.1 9200命令测试下,port is open,ok已经被打开。
二.简单使用
es依赖composer,所以需要使用composer进行安装
新建目录下创建文件composer.json,并写入
{ "require":{ "elasticsearch/elasticsearch":"~6.0" } }
根目录下执行composer install
好了,多出了vendor和composer.lock
新建Elasticsearch.php:
<?php require_once('vendor/autoload.php'); use Elasticsearch\ClientBuilder; use Monolog\Logger; class Elasticsearch{ private $config = array( 'hosts' => ['http://127.0.0.1:9200'] ); private $api; public function __construct(){ $logger = ClientBuilder::defaultLogger('path/to/log.log',Logger::DEBUG); $this->api = ClientBuilder::create()->setHosts($this->config['hosts'])->setLogger($logger)->build(); } /** * 新增one * @return [type] [description] */ public function addOne(){ $params = []; $params['index'] = 'demo'; $params['type'] = 'cat'; $params['id'] = '20190329001'; # 不指定就是es自动分配 $params['body'] = array('name' => 'es编程'); return $this->api->index($params); } /** * 批量新增 * 注意,索引没有被创建时会自动创建索引 */ public function addAll(){ $params = [];//echo 1; for($i = 1;$i < 1001;$i++){ $params['body'][] = [ 'index' => [ '_index' => 'test_index'.$i, '_type' => 'cat_test', '_id' => $i, //'client'=> ['client' => [ // 'timeout'=> 10,//十秒超时 // 'connect_timeout'=> 10 //]] ] ]; $params['body'][] = [ 'name' => 'elasticsearch'.$i, 'content' => '内容'.$i ]; }//var_dump($params);die; return $this->api->bulk($params); } /** * 获取一个文档 */ public function getOne(){ $params = []; $params['index'] = 'demo'; $params['type'] = 'cat'; $params['id'] = '20190329001'; return $this->api->get($params); } /** * 搜索文档 */ public function search(){ $params = []; $params['index'] = 'test_index4'; return $this->api->search($params); } /** * 删除文档 * 注意,删除文档后并不会删除对应索引 */ public function delete(){ $params = []; $params['index'] = 'demo'; $params['type'] = 'cat'; $params['id'] = '20190329001'; return $this->api->delete($params); } /** * 创建索引 */ public function createIndex(){ $params = []; $params['index'] = 'demo'; return $this->api->indices()->create($params); } /** * 删除索引:匹配单个 | 匹配多个 * 说明: 索引删除后,索引下的所有文档也会被删除 */ public function deleteIndex() { $params = []; $params['index'] = 'test_index'; # 删除test_index单个索引 #$params['index'] = 'test_index*'; # 删除以test_index开始的所有索引 return $this->api->indices()->delete($params); } /** * 设置索引配置 */ public function setIndexConfig() { $params = []; $params['index'] = 'demo'; $params['body']['index']['number_of_replicas'] = 0; $params['body']['index']['refresh_interval'] = -1; return $this->api->indices()->putSettings($params); } /** * 获取索引配置 */ public function getIndexConfig() { # 单个获取条件写法 $params['index'] = 'demo'; # 多个获取条件写法 //$params['index'] = ['demo', 'test_index']; return $this->api->indices()->getSettings($params); } /** * 设置索引映射 */ public function setIndexMapping() { # 设置索引和类型 $params['index'] = 'demo'; $params['type'] = 'cat'; # 向现有索引添加新类型 $myTypeMapping = array( '_source' => array( 'enabled' => true ), 'properties' => array( 'first_name' => array( 'type' => 'string', 'analyzer' => 'standard' ), 'age' => array( 'type' => 'integer' ) ) ); $params['body']['cat'] = $myTypeMapping; # 更新索引映射 $this->api->indices()->putMapping($params); } /** * 获取索引映射 */ public function getIndexMapping() { # 获取所有索引和类型的映射 $ret = $this->api->indices()->getMapping(); /* # 获取索引为:demo的映射 $params['index'] = 'demo'; $ret = $this->api->indices()->getMapping($params); # 获取类型为:cat的映射 $params['type'] = 'cat'; $ret = $this->api->indices()->getMapping($params); # 获取(索引为:demo和 类型为:cat)的映射 $params['index'] = 'demo'; $params['type'] = 'cat' $ret = $this->api->indices()->getMapping($params); # 获取索引为:demo和test_index的映射 $params['index'] = ['demo', 'test_index']; $ret = $this->api->indices()->getMapping($params); */ return $ret; } } ?>
新建index.php文件
<?php require_once('Elasticsearch.php'); $es = new Elasticsearch(); //$addOne = $es -> addOne(); //$getOne = $es -> getOne(); //$delete = $es -> delete(); //$addAll = $es -> addAll(); $getIndexMapping = $es -> getIndexMapping(); //$search = $es -> search(); var_dump($getIndexMapping); ?>