triadev / laravel-elasticsearch-dsl
A service provider for laravel with a fluent elasticsearch query and aggregation dsl.
Requires
- php: >=7.1
- ext-json: *
- elasticsearch/elasticsearch: ^6.0
- laravel/framework: 5.5.* || 5.6.* || 5.7.*
- ongr/elasticsearch-dsl: ^6.0
- triadev/laravel-elasticsearch-provider: ^3.0
- triadev/laravel-prometheus-exporter: ^1.6
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: ~1.0
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^6.0 || ^7.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-10-09 14:27:56 UTC
README
A service provider for laravel with a fluent elasticsearch query and aggregation dsl.
Supported laravel versions
Supported elasticsearch versions
Main features
- Query (TermLevel, Fulltext, Geo, Compound, Joining, Specialized, InnerHit)
- Aggregation (Bucketing, Metric, Pipeline)
- Suggestion
Installation
Composer
composer require triadev/laravel-elasticsearch-dsl
Application
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
.
Configuration
Metrics
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
Histogram
Usage
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();
Bool
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()
Nested bool query
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" ] ] ] ] ] ] ]
TermLevel
matchAll, exists, fuzzy, ids, prefix, range, regexp, term, terms, type, wildcard
ElasticDsl::search()->termLevel()->filter()->term('FIELD', 'VALUE')->get();
Fulltext
match, matchPhrase, matchPhrasePrefix, multiMatch, queryString, simpleQueryString, commonTerms
ElasticDsl::search()->fulltext()->must()->match('FIELD', 'QUERY')->get();
Geo
geoBoundingBox, geoDistance, geoPolygon, geoShape
ElasticDsl::search()->geo()->filter()->geoDistance('FIELD','10km', new Location(1, 2))->get();
Compound
functionScore, constantScore, boosting, disMax
ElasticDsl::search()->compound()->functionScore( function (Search $search) { $search->termLevel()->term('FIELD1', 'VALUE1'); }, function (FunctionScore $functionScore) { $functionScore->simple([]); } )->get();
Joining
nested, hasChild, hasParent
ElasticDsl::search()->joining()->nested('PATH', function (Search $search) { $search->termLevel()->filter()->term('FIELD', 'VALUE'); })->get();
Specialized
moreLikeThis
ElasticDsl::search()->specialized()->moreLikeThis('LIKE')->toDsl();
InnerHit
nestedInnerHit, parentInnerHit
ElasticDsl::search()->nestedInnerHit('NAME', 'PATH', function (Search $search) { $search->termLevel()->term('FIELD', 'VALUE'); })->get();
Individual index and type
To set an individual index or type per query you have two overwrite methods.
ElasticDsl::search() ->overwriteIndex('INDEX') ->overwriteType('TYPE') ->termLevel() ->matchAll() ->get();
Aggregation
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();
Suggestions
term, phrase, completion
ElasticDsl::suggest()->term('NAME', 'TEXT', 'FIELD')->get();
Reporting Issues
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. :)
Testing
- docker-compose -f docker-compose.yml up
- composer test
Contributing
Please see CONTRIBUTING for details.
Credits
Other
Project related links
License
The code for laravel-elasticsearch-dsl is distributed under the terms of the MIT license (see LICENSE).