﻿---
title: Getting started
description: This page guides you through the installation process of the PHP client, shows you how to instantiate the client, and how to perform basic Elasticsearch...
url: https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/php/getting-started
products:
  - Elasticsearch
  - Elasticsearch Client
  - Elasticsearch PHP Client
---

# Getting started
This page guides you through the installation process of the PHP client, shows you how to instantiate the client, and how to perform basic Elasticsearch operations with it.

### Requirements

- [composer](http://getcomposer.org)

If you don’t have composer you can install it by running the following commands:
```shell
curl -s http://getcomposer.org/installer | php
php composer.phar install
```


### Installation

To install the latest version of the client, run the following command:
```shell
composer require elasticsearch/elasticsearch
```

When you have installed elasticsearch-php you can start using it with the `Client` class. You can use the `ClientBuilder` class to create this object:
```php
require 'vendor/autoload.php';

$client = Elastic\Elasticsearch\ClientBuilder::create()->build();
```

Refer to the [*Installation*](https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/php/installation) page to learn more.

### Connecting

You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint.
```php
$client = ClientBuilder::create()
   ->setHosts(['<elasticsearch-endpoint>'])
   ->setApiKey('<api-key>')
   ->build();
```

Your Elasticsearch endpoint can be found on the ***My deployment*** page of your deployment:
![Finding Elasticsearch endpoint](https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/php/images/es_endpoint.jpg)
You can generate an API key on the ***Management*** page under Security.
![Create API key](https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/php/images/create_api_key.png)
For other connection options, refer to the [*Connecting*](https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/php/connecting) section.

### Operations

Time to use Elasticsearch! This section walks you through the basic, and most important, operations of Elasticsearch. For more operations and more advanced examples, refer to the [*Operations*](https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/php/operations) page.

#### Creating an index

This is how you create the `my_index` index:
```php
$client = ClientBuilder::create()->build();
$params = [
    'index' => 'my_index'
];

// Create the index
$response = $client->indices()->create($params);
```


#### Indexing documents

This is a simple way of indexing a document:
```php
$params = [
    'index' => 'my_index',
    'body'  => [ 'testField' => 'abc']
];

// Document will be indexed to my_index/_doc/<autogenerated ID>
$response = $client->index($params);
```

You can bulk index documents with batches in a slightly more complex way:
```php
$params = ['body' => []];

for ($i = 1; $i <= 1234567; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'my_index',
            '_id'    => $i
        ]
    ];

    $params['body'][] = [
        'my_field'     => 'my_value',
        'second_field' => 'some more values'
    ];

    // Every 1000 documents stop and send the bulk request
    if ($i % 1000 == 0) {
        $responses = $client->bulk($params);

        // erase the old bulk request
        $params = ['body' => []];

        // unset the bulk response when you are done to save memory
        unset($responses);
    }
}

// Send the last batch if it exists
if (!empty($params['body'])) {
    $responses = $client->bulk($params);
}
```


#### Getting documents

You can get documents by using the following code:
```php
$params = [
    'index' => 'my_index',
    'id'    => 'my_id'
];

// Get doc at /my_index/_doc/my_id
$response = $client->get($params);
```


#### Searching documents

This is how you can create a single match query with the PHP client:
```php
$params = [
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$results = $client->search($params);
```


#### Updating documents

This is how you can update a document, for example to add a new field:
```php
$params = [
    'index' => 'my_index',
    'id'    => 'my_id',
    'body'  => [
        'doc' => [
            'new_field' => 'abc'
        ]
    ]
];

// Update doc at /my_index/_doc/my_id
$response = $client->update($params);
```


#### Deleting documents

```php
$params = [
    'index' => 'my_index',
    'id'    => 'my_id'
];

// Delete doc at /my_index/_doc_/my_id
$response = $client->delete($params);
```


#### Deleting an index

```php
$params = ['index' => 'my_index'];
$response = $client->indices()->delete($params);
```


## Further reading

- Use [*Client helpers*](https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/php/client-helpers) for a more confortable experience with the APIs.