Composer Installation

edit
  • Include elasticsearch-php in your composer.json file. If you are starting a new project, simply paste the following JSON snippet into a new file called composer.json. If you have an existing project, include this requirement under the rest of requirements already present:

    {
        "require": {
            "elasticsearch/elasticsearch": "~2.0@beta"
        }
    }
  • Install the client with composer. The first command download the composer.phar PHP package, and the second command invokes the installation. Composer will automatically download any required dependencies, store them in a /vendor/ directory and build an autoloader.:

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

    More information about Composer can be found at their website.

  • Finally, include the generated autoloader in your main project. If your project is already based on Composer, the autoloader is likely already included somewhere and you don’t need to add it again. Finally, instantiate a new client:

    require 'vendor/autoload.php';
    
    $client = Elasticsearch\ClientBuilder::create()->build();

    Client instantiation is performed with a static helper function create(). This creates a ClientBuilder object, which helps you to set custom configurations. When you are done configuring, you call the build() method to generate a Client object. We’ll discuss configuration more in the Configuration section