Simple PHP client for ElasticSearch
PHP Shell
Latest commit fbf3f41 Aug 11, 2016 @nervetattoo committed on GitHub Merge pull request #51 from pedropenduko/dev-preference
added support for preference
Failed to load latest commit information.
docs Moved array->string conversion out to a separate class, updated tests… Jul 24, 2010
.gitignore Updated ignore pattern to ignore Vagrant files Apr 9, 2015
.travis.yml Fixing travisci build Sep 25, 2012
README.markdown Remove `<?php` tags from "README.markdown" Jun 25, 2015
composer.json Change namespace of atoum package as its renamed on packagist Jan 9, 2013


Build Status

ElasticSearch PHP client

ElasticSearch is a distributed lucene powered search indexing, this is a PHP client for it


Initial setup

  1. Install composer. curl -s | php
  2. Create composer.json containing:

        "require" : {
            "nervetattoo/elasticsearch" : ">=2.0"
  3. Run ./composer.phar install
  4. Keep up-to-date: ./composer.phar update

Indexing and searching

require_once __DIR__ . '/vendor/autoload.php';

use \ElasticSearch\Client;
// The recommended way to go about things is to use an environment variable called ELASTICSEARCH_URL
$es = Client::connection();

// Alternatively you can use dsn string
$es = Client::connection('');

$es->index(array('title' => 'My cool document'), $id);

Creating mapping

    'title' => array(
        'type' => 'string',
    'index' => 'analyzed'

Search multiple indexes or types

$results = $es
    ->setIndex(array("one", "two"))
    ->setType(array("mytype", "other-type"))

Using the Query DSL

    'query' => array(
        'term' => array('title' => 'cool')

Provide configuration as array

Using an array for configuration also works

$es = Client::connection(array(
    'servers' => '',
    'protocol' => 'http',
    'index' => 'myindex',
    'type' => 'mytype'

Support for Routing

$document = array(
    'title' => 'My routed document',
    'user_id' => '42'
$es->index($document, $id, array('routing' => $document['user_id']));
$es->search('title:routed', array('routing' => '42'));

Support for Bulking

$document = array(
    'title' => 'My bulked entry',
    'user_id' => '43'
$es->index($document, $id, array('routing' => $document['user_id']));

    ->index($document, $id, 'myIndex', 'myType', array('parent' => $parentId));

Usage as a service in Symfony2

In order to use the Dependency Injection to inject the client as a service, you'll have to define it before. So in your bundle's services.yml file you can put something like this :

        class: ElasticSearch\Transport\HTTP
            - localhost
            - 9200
            - 60

        class: ElasticSearch\Client
            - @your_bundle.elastic_transport

To make Symfony2 recognize the ElasticSearch namespace, you'll have to register it. So in your app/autoload.php make sure your have :

// ...

    // ...
    'ElasticSearch' => __DIR__.'/path/to/your/vendor/nervetattoo/elasticsearch/src',

Then, you can get your client via the service container and use it like usual. For example, in your controller you can do this :

class FooController extends Controller
    // ...

    public function barAction()
        // ...
        $es = $this->get('your_bundle.elastic_client');
        $results = $es
            ->setIndex(array("one", "two"))
            ->setType(array("mytype", "other-type"))