designmynight / laravel-elasticsearch
Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perform aggregations.
Installs: 28 237
Dependents: 0
Suggesters: 0
Security: 0
Stars: 30
Watchers: 6
Forks: 14
Open Issues: 4
Requires
- php: ^8.0
- ext-json: *
- elasticsearch/elasticsearch: ^7.17
- laravel/framework: ^9.0
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ^7.2.0
- phpunit/phpunit: ^9.5.19
- dev-master
- 7.0.x-dev
- v7.0.1
- v7.0
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.4
- v6.2.3
- 6.2.2
- 6.2.1
- v6.2.0
- 6.1.4
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- v6.0.2
- v6.0.1
- v6.0.0
- 5.0.x-dev
- v5.0.0
- v4.5
- 4.4.x-dev
- v4.3.11
- 4.3.10.x-dev
- v4.3.10
- 4.3.9.x-dev
- v4.3.9
- 4.3.8.x-dev
- v4.3.8
- v4.3.7
- v4.3.6
- v4.3.5
- 4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- 4.3.0.x-dev
- v4.3.0
- 4.2.4.x-dev
- v4.2.4
- v4.2.3
- v4.2.2.x-dev
- v4.2.2
- v4.2.1
- 4.2.0.x-dev
- v4.2.0
- 4.1.0.x-dev
- v4.1.0
- 4.0.x-dev
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- 3.0.x-dev
- v3.0.15
- v3.0.14
- v3.0.13
- v3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.1
- 1.1.0
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.2.1-beta
- v0.2.0-beta
- v0.1.0-beta
- v0.1.0-alpha.3
- v0.1.0-alpha.2
- v0.1.0-alpha1
- v0.1.0-alpha
- dev-shifting_tests
- dev-fix_apply_grammar
- dev-extended_methods_no_types
- dev-patching_docs
- dev-laravel_9_support
- dev-4.3.0-change-array-check
- dev-4.2-merge
- dev-dependabot/composer/league/flysystem-1.1.4
- dev-alasdairmackenzie-patch-1
- dev-fixClauseOptions
- dev-Bump-laravel-version-reqs
- dev-v4.1.0-branch
- dev-2.1-fixes
This package is auto-updated.
Last update: 2024-12-05 19:11:22 UTC
README
Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perform aggregations.
Build Elasticsearch queries as you're used to with Eloquent, and get Model instances in return, with some nice extras:
- Use
query
,filter
andpostFilter
query types - Perform geo searches
- Build and perform complex aggregations on your data
- Use the Elasticsearch scroll API to retrieve large numbers of results
Versions
Depending on your version of Elasticsearch you can use the following version of this package
Setup
Add elasticsearch connection configuration to database.php
'elasticsearch' => [
'driver' => 'elasticsearch',
'host' => 'localhost',
'port' => 9200,
'database' => 'your_es_index',
'username' => 'optional_es_username',
'password' => 'optional_es_username',
'suffix' => 'optional_es_index_suffix',
]
Create or update your base Model.php class to override newEloquentBuilder()
and newBaseQueryBuilder()
:
/** * Create a new Eloquent builder for the model. * * @param \Illuminate\Database\Query\Builder $query * @return \Illuminate\Database\Eloquent\Builder|static */ public function newEloquentBuilder($query) { switch ($this->getConnectionName()) { case static::getElasticsearchConnectionName(): $builder = new ElasticsearchEloquentBuilder($query); break; default: $builder = new Illuminate\Database\Eloquent\Builder($query); } return $builder; } /** * Get a new query builder instance for the connection. * * @return \Illuminate\Database\Query\Builder */ protected function newBaseQueryBuilder() { $connection = $this->getConnection(); switch ($this->getConnectionName()) { case static::getElasticsearchConnectionName(): $builder = new ElasticsearchQueryBuilder($connection, $connection->getQueryGrammar(), $connection->getPostProcessor()); break; default: $builder = new Illuminate\Database\Query\Builder($connection, $connection->getPostProcessor()); } return $builder; }
Search
You're now ready to carry out searches on your data. The query will look for an Elasticsearch index with the same name as the database table that your models reside in.
$documents = MyModel::newElasticsearchQuery() ->where('date', '>', Carbon\Carbon::now()) ->get();
Aggregations
Aggregations can be added to a query with an approach that's similar to querying Elasticsearch directly, using nested functions rather than nested arrays. The aggregation()
method takes three or four arguments:
- A key to be used for the aggregation
- The type of aggregation, such as 'filter' or 'terms'
- (Optional) A callback or array providing options for the aggregation
- (Optional) A function allowing you to provide further sub-aggregations
$myQuery = MyModel::newElasticsearchQuery() ->aggregation( // The key of the aggregation (used in the Elasticsearch response) 'my_filter_aggregation', // The type of the aggregation 'filter', // A callback providing options to the aggregation, in this case adding filter criteria to a query builder function ($query) { $query->where('lost', '!=', true); $query->where('concierge', true); }, // A callback specifying a sub-aggregation function ($builder) { // A simpler aggregation, counting terms in the 'status' field $builder->aggregation('my_terms_aggregation', 'terms', ['field' => 'status']); } ); $results = $myQuery->get(); $aggregations = $myQuery->getQuery()->getAggregationResults();
Geo queries
You can filter search results by distance from a geo point or include only those results that fall within given bounds, passing arguments in the format you'd use if querying Elasticsearch directly.
$withinDistance = MyModel::newElasticsearchQuery() ->whereGeoDistance('geo_field', [$lat, $lon], $distance); $withinBounds= MyModel::newElasticsearchQuery() ->whereGeoBoundsIn('geo_field', $boundingBox);
Scroll API
You can use a scroll search to retrieve large numbers of results. Rather than returning a Collection, you'll get a PHP Generator function that you can iterate over, where each value is a Model for a single result from Elasticsearch.
$documents = MyModel::newElasticsearchQuery() ->limit(100000) ->usingScroll() ->get(); // $documents is a Generator foreach ($documents as $document){ echo $document->id; }
Console
This package ships with the following commands to be used as utilities or as part of your deployment process.
Mappings and Aliases
When creating a new index during a migrate:mappings
the command will automatically create an alias based on the migration name by removing the date string. For example the migration 2018_08_03_095804_users.json
will create the alias users
.
During the first migration an index appears in the migrate:mappings
command will also switch the alias to the latest index mapping. The above will only happen when the alias does not already exist.
Future migrations will require you to use the --swap
option.