进入项目目录
在 composer.json 文件中引入 elasticsearch-php:
"require": {
"php": ">=5.6.0",
"topthink/framework": "5.1.*",
"elasticsearch/elasticsearch": "~6.0"
},
//最后一行为新增
用 composer 安装客户端:
curl -s http://getcomposer.org/installer | php
php composer.phar install --no-dev
在项目中引入自动加载文件(如果还没引入),并且实例化一个客户端:
<?php
namespace app\index\controller;
#require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Index
{
public function elasticsearch(){
$hosts = [ 'localhost:9200' ];//本地使用localhost 也可指定IP
$client = ClientBuilder::create() ->setHosts($hosts) ->build();
$params = [
'index' => 'accounts',
'type' => 'person',
'id' => 1,
'client' => [ 'ignore' => 404 ]
];
print_r ($client->get($params));
}
}
返回
更多参阅官方文档 https://www.elastic.co/guide/cn/elasticsearch/php/current/index.html