Overview
Elasticsearch provides:- Full-text search with custom analyzers
- Synonym matching for anime titles
- Fast autocomplete and search-as-you-type
- Advanced filtering and aggregations
Prerequisites
- Elasticsearch 7.x or 8.x
- PHP 8.1+
- Composer dependencies installed
Installation
Option 1: Docker
Option 2: Docker Compose
Option 3: System Package
Ubuntu/Debian:Configuration
Environment Variables
Configure Elasticsearch connection in.env:
Configuration Files
config/elastic.client.php:
config/elastic.migrations.php:
config/elastic.scout_driver.php:
Environment Variables Reference
| Variable | Default | Description |
|---|---|---|
ELASTIC_CONNECTION | default | Connection name to use |
ELASTIC_HOST | http://localhost:9200 | Elasticsearch server URL |
SCOUT_DRIVER | null | Set to elastic to enable |
SCOUT_PREFIX | - | Index prefix (useful for multi-tenancy) |
SCOUT_QUEUE | false | Queue search index updates |
ELASTIC_SCOUT_DRIVER_REFRESH_DOCUMENTS | false | Immediately refresh indices after write operations |
ELASTIC_MIGRATIONS_TABLE | elastic_migrations | Database table to track migrations |
ELASTIC_MIGRATIONS_CONNECTION | - | Database connection for migration tracking |
Index Setup
AnimeThemes Server includes migrations for all searchable models.Available Indices
Located inelastic/migrations/:
2020_12_21_225415_create_anime_index.php- Anime search2020_12_22_033019_create_artist_index.php- Artist search2020_12_22_034505_create_entry_index.php- Entry search2020_12_22_034544_create_series_index.php- Series search2020_12_22_034549_create_song_index.php- Song search2020_12_22_034553_create_anime_synonym_index.php- Anime synonym search2020_12_22_034557_create_theme_index.php- Theme search
Run Migrations
Create all Elasticsearch indices:- Create indices with proper mappings and analyzers
- Track migration status in the database
- Set up index aliases for zero-downtime reindexing
Migration Status
Check which migrations have run:Rollback Migrations
Rollback the last batch:Refresh Migrations
Drop all indices and recreate them:Index Data
After creating indices, populate them with data:Index All Models
Index Specific Model
Flush Index
Remove all documents from an index:Index Structure
Anime Index
Example migration fromelastic/migrations/2020_12_21_225415_create_anime_index.php:
- Name field with custom analyzer for better matching
- Synonyms as nested documents for alternative titles
- Keyword fields for exact matching
- Metadata like season, year, media format
Text Analyzers
Custom analyzers improve search quality. FromApp\Concerns\Elastic\ConfiguresTextAnalyzers:
The analyzers handle:
- Case normalization
- ASCII folding (é → e)
- Stemming
- Synonym expansion
Search Operations
Basic Search
Advanced Search
Synonym Search
Anime synonyms are indexed as nested documents:Maintenance
Reindex Data
When changing index mappings or analyzers:- Create a new migration with updated mapping
- Run the migration:
- Reindex the data:
Monitor Index Health
Optimize Indices
Performance Tuning
Queue Index Updates
For better performance, queue search index updates:Bulk Indexing
When importing large datasets, use chunking:Index Prefixing
Use prefixes for multiple environments sharing one Elasticsearch cluster:production_anime, production_artist, etc.
Troubleshooting
Connection refused
Index not found
Search returns no results
-
Verify data is indexed:
-
Check index mapping:
-
Test query directly:
-
Flush and reimport:
Out of memory errors
Increase Elasticsearch heap size:Slow queries
- Enable slow query logging in Elasticsearch
- Use index prefixes to isolate environments
- Optimize index settings and mappings
- Consider index lifecycle management for large datasets
Next Steps
REST API
Explore the REST API
Environment Variables
Configure environment variables
Feature Flags
Manage feature availability
GraphQL API
Learn about GraphQL queries