2017 © Pedro Peláez
 

library elasticsearch-client

A lightweight PHP client for Elasticsearch

image

novaway/elasticsearch-client

A lightweight PHP client for Elasticsearch

  • Thursday, July 12, 2018
  • by skwi
  • Repository
  • 6 Watchers
  • 4 Stars
  • 2,879 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 6 Open issues
  • 12 Versions
  • 18 % Grown

The README.md

Novaway ElasticSearch Client

Note : this project is discontinued in favor of the ElasticsearchBundle, as it was more or less trying to do what elastica does already well, (*1)

A lightweight PHP 7.0+ client for Elasticsearch, providing features over Elasticsearch-PHP, (*2)

Compatibility

This branch is tested and compatible with ElasticSearch 6.*, (*3)

The compatibility with ElasticSearch 5.* is supported, and should work, but is to be considered hazardous., (*4)

Installation

Install using composer:, (*5)

$ composer require novaway/elasticsearch-client

Usage

Create an index

The first thing you'll need to do to use this library is to instatiate an index. This will be the keystone of the client., (*6)

$index = new \Novaway\ElasticsearchClient\Index(
    ['127.0.0.1:9200'],     # elasticsearch hosts
    'main_index',               # index name
    [
        'settings' => [
            'number_of_shards' => 3,
            'number_of_replicas' => 2
        ],
        'mappings' => [
            'my_type' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'first_name' => [
                        'type' => 'string',
                        'analyzer' => 'standard'
                    ],
                    'age' => [
                        'type' => 'integer'
                    ]
                ]
            ]
        ]
    ]    
);

Index an object

In order to be searched, objects should be indexed as a serialized version. In order to be indexed, Object should implement \Novaway\ElasticsearchClient\Indexable interface., (*7)

By default, objects are serialized with Elasticsearch-PHP's SmartSerializer, but you can choose to use a custom serializer., (*8)

$objectIndexer = new \Novaway\ElasticsearchClient\ObjectIndexer($index);
$objectIndexer->index($object, 'my_type');

Remove an object from index

To remove an object from the index, the process is still, (*9)

$objectIndexer = new \Novaway\ElasticsearchClient\ObjectIndexer($index);
$objectIndexer->remove($object, 'my_type');

// Alternatively, you can remove an indexed object knowing only it's ID.
$objectIndexer->removeById($objectId, 'my_type');

Search the index

Basic match query

First, create a QueryExecutor., (*10)

$queryExecutor = new \Novaway\ElasticsearchClient\QueryExecutor($index);

Use the QueryBuilder to build your query and execute it., (*11)

use Novaway\ElasticsearchClient\Query\CombiningFactor;

$queryBody = QueryBuilder::createNew()
                    ->match('first_name', 'John', CombiningFactor::MUST)
                    ->getQueryBody()
;
$queryExecutor->execute($queryBody, 'my_type');

The QueryBuilder allow you to define a limit and an offset for a search result, and choose the minimum score to display., (*12)

const MIN_SCORE = 0.4;
const OFFSET = 0;
const LIMIT = 10;

$queryBuilder = QueryBuilder::createNew(0, 10, 0.3);

Advanced Querying

This client provide several ways to improve querying :, (*13)

  • Filtering (missing documentation)
  • Aggregations
  • Result Formating (missing documentation)

Clear the index

You might want, for some reason, to purge an index. The reload method drops and recreates the index., (*14)

$index->reload();

Hotswapping

You will want to reindex all your data sometimes., (*15)

It is possible to do it without downtime using the hotswap mechanisme, (*16)

$index->hotswapToTmp();
// at that point, all your search request will go to the tmp index, and your create/delete will go to the main index
// when your are done reindexing your data, simply call 
$index->hotswapToMain()

If you are using this library in a symfony project, we recommend to use it as service., (*17)

# services.yml
parameters:
    myapp.search.myindex.config:
        settings:
            number_of_shards : 1
            number_of_replicas : 1
        mappings:
            my_type:
                _source : { enabled : true }
                properties:
                    first_name:
                        type: string
                        analyzer: standard
                    age:
                        type: integer

services:
    myapp.search.index:
        class: Novaway\ElasticsearchClient\Index
        arguments:
            - ['127.0.0.1:9200'] #define it in the parameter.yml file
            - 'myapp_myindex_%kernel.environment%'
            - 'myapp.search.myindex.config'

    myapp.search.object_indexer:
        class: Novaway\ElasticsearchClient\ObjectIndexer
        arguments:
            - '@myapp.search.index'

    myapp.search.query_executor:
        class: Novaway\ElasticsearchClient\QueryExecutor
        arguments:
            - '@myapp.search.index'

Then you'll only have to work with the myapp.search.object_indexer and myapp.search.query_executor services., (*18)

Testing

A testing environment is provided using a dockerized version of elasticsearch., (*19)

Testing is done using the Atoum framework for unit testing and the Behat framework for behavior testing., (*20)

A Makefile provide useful commands for testing, so you can run the full test suite by running :, (*21)

$ make test

License

This library is published under MIT license, (*22)

The Versions

15/11 2017

dev-revert-20-match-query

dev-revert-20-match-query

A lightweight PHP client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

elasticsearch

03/10 2017
02/10 2017
07/09 2017

dev-aggregations-master

dev-aggregations-master

A lightweight PHP client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

elasticsearch

07/09 2017
17/05 2017

0.1.4

0.1.4.0

A lightweight PHP client for Elasticsearch

  Sources   Download

MIT

The Requires

 

elasticsearch

17/05 2017

0.1.3

0.1.3.0

A lightweight PHP client for Elasticsearch

  Sources   Download

MIT

The Requires

 

elasticsearch

07/05 2017

0.1.2

0.1.2.0

A lightweight PHP client for Elasticsearch

  Sources   Download

MIT

The Requires

 

elasticsearch

05/05 2017

0.1.1

0.1.1.0

A lightweight PHP client for Elasticsearch

  Sources   Download

MIT

The Requires

 

elasticsearch

04/05 2017

0.1

0.1.0.0

A lightweight PHP client for Elasticsearch

  Sources   Download

MIT

The Requires

 

elasticsearch