Elasticsearch-PHP 配置
2015-09-23 18:21
633 查看
配置
客户端的几乎每个方面都是可配置的。客户端是建立在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);
这将启用日志记录到elasticsearch.log 文件,它在你项目目录下。默认的日志级别是WARN。这个级别仅仅会描述你必要行为的重要事件。
通常情况下,你会想要修改日志文件位置或设置为组的可写权限。如果要修改这些东西,只需要做如下操作:
$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 );
相关文章推荐
- thinkphp 钩子的简单实现和总结
- OLAP与OLTP介绍
- URL::to() 与 URL::toRoute()
- php设计模式----工厂模式
- PHP大小写:函数名和类名不区分,变量名区分
- php实现简易聊天室应用代码
- ThinkPHP学习
- PHP error_reporting() 错误控制函数功能详解
- 利用PHP如何实现Socket服务器
- ubantu下配置ftp服务器
- 安装ecshop2.7时候的错误处理 php版本不兼容引起
- php防注入xss攻击
- php 详解spl_autoload_register()函数
- NRPE: Unable to read output 常见问题
- 10个超级有用的PHP代码片段果断收藏
- 两种不同版本的防注入函数
- thinkphp框架使用支付宝
- executing preg_match_all() function with this Regular Expression causes a PHP Segmentation Fault.
- ThinkPHP3.2中关联查询的代码收藏
- php常用的安全过滤函数