Skip to content

Latest commit

 

History

History
313 lines (232 loc) · 6.71 KB

README.md

File metadata and controls

313 lines (232 loc) · 6.71 KB

Elastic Adapter

Latest Stable Version Total Downloads License Build Status Donate PayPal Donate Amazon


Elastic Adapter is an adapter for official PHP Elasticsearch client. It's designed to simplify basic index and document operations.

Contents

Compatibility

The current version of Elastic Adapter has been tested with the following configuration:

  • PHP 7.2-7.4
  • Elasticsearch 7.x

Installation

The library can be installed via Composer:

composer require babenkoivan/elastic-adapter

Index Management

IndexManager can be used to manipulate indices. It uses Elasticsearch client as a dependency, therefore you need to initiate the client before you create an IndexManager instance:

$client = \Elasticsearch\ClientBuilder::fromConfig([
  'hosts' => [
      'localhost:9200'
  ]
]);

$indexManager = new \ElasticAdapter\Indices\IndexManager($client);

The manager provides a list of useful methods, which are listed below.

Create

Create an index, either with the default settings and mapping:

$index = new \ElasticAdapter\Indices\Index('my_index');

$indexManager->create($index);

or configured according to your needs:

$mapping = (new \ElasticAdapter\Indices\Mapping())
    ->text('title', [
        'boost' => 2,
    ])
    ->keyword('tag', [
        'null_value' => 'NULL'
    ])
    ->geoPoint('location')
    ->dynamicTemplate('no_doc_values', [
        'match_mapping_type' => '*',
        'mapping' => [
            'type' => '{dynamic_type}',
            'doc_values' => false,
        ],
    ]);

$settings = (new \ElasticAdapter\Indices\Settings())
    ->index([
        'number_of_replicas' => 2,
        'refresh_interval' => -1
    ]);

$index = new \ElasticAdapter\Indices\Index('my_index', $mapping, $settings);

$indexManager->create($index);

Drop

Delete an index:

$indexManager->drop('my_index');

Put Mapping

Update an index mapping:

$mapping = (new \ElasticAdapter\Indices\Mapping())
    ->text('title', [
        'boost' => 2,
    ])
    ->keyword('tag', [
        'null_value' => 'NULL'
    ])
    ->geoPoint('location');

$indexManager->putMapping('my_index', $mapping);

Put Settings

Update an index settings:

$settings = (new \ElasticAdapter\Indices\Settings())
    ->analysis([
        'analyzer' => [
            'content' => [
                'type' => 'custom',
                'tokenizer' => 'whitespace'    
            ]
        ]
    ]);

$indexManager->putSettings('my_index', $settings);

Exists

Check if an index exists:

$indexManager->exists('my_index');

Open

Open an index:

$indexManager->open('my_index');

Close

Close an index:

$indexManager->close('my_index');

Document Management

Similarly to IndexManager, the DocumentManager class also depends on Elasticsearch client:

$client = \Elasticsearch\ClientBuilder::fromConfig([
  'hosts' => [
      'localhost:9200'
  ]
]);

$documentManager = new \ElasticAdapter\Documents\DocumentManager($client);

Index

Add a document to the index:

$documents = [
    new ElasticAdapter\Documents\Document('1', ['title' => 'foo']),
    new ElasticAdapter\Documents\Document('2', ['title' => 'bar']),
];

$documentManager->index('my_index', $documents);

There is also an option to refresh index immediately:

$documentManager->index('my_index', $documents, true);

Delete

Remove a document from the index:

$documents = [
    new ElasticAdapter\Documents\Document('1', ['title' => 'foo']),
    new ElasticAdapter\Documents\Document('2', ['title' => 'bar']),
];

$documentManager->delete('my_index', $documents);

If you want the index to be refreshed immediately pass true as the third argument:

$documentManager->delete('my_index', $documents, true);

You can also delete documents using query:

$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);

Search

Search documents in the index:

// create a search request
$request = new \ElasticAdapter\Search\SearchRequest([
    'match' => [
        'message' => 'test'
    ]
]);

// configure highlighting
$request->setHighlight([
    'fields' => [
        'message' => [
            'type' => 'plain',
            'fragment_size' => 15,
            'number_of_fragments' => 3,
            'fragmenter' => 'simple'
        ]
    ]
]);

// add suggestions
$request->setSuggest([
    'message_suggest' => [
        'text' => 'test',
        'term' => [
            'field' => 'message'
        ]
    ]
]);

// enable source filtering
$request->setSource(['message', 'post_date']);

// collapse fields
$request->setCollapse([
    'field' => 'user'
]);

// aggregate data
$request->setAggregations([
    'max_likes' => [
        'max' => [
            'field' => 'likes'
        ]
    ]
]);

// sort documents
$request->setSort([
    ['post_date' => ['order' => 'asc']],
    '_score'
]);

// add a post filter
$request->setPostFilter([
    'term' => [
        'cover' => 'hard'
    ]
]);

// track total hits
$request->setTrackTotalHits(true);

// use pagination
$request->setFrom(0)->setSize(20);

// execute the search request and get the response
$response = $documentManager->search('my_index', $request);

// get the total number of matching documents
$total = $response->getHitsTotal(); 

// get the corresponding hits
$hits = $response->getHits();

// every hit provides an access to the related index name, the score, the document and the highlight
// in addition, you can get a raw representation of the hit
foreach ($hits as $hit) {
    $indexName = $hit->getIndexName();
    $score = $hit->getScore();
    $document = $hit->getDocument();
    $highlight = $hit->getHighlight();
    $raw = $hit->getRaw();
}

// get the suggestions
$suggestions = $response->getSuggestions();

// get the aggregations
$aggregations = $response->getAggregations();