PHP 使用elasticsearch
composer require elasticsearch/elasticsearch
会自动加载合适的版本!我的php是7.1的,它会自动加载7.9的elasticsearch版本!
elasticsearch 的安装:
https://www.cnblogs.com/-mrl/p/13854210.html
elasticsearch的简单使用:
例子1:
<?php require 'vendor/autoload.php'; use Elasticsearch\ClientBuilder; //https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html#_quickstart //$client = ClientBuilder::create()->build(); //如果没有设置主机地址默认为127.0.0.1:9200 $client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); //var_dump($client); //索引一个文档 echo '索引一个文档'.PHP_EOL; $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => ['testField' => 'abc'] ]; $response = $client->index($params); pr($response); //获取一个文档 echo '获取一个文档'.PHP_EOL; $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->get($params); pr($response); echo '搜索一个文档'.PHP_EOL; //搜索一个文档 $params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ] ]; $response = $client->search($params); pr($response); //DIE; echo '删除一个文档'.PHP_EOL; //删除一个文档 $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->delete($params); pr($response); echo '删除一个索引'.PHP_EOL; //删除一个索引 $deleteParams = [ 'index' => 'my_index' ]; $response = $client->indices()->delete($deleteParams); pr($response); echo '创建一个索引'.PHP_EOL; //创建一个索引 $params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ] ]; $response = $client->indices()->create($params); pr($response); function pr($response){ echo '<pre>'; print_r($response); echo '</pre>'; }