AWS Elasticsearch Service for Laravel/Lumen
NOTE: This package only caters search, aggregation, and indexing transactions. Other than that, you can refer to elasticsearch's official documentation.
composer require elegisandi/aws-elasticsearch-laravel
-
Add service provider to your
config/app.php
providerselegisandi\AWSElasticsearchService\ElasticSearchServiceProvider::class
-
Add facade to your
config/app.php
aliases'ElasticSearch' => elegisandi\AWSElasticsearchService\Facades\ElasticSearch::class
-
Set AWS credentials and Elasticsearch config in your
.env
fileAWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION ELASTICSEARCH_ENDPOINT ELASTICSEARCH_PORT ELASTICSEARCH_SHARDS ELASTICSEARCH_REPLICAS ELASTICSEARCH_DEFAULT_INDEX ELASTICSEARCH_DEFAULT_TYPE ELASTICSEARCH_DEFAULT_TIME_FILTER_FIELD
When you are already using aws elasticsearch service, set
AWS_ELASTICSEARCH_SERVICE=true
If you want to configure elasticsearch mappings, settings and/or default type and index, just run:
php artisan vendor:publish --provider=elegisandi\\AWSElasticsearchService\\ElasticSearchServiceProvider
For Lumen:
-
Register service provider to your
bootstrap/app.php
$app->register(elegisandi\AWSElasticsearchService\ElasticSearchServiceProvider::class);
Using Facade:
<?php
namespace App;
use ElasticSearch;
public function index() {
extract(ElasticSearch::setSearchParams(request()));
$clicks = [];
$total = 0;
if ($hits = ElasticSearch::search($query, $options, $date_range)) {
$clicks = $hits['hits']['hits'];
$total = $hits['hits']['total'];
}
}
For Lumen:
<?php
namespace App;
public function index() {
extract(app('elasticsearch')->setSearchParams(request()));
$clicks = [];
$total = 0;
if ($hits = app('elasticsearch')->search($query, $options, $date_range)) {
$clicks = $hits['hits']['hits'];
$total = $hits['hits']['total'];
}
}
-
Create Index (creates the default index)
php artisan elasticsearch:create-index
To reset existing index,
php artisan elasticsearch:create-index --reset
-
Update Index Mapping (updates the default index mapping)
php artisan elasticsearch:update-index-mapping
Only supports new properties updates.
-
$aggs : must follow the structure specified in elasticsearch docs.
$query : see
search
method$query
argument$options : see
search
method$options
argumentreturns
Array
-
$query : an array of key-value pair of any available properties
$options : an array of key-value pair of the ff:
from
,size
,sort
$range : an array representation of range query.
returns
Array
-
a (syntactic sugar) method of search with zero hits result
returns
Int
-
an optional and conventional approach of setting search params via query string
$request : an instance of
\Illuminate\Http\Request
, query variables in used:range
, see getDateRange methodstart
, a valid date stringend
, a valid date stringsort
, a mapping propertyorder
, value is eitherdesc
orasc
size
, total results to return (max of 10000)
$defaults : an array of key-value pair of the ff:
sort, order, size
returns
Array
-
$range : predefined date range values:
today, yesterday, last-7-days, this-month, last-month, last-2-months, last-3-months
$format must be a valid date format, default is
null
which will return a DateTime instancereturns
Array
-
$format must be a valid date format, default is
null
which will return a DateTime instancereturns
Array
-
returns
Array
-
returns
String
-
returns
String
-
returns
String
-
returns
Array
-
setBoolQueryClause(
Collection $query
,array $properties
,$context
,$occur
,callable $callback = null
)returns
Array
-
returns
Array
-
returns
Collection
-
returns
Array
-
returns
Array
-
returns
Array
-
returns
Array
-
returns
Array
-
returns
Array
-
returns
Array
-
returns
Array
-
returns
Boolean
-
returns
Array
-
Supported data types in search method are:
- keyword
- text
- array
- integer
- boolean
- ip
Open an issue first to discuss potential changes/additions.