Manticore + Laravel + Scout

Easy integration of Manticore Search engine with Laravel framework using Laravel Scout and unofficial driver manticore scout engine.

Despite the fact that the driver is unofficial, it copes perfectly with all the tasks that are required of it, namely:

  • Elementary installation and configuration.

  • Convenient data indexer. No need to write your own for RT tables.

  • Automatic index management during database manipulations.

  • Simple search with basic capabilities and even a little more.

Let's move on to action.

Installing manticore

Install the necessary libraries

composer require laravel/scout
composer require romanstruk/manticore-scout-engine

Add configuration files to the configs folder

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
php artisan vendor:publish --provider="RomanStruk\ManticoreScoutEngine\ManticoreServiceProvider"

In .env we specify the required driver

SCOUT_DRIVER=manticore

Model settings

Each database table will have its own index table in Manticore.
In the model for which the search is required, it is necessary to define fields, their types And table-index settings for manticore.
In addition, it is necessary to determine toSearchableArrayto specify what data will be saved to Manticore. Here, if necessary, you can modify this data. If you do not do this, then scout will try to save all the data from the table, which may cause errors.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Post extends Model
{
    use Searchable;

    public function scoutIndexMigration(): array
    {
        return [
            'fields' => [
                'id' => ['type' => 'bigint'],
                'name' => ['type' => 'text'],
                'category' => ['type' => 'string stored indexed'],
            ],
            'settings' => [
                'min_prefix_len' => '3',
                'min_infix_len' => '3',
                'expand_keywords' => '1',
            ],
        ];
    }

    // Какие данные сохранять и их модификация при необходимости
    public function toSearchableArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'category' => $this->category_id,
        ];
    }
}

Creating a table index in manticore

The settings defined in will be used. scoutIndexMigration.

php artisan manticore:index "App\Models\Post"

Import (indexing) data

After the above steps, call the methods save or create will automatically create, save and delete data from the Manticore index table. That is, the data will be automatically synchronized between your database and the Manticore search engine indexes.

If you already have some data in tables, you can easily import it into Manticore.

php artisan scout:import "App\Models\Post"

If there is no data, this step is optional.

Search

After completing these steps, you can start searching.

Post::search('Поисковый запрос')->get();

In addition to the usual search, the driver also provides functions with the implementation of fuzzy search, auto-completion, spelling correction, highlighting words from a query and even reverse queries (Percolate queries). Details in github repositories.

If you want something more, you have specific search queries, you can always connect directly and do whatever you want.

Conclusion

Despite the surprisingly low popularity of manticore-scout-engine, this driver for Laravel Scout implements simple and convenient integration of Manticore with Laravel. The library is unfairly overlooked, so much so that it is difficult to find any mention. In addition, official support from the Laravel Scout developers is unlikely to be expected in the near future.

All of the above is in the Manticore, Laravel documentation and the repository of this driver. The purpose of the article is only to show that all of this is already possible and works effectively without official support from the Laravel Scout developers.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *