Quickstartedit

This section will give you a quick overview of the client and how the major functions work.

Installationedit

  • Include elasticsearch-php in your composer.json file:

    {
        "require": {
            "elasticsearch/elasticsearch": "~1.0"
        }
    }
  • Install the client with composer:

    curl -s http://getcomposer.org/installer | php
    php composer.phar install
  • Include the autoloader in your main project (if you haven’t already), and instantiate a new client :

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

Index a documentedit

In elasticsearch-php, almost everything is configured by associative arrays. The REST endpoint, document and optional parameters - everything is an associative array.

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.

The index, type and ID are also specified in the parameters assoc. array:

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

Get a documentedit

Let’s get the document that we just indexed:

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

Search for a documentedit

Searching is a hallmark of elasticsearch, so let’s perform a search. We are going to use the Match query as a demonstration:

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

Delete a documentedit

Alright, let’s go ahead and delete the document that we added previously:

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

Delete an indexedit

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:

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

Create an indexedit

Ok, now that we are starting fresh, let’s add a new index with some custom settings:

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

Wrap upedit

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.

You’ll also notice that the client is configured in a manner that facilitates easy discovery via your 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.

Check out the rest of the Documentation to see how the entire client works.