Index Blueprint
This content is for v3.9. Switch to the latest version for up-to-date documentation.
Delve into the intricacies of managing Elasticsearch schemas in Laravel. This guide covers everything from defining field types with specific parameters to adjusting index settings, providing developers with the tools needed for precise schema configuration.
Index Blueprint
The Index Blueprint applies to mapping fields in the index. It provides a range of methods to define the structure and behavior of your Elasticsearch index.
public function up(){ Schema::create('contacts', function (IndexBlueprint $index) { $index->text('first_name')->copyTo('full_name'); $index->text('last_name')->copyTo('full_name'); $index->text('full_name');
//Multiple types => Order matters :: //Top level `email` will be a searchable text field //Sub Property will be a keyword type which can be sorted using orderBy('email.keyword') $index->text('email'); $index->keyword('email');
//Dates have an optional formatting as second parameter $index->date('first_contact', 'epoch_second');
//Objects are defined with dot notation: $index->text('products.name'); $index->float('products.price')->coerce(false);
//Disk space considerations :: //Not indexed and not searchable: $index->keyword('internal_notes')->docValues(false); //Remove scoring for search: $index->array('tags')->norms(false); //Remove from index, can't search by this field but can still use for aggregations: $index->integer('score')->index(false);
//If null is passed as value, then it will be saved as 'NA' which is searchable $index->keyword('favorite_color')->nullValue('NA');
//Numeric Types $index->integer('some_int'); $index->float('some_float'); $index->double('some_double'); $index->long('some_long'); $index->short('some_short'); $index->byte('some_byte'); $index->halfFloat('some_half_float'); $index->scaledFloat('some_scaled_float',140); $index->unsignedLong('some_unsigned_long');
//Alias Example $index->text('notes'); $index->alias('comments', 'notes');
$index->geo('last_login'); $index->date('created_at'); $index->date('updated_at');
//Settings $index->settings('number_of_shards', 3); $index->settings('number_of_replicas', 2);
//Other Mappings $index->map('dynamic', false); $index->map('date_detection', false);
//Custom Mapping $index->mapProperty('purchase_history', 'flattened'); }}
Analyzer Blueprint
The Analyzer Blueprint is used to define custom analyzers for your index. It allows you to specify the tokenizer and filters that will be applied to the text fields during indexing and searching.
public function up(){ Schema::setAnalyser('contacts', function (AnalyzerBlueprint $settings) { $settings->analyzer('my_custom_analyzer') ->type('custom') ->tokenizer('punctuation') ->filter(['lowercase', 'english_stop']) ->charFilter(['emoticons']); $settings->tokenizer('punctuation') ->type('pattern') ->pattern('[ .,!?]'); $settings->charFilter('emoticons') ->type('mapping') ->mappings([":) => _happy_", ":( => _sad_"]); $settings->filter('english_stop') ->type('stop') ->stopwords('_english_'); });}