课程推荐→:《elasticsearch全文搜索实战》(实战视频)
来自课程《千万级数据并发解决方案(理论+实战)》
php中使用elasticsearch
composer require elasticsearch/elasticsearch
会自动加载合适的版本!我的php是5.6的,它会自动加载5.3的elasticsearch版本!
using version ^5.3 for elasticsearch/elasticsearch./composer.json has been updatedloading composer repositories with package informationupdating dependencies (including require-dev)package operations: 4 installs, 0 updates, 0 removals - installing react/promise (v2.7.0): downloading (100%) - installing guzzlehttp/streams (3.0.0): downloading (100%) - installing guzzlehttp/ringphp (1.1.0): downloading (100%) - installing elasticsearch/elasticsearch (v5.3.2): downloading (100%) writing lock filegenerating autoload files
简单使用
<?phpclass myelasticsearch{ private $es; // 构造函数 public function __construct() { include('../vendor/autoload.php'); $params = array( '127.0.0.1:9200' ); $this->es = \elasticsearch\clientbuilder::create()->sethosts($params)->build(); } public function search() { $params = [ 'index' => 'megacorp', 'type' => 'employee', 'body' => [ 'query' => [ 'constant_score' => [ //非评分模式执行 'filter' => [ //过滤器,不会计算相关度,速度快 'term' => [ //精确查找,不支持多个条件 'about' => '谭' ] ] ] ] ] ]; $res = $this->es->search($params); print_r($res); }}
<?phprequire "./myelasticsearch.php";$es = new myelasticsearch();$es->search();
执行结果
array( [took] => 2 [timed_out] => [_shards] => array ( [total] => 5 [successful] => 5 [skipped] => 0 [failed] => 0 ) [hits] => array ( [total] => 1 [max_score] => 1 [hits] => array ( [0] => array ( [_index] => megacorp [_type] => employee [_id] => 3 [_score] => 1 [_source] => array ( [first_name] => 李 [last_name] => 四 [age] => 24 [about] => 一个php程序员,热爱编程,谭康很帅,充满激情。 [interests] => array ( [0] => 英雄联盟 ) ) ) ) ))
下面是官方的一些样例:
初始化require '../vendor/autoload.php';use elasticsearch\clientbuilder;$client = clientbuilder::create()->build();
增加配置
$hosts = [ '127.0.01:9200', // ip + port];$client = clientbuilder::create() // instantiate a new clientbuilder->sethosts($hosts) // set the hosts->build(); // build the client object
或
$hosts = [ '127.0.01:9200', // ip + port];$clientbuilder = clientbuilder::create(); // instantiate a new clientbuilder$clientbuilder->sethosts($hosts); // set the hosts$client = $clientbuilder->build(); // build the client object
插入一个文档// index 一个文档$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => ['testfield' => 'abc']];$response = $client->index($params);print_r($response);
获取一个文档$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id'];$response = $client->get($params);print_r($response);
查询一个文档$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testfield' => 'abc' ] ] ]];$response = $client->search($params);print_r($response);
删除一个文档$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id'];$response = $client->delete($params);print_r($response);
结果如下
array( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 3 [result] => deleted [_shards] => array ( [total] => 2 [successful] => 1 [failed] => 0 ) [_seq_no] => 2 [_primary_term] => 1)
删除一个索引$deleteparams = [ 'index' => 'my_index'];$response = $client->indices()->delete($deleteparams);print_r($response);
创建一个索引$params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ]];$response = $client->indices()->create($params);print_r($response);
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
php数据结构基础之栈
php操作beanstalkd的方法及参数注释
以上就是php中使用elasticsearch的方法的详细内容。