A service provider for laravel with a fluent elasticsearch query and aggregation dsl.
- Query (TermLevel, Fulltext, Geo, Compound, Joining, Specialized, InnerHit)
- Aggregation (Bucketing, Metric, Pipeline)
- Suggestion
composer require triadev/laravel-elasticsearch-dsl
The package is registered through the package discovery of laravel and Composer.
Once installed you can now publish your config file and set your correct configuration for using the package.
php artisan vendor:publish --provider="Triadev\Es\Dsl\Provider\ServiceProvider" --tag="config"
This will create a file config/laravel-elasticsearch-dsl.php
.
Key | Env | Value | Default |
---|---|---|---|
index | LARAVEL_ELASTICSEARCH_DSL_INDEX | STRING | default_index |
metrics.enabled | LARAVEL_ELASTICSEARCH_DSL_METRICS | BOOL | false |
metrics.buckets.search | --- | ARRAY | array of buckets in milliseconds |
metrics.buckets.suggest | --- | ARRAY | array of buckets in milliseconds |
Metrics are generated with the package: LaravelPrometheusExporter
Detailed configuration options are documented in the readme of the package.
The following metrics are generated as long as metrics.enabled = true
:
Namespace: triadev_laravel_elasticsearch_dsl
Name | Handler | Description |
---|---|---|
query_duration_milliseconds | search | execution time of search query |
query_duration_milliseconds | suggest | execution time of suggestion query |
This package offers a dsl for elasticsearch. The entry point for each query / aggregation is a facade.
Triadev\Es\Dsl\Facade\ElasticDsl;
Each query / aggregation returns an object containing the search result and aggregation.
Triadev\Es\Dsl\Model\SearchResult
int: time needed to execute the query
$result->took();
bool
$result->timedOut();
float
$result->maxScore();
int: number of matched documents
$result->totalHits();
Illuminate\Support\Collection: collection of searchable eloquent models
$result->hits();
array|null
$result->aggregation();
For every query that is based on bool, the bool status can be changed.
Default bool state: must
ElasticDsl::search()->termLevel()
->must()
->term('FIELD', 'VALUE')
->mustNot()
->term('FIELD', 'VALUE')
->should()
->term('FIELD', 'VALUE')
->filter()
->term('FIELD', 'VALUE')
})->get()
A nested query is realized with bool(\Closure $closure)
.
ElasticDsl::search()
->termLevel()
->term('FIELD', 'VALUE')
->bool(function (Search $search) {
$search->termLevel()
->term('FIELD', 'VALUE')
->bool(function (Search $search) {
$search->fulltext()
->match('FIELD1', 'QUERY1')
->matchPhrase('FIELD2', 'QUERY2');
});
})
->prefix('FIELD', 'VALUE')
->get();
--------------------------------------------------
[
"query" => [
"bool" => [
"must" => [
[
"term" => [
"FIELD" => "VALUE"
]
],
[
"bool" => [
"must" => [
[...],
[...]
]
]
],
[
"prefix" => [
"FIELD" => [
"value" => "VALUE"
]
]
]
]
]
]
]
matchAll, exists, fuzzy, ids, prefix, range, regexp, term, terms, type, wildcard
ElasticDsl::search()->termLevel()->filter()->term('FIELD', 'VALUE')->get();
match, matchPhrase, matchPhrasePrefix, multiMatch, queryString, simpleQueryString, commonTerms
ElasticDsl::search()->fulltext()->must()->match('FIELD', 'QUERY')->get();
geoBoundingBox, geoDistance, geoPolygon, geoShape
ElasticDsl::search()->geo()->filter()->geoDistance('FIELD','10km', new Location(1, 2))->get();
functionScore, constantScore, boosting, disMax
ElasticDsl::search()->compound()->functionScore(
function (Search $search) {
$search->termLevel()->term('FIELD1', 'VALUE1');
},
function (FunctionScore $functionScore) {
$functionScore->simple([]);
}
)->get();
nested, hasChild, hasParent
ElasticDsl::search()->joining()->nested('PATH', function (Search $search) {
$search->termLevel()->filter()->term('FIELD', 'VALUE');
})->get();
moreLikeThis
ElasticDsl::search()->specialized()->moreLikeThis('LIKE')->toDsl();
nestedInnerHit, parentInnerHit
ElasticDsl::search()->nestedInnerHit('NAME', 'PATH', function (Search $search) {
$search->termLevel()->term('FIELD', 'VALUE');
})->get();
To set an individual index or type per query you have two overwrite methods.
ElasticDsl::search()
->overwriteIndex('INDEX')
->overwriteType('TYPE')
->termLevel()
->matchAll()
->get();
Bucketing, Metric, Pipeline
ElasticDsl::search()->aggregation(function (Aggregation $aggregation) {
$aggregation->metric(function (Aggregation\Metric $metric) {
...
});
})->get();
ElasticDsl::search()->aggregation(function (Aggregation $aggregation) {
$aggregation->bucketing(function (Aggregation\Bucketing $metric) {
...
});
})->get();
ElasticDsl::search()->aggregation(function (Aggregation $aggregation) {
$aggregation->pipeline(function (Aggregation\Pipeline $metric) {
...
});
})->get();
term, phrase, completion
ElasticDsl::suggest()->term('NAME', 'TEXT', 'FIELD')->get();
If you do find an issue, please feel free to report it with GitHub's bug tracker for this project.
Alternatively, fork the project and make a pull request. :)
- docker-compose -f docker-compose.yml up
- composer test
Please see CONTRIBUTING for details.
The code for laravel-elasticsearch-dsl is distributed under the terms of the MIT license (see LICENSE).