Connect PHP to Elasticsearch

Connect PHP to Elasticsearch

Elasticsearch is a RESTful search and analytics engine capable to perform super fast full-text and other complex searches. In PHP REST API we easily create requests for creating, deleting, updating and retrieving of data. Elasticsearch is developed in Java.

Installing Elasticsearch

To install Elasticsearch we first need to install Java.

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

After That, we can install Java.

sudo apt-get install oracle-java8-installer

Now, download Elasticsearch using wget and manually download from official website click.

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.1-linux-x86_64.tar.gz

After That, Extract and Install.

mkdir elasticsearch
tar -xf elasticsearch-7.10.1-linux-x86_64.tar.gz -C es
cd elasticsearch

Start Elasticsearch using this command.

./bin/elasticsearch

Now we access Elasticsearch in browser using this url – http://localhost:9200 and we get this response:

{
  "status" : 200,
  "name" : "Jhon Doe",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "7.10.1",
    "build_hash" : "67ff9868b4c8a0c45864fhkd259e2dsfsdfd43",
    "build_timestamp" : "2021-01-04T09:21:06Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

Install PHP Client Library Using Composer:

Now we Install the PHP client library from official Github repository. You can download with composer.

1. Create a composer.json file in your project directory with below code.

 {
        "require": {
            "elasticsearch/elasticsearch": "^7.0"
        }
    }

2. Download and install Composer:

curl -s http://getcomposer.org/installer | php

3. Install your dependencies:

php composer.phar install

Connect PHP Client to Elasticsearch

Now create a new PHP file for the code that will interface with the Elasticsearch cluster. In elasticsearch, almost everything is configured by associative arrays. Include the autoloader in your file.

<?php

    use Elasticsearch\ClientBuilder;

    require 'vendor/autoload.php';

    $client = ClientBuilder::create()->build();

Next step to make connection with Elasticsearch Host, with Host, user and password,

$hosts = [
'host' => 'example.com',
'port' => '9200',
'scheme' => 'https',
'user' => 'username',
'pass' => 'password'
];

$client = ElasticsearchClientBuilder::create()
->setHosts($hosts)
->build();

Index a document

To index a document on Elasticsearch create associative arrays of key:value pairs with index, id and body parameters.

$params = [
    'index' => 'test_index',
    'id'    => '123abcd',
    'body'  => ['testField' => 'xyz']
];

$response = $client->index($params);
print_r($response);

After Successfully index document you get this response.

Array
(
    [_index] => test_index
    [_type] => _doc
    [_id] => 123abcd
    [_version] => 1
    [created] => 1
)

Get a document

Let’s get the document that we just indexed.

$params = [
    'index' => 'test_index',
    'id'    => '123abcd'
];

$response = $client->get($params);
print_r($response);

The response contains document metadata.

Array
(
    [_index] => test_index
    [_type] => _doc
    [_id] => 123abcd
    [_version] => 1
    [found] => 1
    [_source] => Array
        (
            [testField] => xyz
        )

)

Delete a document

$params = [
    'index' => 'test_index',
    'id'    => '123abcd'
];

$response = $client->delete($params);
print_r($response);
Array
(
    [found] => 1
    [_index] => test_index
    [_type] => _doc
    [_id] => 123abcd
    [_version] => 2
)

Leave a Reply

Your email address will not be published. Required fields are marked *