Elasticsearch-PHP 配置
来源:互联网 发布:windows defender开启 编辑:程序博客网 时间:2024/06/05 21:08
配置
客户端的几乎每个方面都是可配置的。客户端是建立在Pimple一个轻量级的依赖注入容器之上的。大多数用户只需要配置几个参数来满足他们的要求。
然而,由于容器控制着所有对象实例化,用户有可能通过客户端改变内部组件。用户可以,例如,编写和使用一个自定义连接池类来替换默认的连接池装载在客户端中。
主机配置
一个常见的操作会告诉客户端哪些节点在集群中。默认情况下,客户端连接到 localhost:9200,明显,许多生产环境不是这样工作的。
所有的配置(简单的参数和高级的组建替换)都被注入到客户端的构造函数中。
客户端接受一个你想连接到的主机数组,数组中的每一个值必须是字符串(包含或不包含端口号)。通过添加主机 https 前缀来启用SSL:
$params = array();$params['hosts'] = array ( '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 'http://user:pass@localhost:9200', // HTTP Basic Auth 'https://user:pass@localhost:9200', // SSL + HTTP Basic Auth);$client = new Elasticsearch\Client($params);
忽略异常
库会尝试抛出一些常见问题的异常,这些异常匹配着由Elasticsearch提供的HTTP响应码。例如,尝试获取一个不存在的文档会抛出 MissingDocument404Exception。
异常是一个有用且一致的方式去处理像缺少文档,语法错误,版本冲突等之类的问题。但是,有时候你会想要处理响应正文而不是捕获异常(通常在测试中使用)。
如果你需要这种行为,你可以配置一个忽略参数。以下设置会忽略MissingDocument404Exception 异常,相反,会返回由Elasticsearch 提供的JSON字符串。
$client = new Client();$params = array( 'index' => 'test_missing', 'type' => 'test', 'id' => 1, 'ignore' => 404 //这个会忽略404丢失/缺少异常);echo $client->get($params);> {"_index":"test_missing","_type":"test","_id":"1","found":false}$params = array( 'index' => 'test_missing', 'type' => 'test', 'ignore' => [400, 404] //ignore也可以接受一个要忽略的异常数组,BadRequest404Exception 会被忽略);echo $client->get($params);> No handler found for uri [/test_missing/test/] and method [GET]
应该被提到的是,响应是一个简单的字符串,该字符串可能不能被JSON格式编码。在第一个例子中,响应正文是一个完整的JSON对象,可以被JSON解码。在第二个例子中,是一个简单的字符串。
由于客户端没有办法知道响应正文会包含什么内容,没有常识去破解它。
提供自定义查询参数
有时,你需要提供自定义的查询参数,例如第三方的插件身份验证领牌或代理。所有的查询参数都在Elasticsearch-PHP 的白名单中,这是为了保护你避免Elasticsearch指定的不接受的参数。
如果你需要自定义参数,你需要绕过这个白名单机智。这样做,添加自定义参数作为数组的值:
$getParams = array( 'index' => 'test', 'type' => 'test', 'id' => 1, 'parent' => 'abc', // white-listed 'custom' => array('customToken' => 'abc', 'otherToken' => 123) // user-defined, not white listed, not checked);$exists = $client->exists($getParams);
配置日志
默认情况下,出于性能原因客户端日志是禁用的,如果你想启用日志,简单的设置日志参数为true:
$params = array();$params['logging'] = true;$client = new Elasticsearch\Client($params);
通常情况下,你会想要修改日志文件位置或设置为组的可写权限。如果要修改这些东西,只需要做如下操作:
$params = array();$params['logging'] = true;$params['logPath'] = '/var/logs/elasticsearch/elasticsearch.log';$params['logPermission'] = 0664;$client = new Elasticsearch\Client($params);
不是所有的参数都是字符串。例如,我们可以修改客户端的日志级别:
$params = array();$params['logging'] = true;$params['logPath'] = '/var/logs/elasticsearch/elasticsearch.log';$params['logLevel'] = Psr\Log\LogLevel::INFO;$client = new Elasticsearch\Client($params);
默认情况下,客户端使用一个Monolog框架提供的基于文件的日志记录器,Monolog提供了各种各样的日志记录器。例如,我们可以指示客户端把日志文件记录在SysLog中,而不是文件中。
use Monolog\Logger;// Build a Monolog logger that uses the SyslogHandler$logger = new Logger('log');$handler = new SyslogHandler('my_user');$processor = new IntrospectionProcessor();$logger->pushHandler($handler);$logger->pushProcessor($processor);// Over-ride the client's logger object with your own$params['logging'] = true;$params['logObject'] = $logger;$client = new Elasticsearch\Client($params);
客户端使用通用的PSR\Log 接口,这意味着任何的PSR\Log 日志记录器都会在你的客户端工作的很好。
使用另外一种兼容的PSR\Log日志记录器去替换日志记录器就像前边配置使用Monolog日志记录器像似。
use Monolog\Logger;$logger = new MySpecialPSRLogger();$params['logging'] = true;$params['logObject'] = $logger;$client = new Elasticsearch\Client($params);
完整的配置列表
默认配置
$paramDefaults = array( 'connectionClass' => '\Elasticsearch\Connections\GuzzleConnection', 'connectionFactoryClass'=> '\Elasticsearch\Connections\ConnectionFactory', 'connectionPoolClass' => '\Elasticsearch\ConnectionPool\StaticNoPingConnectionPool', 'selectorClass' => '\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector', 'serializerClass' => '\Elasticsearch\Serializers\SmartSerializer', 'sniffOnStart' => false, 'connectionParams' => array(), 'logging' => false, 'logObject' => null, 'logPath' => 'elasticsearch.log', 'logLevel' => Log\LogLevel::WARNING, 'traceObject' => null, 'tracePath' => 'elasticsearch.log', 'traceLevel' => Log\LogLevel::WARNING, 'guzzleOptions' => array(), 'connectionPoolParams' => array( 'randomizeHosts' => true ), 'retries' => null);
- Elasticsearch-PHP 配置
- Elasticsearch配置
- Elasticsearch配置
- elasticsearch配置
- elasticsearch配置
- elasticsearch配置
- ElasticSearch配置
- elasticsearch配置
- ElasticSearch配置
- elasticsearch配置
- Elasticsearch查询配置(Elasticsearch+springboot)
- 【配置】Elasticsearch安装配置
- PHP ElasticSearch的使用
- PHP 中运用 elasticsearch
- Elasticsearch-PHP 概述
- Elasticsearch-PHP 快速开始
- Elasticsearch-PHP 安装
- Elasticsearch-PHP 索引操作
- Mvvmcross Plugins - Cirrious.MvvmCross.Plugins.Messenger
- 【html5多媒体】自定义播放器(弹幕,水印,甚至在视频开头添加封面)
- UITableView
- 分享一款HI3520D实现HDMI产品上用的大容量存储器
- 上传时生成缩略图
- Elasticsearch-PHP 配置
- 这个博客地址对于caffe初学者十分有帮助
- VC窗体透明而控件不透明以及Static文本背景透明方法
- shell 编程实例练习(二)
- UI的布局优化
- 开通新博客啦
- ssh 免密码登录 密钥登录
- 使用js对cookie操作
- Shell编程样例一篇