您的位置:首页 > 编程语言 > PHP开发

Elasticsearch-PHP 快速开始

2015-09-18 18:08 776 查看

快速开始

本章节会给你一个客户端的主要功能(函数)是如何工作的快速概述。

安装

引入(包含)elasticsearch-php 在你的 composer.json
文件:

{
    "require": {
        "elasticsearch/elasticsearch": "~1.0"
    }
}


使用composer安装客户端:

curl -s http://getcomposer.org/installer | php
php composer.phar install


在主项目(一般是index.php)中引入autoloader.php文件(如果你还没有引入的话),并且要实例化Elasticsearch的客户端:

require 'vendor/autoload.php';

$client = new Elasticsearch\Client();


索引一个文档

在elasticsearch-php中,几乎所有的东西都是通过数组配置的。REST 的端点(终结点),文档和可选参数,一切都是一个关联数组。

去索引一个文档,我们简单的指定一个主体(body)来包含我们希望索引的文档。文档中的每一个字段都由一个关联数组的键/值对表示。

索引(index),类型(type)和 ID 也被指定在数组参数中,数组如下:

$params = array();
$params['body']  = array('testField' => 'abc');
$params['index'] = 'my_index';
$params['type']  = 'my_type';
$params['id']    = 'my_id';
$ret = $client->index($params);


获取一个文档

让我们来获取我们刚刚索引的文档:

$getParams = array();
$getParams['index'] = 'my_index';
$getParams['type']  = 'my_type';
$getParams['id']    = 'my_id';
$retDoc = $client->get($getParams);


搜索一个文档

搜索是 elasticsearch 的一个标志,所以让我们执行搜索。我们打算使用匹配查询作为示范:

$searchParams['index'] = 'my_index';
$searchParams['type']  = 'my_type';
$searchParams['body']['query']['match']['testField'] = 'abc';
$retDoc = $client->search($searchParams);


删除一个文档

好的,让我们继续删除一个我们之前添加的文档:

$deleteParams = array();
$deleteParams['index'] = 'my_index';
$deleteParams['type'] = 'my_type';
$deleteParams['id'] = 'my_id';
$retDelete = $client->delete($deleteParams);


删除一个索引

由于 elasticsearch 的动态性质,我们添加第一个文档的时候自动创建了索引和一些默认设置。让我们删除这个索引,因为我们以后想要指定自己的设置:

$deleteParams = array();
$deleteParams['index'] = 'my_index';
$client->indices()->delete($deleteParams);


创建一个索引

好吧,我们的索引被清空了,现在我们开始添加一个新的索引和一些自定义设置:

$indexParams['index'] = 'my_index';
$indexParams['body']['settings']['number_of_shards'] = 2;
$indexParams['body']['settings']['number_of_replicas'] = 0;
$client->indices()->create($indexParams);


总结

那些只是在客户端速成课程和语法上的概述。如果你熟悉elasticsearch, 你会注意到,这些方法的命名就像 REST 的端点(终结点)。

你还会发现客户端的配置方式使你发现通过你的IDE配置会非常方便。所有的核心操作都在 $client 对象(索引,搜索,获取等)下。索引和集群管理分别位于 $client->indices() 和 $client->cluster() 对象下。

查看剩下的文档去了解整个客户端是如何工作的。

例子代码

<?php
require 'vendor/autoload.php'; $client = new Elasticsearch\Client();

index($client);
//get($client);
// search($client);
// deleteDoc($client);
// deleteIndex($client);
// createIndex($client);
function index($client) {
$params = array ();
$params ['body'] = array (
'testField' => 'abc'
);
$params ['index'] = 'my_index';
$params ['type'] = 'my_type';
$params ['id'] = 'my_id';
try {
$ret = $client->index($params);
println("create index success");
} catch(Exception $e) {
echo $e->getMessage();
}
}

function get($client) {
$getParams = array ();
$getParams ['index'] = 'my_index';
$getParams ['type'] = 'my_type';
$getParams ['id'] = 'my_id';
$retDoc = $client->get($getParams);
println($retDoc);
}

function search($client) {
$searchParams ['index'] = 'my_index';
$searchParams ['type'] = 'my_type';
$searchParams ['body'] ['query'] ['match'] ['testField'] = 'abc';
$retDoc = $client->search($searchParams);
println($retDoc);
}

function deleteDoc($client) {
$deleteParams = array ();
$deleteParams ['index'] = 'my_index';
$deleteParams ['type'] = 'my_type';
$deleteParams ['id'] = 'my_id';
$retDelete = $client->delete($deleteParams);
println($retDelete);
}

function deleteIndex($client) {
$deleteParams = array ();
$deleteParams ['index'] = 'my_index';
$retDelete = $client->indices()->delete($deleteParams);
println($retDelete);
}

function createIndex($client) {
$indexParams ['index'] = 'my_index';
$indexParams ['body'] ['settings'] ['number_of_shards'] = 2;
$indexParams ['body'] ['settings'] ['number_of_replicas'] = 0;
$retCreate = $client->indices()->create($indexParams);
println($retCreate);
}

function println($var) {
echo "<br>";
$type = gettype($var);
if ($type == "array" || $type == "object") {
echo json_encode($var);
} else {
echo $var;
}
echo "<br>";
}


查看每个方法的运行结果:

index():

create index success


get():

{
    "_index": "my_index",
    "_type": "my_type",
    "_id": "my_id",
    "_version": 1,
    "found": true,
    "_source": {
        "testField": "abc"
    }
}


search():

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.30685282,
        "hits": [
            {
                "_index": "my_index",
                "_type": "my_type",
                "_id": "my_id",
                "_score": 0.30685282,
                "_source": {
                    "testField": "abc"
                }
            }
        ]
    }
}


deleteDoc():

{
    "found": true,
    "_index": "my_index",
    "_type": "my_type",
    "_id": "my_id",
    "_version": 2
}


deleteIndex():

{
    "acknowledged": true
}


createIndex():

{
    "acknowledged": true
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: