Lumen Elasticsearch
Simple wrapper of Elasticsearch-PHP for the Lumen PHP framework.
NOTE: Branch 5.2 is using Lumen framework 5.2. Only bug fixes for 0.7.X should be tagged in the 5.2 branch.
Requirements
- PHP 5.6 or newer
- Composer
Usage
Installation
Run the following command to install the package through Composer:
composer require nordsoftware/lumen-elasticsearch
Bootstrapping
Add the following line to bootstrap/app.php
:
$app->register(Nord\Lumen\Elasticsearch\ElasticsearchServiceProvider::class);
You can now get the service instance using app(ElasticsearchServiceContract::class)
or inject the ElasticsearchServiceContract
where needed.
Configure
Copy the configuration template in config/elasticsearch.php
to your application's config
directory and modify.
For more information see the Configuration Files
section in the Lumen documentation.
Quickstart
Using the query builder:
$service = app(ElasticsearchServiceContract::class);
$queryBuilder = $service->createQueryBuilder();
$query = $queryBuilder->createBoolQuery()
->addMust(
$queryBuilder->createTermQuery()
->setField('user')
->setValue('kimchy'))
->addFilter(
$queryBuilder->createTermQuery()
->setField('tag')
->setValue('tech'))
->addMustNot(
$queryBuilder->createRangeQuery()
->setField('age')
->setGreaterThanOrEquals(18)
->setLessThanOrEquals(40))
->addShould(
$queryBuilder->createTermQuery()
->setField('tag')
->setValue('wow'))
->addShould(
$queryBuilder->createTermQuery()
->setField('tag')
->setValue('elasticsearch'));
$search = $service->createSearch()
->setIndex('index')
->setType('document')
->setQuery($query)
->setSize(50)
->setPage(1);
$result = $service->execute($search);
Raw arrays:
$service = app(ElasticsearchServiceContract::class);
$result = $service->search([
'index' => 'index',
'type' => 'document',
'body' => [
'query' => [
'bool' => [
'must' => [
'term' => ['user' => 'kimchy']
],
'filter' => [
'term' => ['tag' => 'tech']
],
'must_not' => [
'range' => [
'age' => ['gte' => 10, 'lte' => 20]
]
],
'should' => [
[
'term' => ['tag' => 'wow']
],
[
'term' => ['tag' => 'elasticsearch']
]
],
]
],
'size' => 50,
'from' => 0
],
]);
Contributing
Please read the guidelines.
Running tests
Clone the project and install its dependencies by running:
composer install
Run the following command to run the test suite:
vendor/bin/codecept run unit
License
See LICENSE.