dev-master
9999999-devPHP No Blocking Client for Elasticsearch
Apache 2
The Requires
by Zachary Tong
search client yz-elasticsearch
Wallogit.com
2017 © Pedro Peláez
PHP No Blocking Client for Elasticsearch
Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in PHP; because of this it tries to be opinion-free and very extendable., (*1)
To maintain consistency across all the low-level clients (Ruby, Python, etc), clients accept simple associative arrays as parameters. All parameters, from the URI to the document body, are defined in the associative array., (*2)
| Branch | Unit Tests | Coverage |
|---|---|---|
![]() |
![]() |
![]() |
Full documentation can be found here. Docs are stored within the repo under /docs/, so if you see a typo or problem, please submit a PR to fix it!, (*3)
The recommended method to install Elasticsearch-PHP is through Composer., (*4)
elasticsearch/elasticsearch as a dependency in your project's composer.json file: {
"require": {
"elasticsearch/elasticsearch": "~0.4"
}
}
Download and install Composer:, (*5)
curl -s http://getcomposer.org/installer | php
Install your dependencies:, (*6)
php composer.phar install
Require Composer's autoloader, (*7)
Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process:, (*8)
<?php
require 'vendor/autoload.php';
$client = new Elasticsearch\Client();
You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at getcomposer.org., (*9)
In elasticsearch-php, almost everything is configured by associative arrays. The REST endpoint, document and optional parameters - everything is an associative array., (*10)
To index a document, we simply specify a body that contains the document that we wish to index. Each field in the document is represented by a different key/value pair in the associative array., (*11)
The index, type and ID are also specified in the parameters assoc. array:, (*12)
$params = array();
$params['body'] = array('testField' => 'abc');
$params['index'] = 'my_index';
$params['type'] = 'my_type';
$params['id'] = 'my_id';
$ret = $client->index($params);
Let's get the document that we just indexed:, (*13)
$getParams = array();
$getParams['index'] = 'my_index';
$getParams['type'] = 'my_type';
$getParams['id'] = 'my_id';
$retDoc = $client->get($getParams);
Searching is a hallmark of elasticsearch (no surprise there!), so let's perform a basic search. We are going to use the Match query as a demonstration:, (*14)
$searchParams['index'] = 'my_index';
$searchParams['type'] = 'my_type';
$searchParams['body']['query']['match']['testField'] = 'abc';
$queryResponse = $client->search($searchParams);
echo $queryResponse['hits']['hits'][0]['_id']; // Outputs 'abc'
Alright, let's go ahead and delete the document that we added previously:, (*15)
$deleteParams = array();
$deleteParams['index'] = 'my_index';
$deleteParams['type'] = 'my_type';
$deleteParams['id'] = 'my_id';
$retDelete = $client->delete($deleteParams);
Due to the dynamic nature of elasticsearch, the first document we added automatically built an index with some default settings. Let's delete that index because we want to specify our own settings later:, (*16)
$deleteParams['index'] = 'my_index';
$client->indices()->delete($deleteParams);
Ok, now that we are starting fresh, let's add a new index with some custom settings:, (*17)
$indexParams['index'] = 'my_index';
$indexParams['body']['settings']['number_of_shards'] = 2;
$indexParams['body']['settings']['number_of_replicas'] = 0;
$client->indices()->create($indexParams);
That was just a crash-course overview of the client and it's syntax. If you are familiar with elasticsearch, you'll notice that the methods are named just like REST endpoints., (*18)
You'll also notice that the client is configured in a manner that facilitates easy discovery via the IDE. All core actions are available under the $client object (indexing, searching, getting, etc). Index and cluster management are located under the $client->indices() and $client->cluster() objects, respectively., (*19)
Check out the rest of the Documentation to see how the entire client works., (*20)
Copyright 2013 Elasticsearch, (*21)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at, (*22)
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License., (*23)
PHP No Blocking Client for Elasticsearch
Apache 2
search client yz-elasticsearch