- How do I integrate Elasticsearch into my Laravel app without breaking Eloquent?
- The package extends Eloquent natively, so you can use familiar methods like `where()`, `searchTerm()`, or `geoDistance()` directly on models. Just configure your `.env` with Elasticsearch credentials, register the service provider, and run `php artisan elastic:make` to scaffold models. No need to rewrite queries—just add search-specific methods.
- Does this package support Laravel 11+ and Elasticsearch 8.x?
- Yes, it’s fully compatible with Laravel 11+ and Elasticsearch 8.x. Older versions (e.g., Laravel 10, ES 7.x) are supported via separate branches. The package auto-adapts to Laravel’s query builder changes (e.g., Laravel 13’s `Connection::select()`) to avoid breaking updates.
- Can I mix SQL and Elasticsearch queries in the same app?
- Absolutely. The package supports hybrid queries, including relationships like `UserLog::with('user')`. However, ensure data consistency by syncing writes (e.g., using Laravel events or queues) since Elasticsearch operates asynchronously by default.
- How do I handle schema migrations for Elasticsearch indices?
- Use the `elastic:re-index` Artisan command to automate schema migrations. Define mappings in Laravel migrations with the Blueprint DSL, then run the command to sync changes. For large datasets, test migrations in staging first—downtime may be required depending on your cluster setup.
- What if my search queries return too many results? How do I optimize performance?
- Set `ES_OPT_DEFAULT_LIMIT` in your `.env` to cap results (default: 1000). For large datasets, use pagination (`paginate()`) or Elasticsearch’s scroll API. Avoid broad queries without filters; leverage `whereMatch()` or `geoDistance()` to narrow results early in the query pipeline.
- Does this package support geospatial queries like finding users within 10km of a location?
- Yes, use `whereGeoDistance('location', '10km', [lat, lng])` on models with geo_point fields. Ensure your Elasticsearch mapping includes `geo_point` types for the relevant fields. For complex geospatial logic, consider pre-filtering with bounding boxes to improve performance.
- How do I debug Elasticsearch queries or mappings?
- Use `Schema::compileMapping()` to inspect generated mappings before applying them. For query debugging, enable Elasticsearch’s `pretty` response format in `.env` (e.g., `ES_OPT_PRETTY=true`) or log raw queries with `->toDebugString()`. The package also integrates with Laravel’s query logging.
- What are the alternatives if I need more advanced Elasticsearch features (e.g., nested fields, aggregations)?
- For nested fields or complex aggregations, you may need to use Elasticsearch’s raw DSL via the package’s `Elasticsearch::raw()` method. Alternatively, consider packages like `ruflin/elastica` for low-level control, though they lack Eloquent integration. This package balances convenience and power for 80% of use cases.
- How do I handle fuzzy matching or typo-tolerant searches?
- Use `whereFuzzy()` for typo-tolerant searches (e.g., `whereFuzzy('name', 'john')`). Configure analyzers in your mapping (e.g., `standard` or `english` analyzers) to control tokenization. For custom fuzzy logic, pass parameters like `fuzziness: 2` to the method.
- Is this package suitable for production with high traffic?
- Yes, but monitor Elasticsearch cluster health and query performance. Use managed services (e.g., Elastic Cloud) for scalability, and implement caching (e.g., Redis) for frequent queries. Test under load with tools like `k6` or `Artillery` to identify bottlenecks before deployment.