itrnka/ha-elasticsearch-middleware

Provides access to official Elasticsearch PHP API as a middleware implementation for ha framework. Automatically creates a client based on the configuration when it starts the application.

v1.0.2 2017-11-15 00:00 UTC

This package is not auto-updated.

Last update: 2024-04-28 02:37:43 UTC


README

Provides access to official Elasticsearch PHP API as a middleware implementation for ha framework. Automatically creates a client based on the configuration when it starts the application.

Installation

Installation is available via composer:

composer require itrnka/ha-elasticsearch-middleware

Requirements

This package is based on ha framework. Composer installs ha framework and Elasticsearch PHP API automatically if it is not already installed.

Configuration

Required configuration keys:

  • name: by ha framework requirements
  • hosts: string[] list of elasticsearch hosts

Add your configuration to the configuration file in ha framework according to this example:

$cfg['middleware'] = [

    // ...

    // elasticsearch - single server
    [
        ha\Middleware\NoSQL\Elasticsearch\Elasticsearch::class,
        [
            'name' => 'ES001',
            'hosts' => ['127.0.0.1:9200'],
        ]
    ],

    // elasticsearch - multi server
    [
        ha\Middleware\NoSQL\Elasticsearch\Elasticsearch::class,
        [
            'name' => 'ES002',
            'hosts' => ['10.10.10.1:9200', '10.10.10.2:9200'],
        ]
    ],

    // ...
];

Then the elasticsearch will be available as follows:

// middleware instance
$es1 = main()->middleware->ES001;
$es2 = main()->middleware->ES002;

// es client (instance of \Elasticsearch\Client):
$es1Client = main()->middleware->ES001->driver();
$es2Client = main()->middleware->ES002->driver();

// or (this is the same)
$es1Client = main()->middleware->ES001->client();
$es2Client = main()->middleware->ES002->client();