Given that there is little to no overlap with how Elasticsearch handles index management compared to SQL schema operations, the schema functionality for Elasticsearch has been built from the ground up. This ensures that you have a tailored and effective toolset for working with Elasticsearch indices, designed to cater to their unique requirements and capabilities.
Migration Class Creation
To use migrations for index management, you can create a normal migration class in Laravel as normal. However, it’s crucial to note that the up() and down() methods encapsulate the specific Elasticsearch-related operations, namely:
Schema Management: Utilizes the PDPhilip\Elasticsearch\Schema\Schema class.
Index Definition: Leverages the PDPhilip\Elasticsearch\Schema\IndexBlueprint class for defining index structures.
Analyzer Configuration: Employs the PDPhilip\Elasticsearch\Schema\AnalyzerBlueprint class for specifying custom analyzers.
Full example
Example:
Index Creation
Schema::create
Creates a new index with the specified structure and settings.
Schema::createIfNotExists
Creates a new index only if it does not already exist.
Index Deletion
Schema::delete
Deletes the specified index. Will throw an exception if the index does not exist.
Schema::deleteIfExists
Deletes the specified index if it exists.
Index Modification
Schema::modify
Modifies an existing index. This could involve adding new fields, changing analyzers, or updating other index settings.
Analyzer Configuration
Schema::setAnalyser
Configures or updates analyzers for a specific index. This is crucial for defining how text fields are tokenized and analyzed.
Index Lookup and Information Retrieval
Schema::getIndex
Retrieves detailed information about a specific index or indices matching a pattern.
Schema::getIndices
Equivalent to Schema::getIndex('*'), retrieves information about all indices on the Elasticsearch cluster.
Schema::getMappings
Retrieves the mappings for a specified index.
Schema::getSettings
Retrieves the settings for a specified index.
Schema::hasField
Checks if a specific field exists in the index’s mappings.
Schema::hasFields
Checks if multiple fields exist in the index’s mappings.
Schema::hasIndex
Checks if a specific index exists.
Prefix Management
Schema::overridePrefix
Temporarily overrides the default index prefix for subsequent operations. This can be useful in multi-tenant applications or when accessing indices across different environments.
Direct DSL Access
Schema::dsl
Provides direct access to the Elasticsearch DSL, allowing for custom queries and operations not covered by other methods.