PHP Client Examples =================== Installation ------------- One way of connecting to Elasticsearch with PHP is using `composer `_. To do so, you'll need to include elasticsearch-php in your `composer.json` file: :: { "require": { "elasticsearch/elasticsearch": "~2.0" } } Then you'll want to install the client with composer: .. code-block:: bash $ curl -s http://getcomposer.org/installer | php $ php composer.phar install --no-dev Connecting ----------- The example below shows the connection settings using ``HTTPS``. If you prefer to use ``HTTP``, you only need to change ``$hosts`` to ``http'`` and ``port: 10202``. .. code-block:: php setHosts($hosts) ->build(); $params = [ 'index' => 'myindex', 'type' => 'mytype', 'id' => '2' ]; $response = $client->get($params); print_r($response); Index a document ---------------- .. code-block:: php setHosts($hosts) ->build(); $params = [ 'index' => 'phpindex', 'type' => 'my_type', 'id' => '1', 'body' => ['Description' => 'Hello World'] ]; $response = $client->index($params); print_r($response); Get a document --------------- .. code-block:: php setHosts($hosts) ->build(); $params = [ 'index' => 'phpindex', 'type' => 'my_type', 'id' => '1' ]; $response = $client->get($params); print_r($response); Search (DSL) ------------- .. code-block:: php setHosts($hosts) ->build(); $params = [ 'index' => 'phpindex', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'Description' => 'Hello World', ] ] ] ]; $response = $client->search($params); print_r($response); Delete a document ------------------ .. code-block:: php setHosts($hosts) ->build(); $params = [ 'index' => 'phpindex', 'type' => 'my_type', 'id' => '1' ]; $response = $client->delete($params); print_r($response);