安装
1.在 composer.json 文件中引入 elasticsearch-php:
{
"require": {
"elasticsearch/elasticsearch": "~6.0" }}
2.用 composer 安装客户端:
curl -s http://getcomposer.org/installer | phpphp composer.phar install --no-dev
3.在项目中引入自动加载文件(如果还没引入),并且实例化一个客户端:
require 'vendor/autoload.php';use Elasticsearch\ClientBuilder;$client = ClientBuilder::create()->build();
索引一个文档
在 elasticsearch-php 中,几乎一切操作都是用关联数组来配置。REST 路径(endpoint)、文档和可选参数都是用关联数组来配置。
为了索引一个文档,我们要指定4部分信息:index,type,id 和一个 body。构建一个键值对的关联数组就可以完成上面的内容。body 的键值对格式与文档的数据保持一致性。(译者注:如 ["testField" ⇒ "abc"] 在文档中则为 {"testField" : "abc"}):
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id'