一、安装
以下es基于6.4
1、在 composer.json 文件中引入 elasticsearch-php:
"require":{
"elasticsearch/elasticsearch":"~6.0",
"monolog/monolog": "~1.0"
}
}
2、用 composer 安装客户端:
php composer.phar install --no-dev
二、快速开始
1、创建一个test.php文件,内容如下
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'192.168.16.241:9200', // IP + Port
'192.168.16.241', // Just IP
'localhost:9200', // Domain + Port
'localhost', // Just Domain
'http://localhost', // SSL to localhost
'https://192.168.16.241:9200' // SSL to IP + Port
];
$client = ClientBuilder::create()->setHosts($hosts)->build(); // Instantiate a new ClientBuilder // Set the hosts
$params = [
'index' => 'test_data',
'type' => 'users',
'id' => 100027,
'client' => [ 'ignore' => 404 ]
];
var_dump( $client->get($params));
2、浏览器访问test.php,结果如下(前提是你的es已经有数据)
三、基本操作
1、创建索引
$params = [
'index' => 'test_index'
];
// Create the index
print_r($client->indices()->create($params));
2、创建索引(指定模板)
$params = [
'index' => 'test_index',
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 2
],
'mappings' => [
'test_type' => [
'_source' => [
'enabled' =>