Skip to content

ulff/ElasticsearchPhpClientBundle

Repository files navigation

ElasticsearchPhpClientBundle

Setting up bundle

Version Matrix

You need to match your version of Elasticsearch to the appropriate version of this library.

Bundle Version Elasticsearch Version
2.0 5.0
1.0 >= 1.0, ⇐ 5.0

Step 1: Install bundle

Install bundle using composer:

php composer.phar require "ulff/elasticsearch-php-client-bundle:2.0"

Enable the bundle in AppKernel.php:

// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...
        new Ulff\ElasticsearchPhpClientBundle\UlffElasticsearchPhpClientBundle(),
    ];

    // ...
}

Step 2: add bundle configuration

Add following configuration to config.yml:

# app/config/config.yml

ulff_elasticsearch_php_client:
    elastic_host: "localhost"
    elastic_port: "9200"

Replace values with proper ones.

Usage

Client usage

Elasticsearch client is available as a service:

$client = $this->get('ulff_elasticsearch_php_client.client');

Create new index:

$indexParams = new IndexParams('index-name', 'type-name', 'id');
$indexParams->setBody(['someField' => 'some value']);
$response = $client->index($indexParams);

Returns IndexResponse object.

Get document:

$getParams = new GetParams('index-name', 'type-name', 'id');
$response = $client->get($getParams);

Returns GetResponse object.

Delete document:

$deleteParams = new DeleteParams('index-name', 'type-name', 'id');
$response = $client->delete($deleteParams);

Returns DeleteResponse object.

Delete by query:

$deleteParams = new DeleteByQueryParams('index-name', 'type-name');
$deleteParams->setBody([
   'query' => [
       'match_all' => new \stdClass(),
   ]
]);
$response = $client->deleteByQuery($deleteParams);

Returns DeleteByQueryResponse object.

Make search query:

$searchParams = new SearchParams('index-name', 'type-name');
$searchParams->setBody([
    'query' => [
        'match' => [
            'someField' => 'some value'
        ]
    ]
]);
$response = $client->search($searchParams);

Update document:

$updateParams = new UpdateParams('index-name', 'type-name', 'id');
$updateParams->setBody(['someField' => 'some value']);
$response = $client->update($updateParams);

Returns UpdateResponse object.

Purger usage

Bundle offers a possibility to purge whole index (by deleting and recreating empty). This can be useful e.g. for testing purposes.

There is a separate ulff_elasticsearch_php_client.purger service provided with the bundle. Following example shows how to purge index:

$purger = $this->get('ulff_elasticsearch_php_client.purger');
$purger->purgeIndex('index_name');

Full documentation:

This bundle is a client for:

Follow the documentation there.

License

This bundle is licensed under the MIT license. Please, see the complete license in the bundle LICENSE file.

About

A very simple bundle for integrating Symfony3 with Elasticsearch-PHP 2.0

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages