Skip to content

Generating Bridge Classes

To get started, create a bridge with the artisan make:bridge command. Bridges usually live in the App\Bridges directory. however, this can be changed through elasticbridge.namespace config.

bash
php artisan make:bridge HotelRoom

Naming Conventions

Bridges generated by the make:bridge command will be placed in the app/Bridges directory. Here's a basic bridge class and some key conventions:

php
<?php

namespace App\Bridges;

use Lacasera\ElasticBridge\ElasticBridge;

class HotelRoom extends ElasticBridge 
{
    
}

Index Names

The example above is a bridge class for HotelRoom bridge. By convention, just like an eloquent model, the snake case, plural name of the class will be used as the index name unless another name is explicitly specified.

If your bridge's corresponding search index does not fit this convention, you may manually specify the index name by defining an index property on the model:

php
<?php

namespace App\Bridges;

use Lacasera\ElasticBridge\ElasticBridge;

class HotelRoom extends ElasticBridge 
{
    protected $index = 'hotel-rooms';
}

Multiple Indexes

Time-series data is often spread across many indexes (logs-2025.02.01, logs-2025.02.02, …). You usually want to search across all of them but write to one concrete index. ElasticBridge resolves the read and write index separately.

$index is the index (or wildcard/comma pattern) to read from:

php
class Log extends ElasticBridge
{
    protected $index = 'logs-*'; // searches every logs-* index
}

Writing to a wildcard is rejected (it is ambiguous). Set the concrete write target with $writeIndex, or override getWriteIndex() for a dynamic (e.g. date-based) target:

php
class Log extends ElasticBridge
{
    protected $index = 'logs-*';

    public function getWriteIndex(): string
    {
        return 'logs-'.date('Y.m.d');
    }
}

Documents read from a wildcard write back to the concrete index they came from, so updating a hit found via logs-* targets its exact index automatically.

Per-call overrides

Override the read or write index for a single query with from() and into():

php
// search specific indexes for this query
Log::from('logs-2025.02.01,logs-2025.02.02')
    ->asBoolean()
    ->matchAll()
    ->get();
    
Log::from(['logs-2025.02.01', 'logs-2025.02.02'])->get();

// write to a specific index for this call
Log::into('logs-2025.02.02')->create(['message' => 'hello']);

Released under the MIT License.