Focus of this library is to make it easier to manage elastic indices (wit mappings / settings), create reusable query building (manual or from request).
- Adds ability to manage elasticsearch indices with wrapper class which will help to you create/update index write documents (with bulk mode) etc.
- Adds ability to create a query builder with query filters for each index.
- Adds ability to build a query builder from request data (reusable component).
1. Add custom repository to composer.json
"repositories": {
{
"type": "git",
"url": "https://github.com/pionl/elasticsearch-query-builder.git"
}
}
2. Install via composer
composer require pion/laravel-lelastico
3. Add the service provider (Laravel 5.4 and below - supports Auto discovery)
\Lelastico\LelasticoServiceProvider::class,
- erichard/elasticsearch-query-builder - at this moment forked version with additional functions.
- elasticsearch/elasticsearch, version 7 and above (tested with 7.5).
Set elastic hosts
For development, you can use default value in the config without password: localhost:9200
Use ELASTICSEARCH_HOSTS
environment for setting elastic search hosts. Format.
Resolve elastic search client
$client = resolve(\Elasticsearch\Client::class);
Mapping types constants
Property mappings types using constants like:
- MappingTypes::KEYWORD
- MappingTypes::TEXT
- MappingTypes::TEXT_WITH_KEYWORD
- MappingTypes::SHORT
- MappingTypes::SHORT_WITH_KEYWORD
- MappingTypes::LONG
- MappingTypes::LONG_WITH_KEYWORD
- MappingTypes::INTEGER
- MappingTypes::INTEGER_WITH_KEYWORD
- MappingTypes::DATE
- MappingTypes::BOOLEAN
- MappingTypes::FLOAT
- MappingTypes::textWithAnalyzer(string $analyzer, string $searchAnalyzer), builds
Adding indices
-
Create your indices by extending
AbstractElasticIndex
and implementingcreateIndexName
for elastic index- Implement
propertyMappings
for custom mappings.
protected function propertyMappings(): array { return [ 'id' => MappingTypes::KEYWORD, 'name' => MappingTypes::TEXT_WITH_KEYWORD, 'is_verified' => MappingTypes::BOOLEAN, 'email' => MappingTypes::textWithAnalyzer('fulltext'), 'created_at' => MappingTypes::DATE, 'updated_at' => MappingTypes::DATE, 'deleted_at' => MappingTypes::DATE, ]; }
- Implement
settings
for custom index settings
protected function settings(): array { // Add support for partial text search return [ 'index' => [ 'analysis' => [ 'filter' => [ 'fulltext_filter' => [ // Always from start of beginning of each token 'type' => 'edge_ngram', 'min_gram' => 3, 'max_gram' => 20, ], ], 'analyzer' => [ 'fulltext' => [ 'type' => 'custom', 'tokenizer' => 'standard', 'filter' => ['lowercase', 'fulltext_filter'], ], ], ], ], ]; }
- Implement
-
Create or update
lelastico.php
config with indices classes.return [ 'indices' => [ \App\ElasticSearch\Indices\UsersIndex::class, ], ];
-
Update or create indices in elastic (stores settings / mapping) using
php artisan elastic:indices
Updates the elastic indices --only="only", handle only given index --f, will delete the index and data. Will new index with mappings --d, will delete the index and data --skip-settings-update, when upadting, the index is closed / opened due the settings update. You can skip it by provided this option.
- improve documentation
- add
make
console.
Can be found in releases.
See CONTRIBUTING.md for how to contribute changes. All contributions are welcome.
This library was created and improved thanks to clients projects.
laravel-elastico was written by Martin Kluska and is released under the MIT License.
Copyright (c) 2020 Martin Kluska