Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Elasticsearch

Laravel Elasticsearch Laravel Package

designmynight/laravel-elasticsearch

Laravel package that lets you query Elasticsearch with Eloquent-style builders and get model instances back. Supports query/filter/postFilter, geo search, complex aggregations, and the scroll API for large result sets.

View on GitHub
Deep Wiki
Context7

Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perform aggregations.

Frequently asked questions about Laravel Elasticsearch
Can I use this package to replace my primary database (e.g., MySQL) with Elasticsearch in Laravel?
No, this package is designed for read-heavy search/analytics workloads, not transactional data. You’ll need to sync writes between your relational database (e.g., PostgreSQL) and Elasticsearch manually, likely via Laravel Events or Queues. It’s not a drop-in replacement for a primary database.
How do I set up Elasticsearch as a database connection in Laravel?
Add the `elasticsearch` connection to your `config/database.php` with host, port, and index details. Then override `newEloquentBuilder()` and `newBaseQueryBuilder()` in your base `Model` class to route Elasticsearch queries. The package provides the necessary builder classes (`ElasticsearchEloquentBuilder` and `ElasticsearchQueryBuilder`).
Which Laravel and Elasticsearch versions does this package support?
The package supports Elasticsearch 7.x (package version 6), 6.x (version 5), and 5.x (version 4). Check the [README](https://github.com/designmynight/laravel-elasticsearch) for exact version mappings. Ensure your Laravel version is compatible with the Elasticsearch client used by the package.
How do I perform geo-searches (e.g., find locations within 5km) with this package?
Use the built-in geo methods like `within()` for geo-bounds or `distance()` for geo-distance queries. For example, `$model->where('location', 'distance', ['lat' => 40.7128, 'lon' => -74.0060])->within(5, 'km')`. The package abstracts Elasticsearch’s geo-query DSL into Eloquent-style syntax.
Can I use Eloquent relationships (e.g., hasMany) with Elasticsearch models?
No, this package does not automatically map Eloquent relationships to Elasticsearch parent-child joins. Relationships must be handled manually, often by denormalizing data into Elasticsearch or using custom queries. Consider flattening related data into a single Elasticsearch document for complex searches.
How do I handle large result sets (e.g., exporting 1M+ records) without memory issues?
Use the scroll API via the `scroll()` method, which returns a PHP generator. Process results in batches to avoid memory overload. Example: `foreach ($model->scroll() as $item) { ... }`. The package optimizes this for Laravel’s queue system if needed.
What’s the best way to keep Elasticsearch in sync with my SQL database?
Implement a sync layer using Laravel Events (e.g., `saved`, `deleted`) and Queues to update Elasticsearch asynchronously. For example, trigger a `ForgetCustomer` job on `deleted` events. Avoid real-time sync for write-heavy apps to prevent performance bottlenecks.
How do I create or update Elasticsearch mappings for my models?
Use the CLI tools provided: `php artisan make:mapping` to generate mappings or `php artisan migrate:mappings` to apply them. Mappings must be defined manually or generated from your SQL schema, as Elasticsearch is schema-less. Avoid Laravel migrations for this purpose.
Are aggregations (e.g., nested terms, metrics) supported, and how do I use them?
Yes, the package provides a fluent API for complex aggregations. Use methods like `terms()`, `nested()`, or `metrics()` on your query builder. Example: `$model->selectRaw('*')->terms('category')->metrics(['avg' => 'price'])` to group by category and calculate averages.
What alternatives exist for Elasticsearch in Laravel if this package doesn’t fit my needs?
Consider raw Elasticsearch PHP clients (e.g., `elasticsearch/elasticsearch`), Scout (for simple search), or packages like `spatie/laravel-searchable` for basic full-text search. For advanced use cases, evaluate `ruflin/elastica` or build a custom sync layer with Laravel Events and Queues.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony