Scout


Support Open Source. Buy beer.

This plugin is licensed under a MIT license, which means that it's completely free open source software, and you can use it for whatever and however you wish. If you're using it and want to support the development, buy me a beer over at Beerpay!

Beerpay

Setup

To define your indices, create a new scout.php file within your config folder. This file should return an array with 3 keys, an application_id, your admin_api_key (which are both found in your Algolia account) and a mappings key, which defines your site's mappings.

Within the mappings array, each index is represented by a configuration array.

<?php

return [
    "application_id" => "algolia",
    "admin_api_key" => "algolia",
    "mappings" => [
        [
            'indexName' => 'blog',
            'elementType' => \craft\elements\Entry::class,
            'criteria' => [
                'section' => 'blog'
            ],
            'transformer' => function (craft\base\Element $element) {
                return $element->toArray();
            }
        ],
        ...
    ],
];

Mapping configuration settings

indexName

The index name in Algolia, if you don't already have an index created, Scout will create one for you.

elementType

The element type that this index contains, most of the time this will be craft\elements\Entry::class

Craft's default element type classes are:

  • craft\elements\Asset
  • craft\elements\Category
  • craft\elements\Entry
  • craft\elements\GlobalSet
  • craft\elements\MatrixBlock
  • craft\elements\Tag
  • craft\elements\User
'elementType' => craft\elements\Entry::class,

criteria

An array of parameters that should be set on the Element Query that limits which entries go inside the index. These criteria are also used when importing through the console command.

'criteria' => [
    'section' => 'blog',
],

transformer

The transformer that should be used to define the data that should be sent to Algolia for each element. If you don’t set this, the default transformer will be used, which includes all of the element’s direct attribute values, but no custom field values.

// Can be set to a function
'transformer' => function(craft\elements\Entry $entry) {
    return [
        'title' => $entry->title,
        'id' => $entry->id,
        'url' => $entry->url,
    ];
},

// Or a string/array that defines a Transformer class configuration
'transformer' => 'MyTransformerClassName',

// Or a Transformer class instance
'transformer' => new MyTransformerClassName(),

Your custom transformer class would look something like this:

<?php

use craft\elements\Entry;
use League\Fractal\TransformerAbstract;

class MyTransformerClassName extends TransformerAbstract
{
    public function transform(Entry $entry)
    {
        return [
            // ...
        ];
    }
}

Console commands

Scout provides two easy console commands for managing your indices.

Importing

To import one or all indices you can run the following console command

./craft scout/index/import <indexName?>

The indexName argument is not required, all your mappings will be imported when you omit it.

Flushing/Clearing

Clearing an index is as easy as running a command in your console.

./craft scout/index/flush <indexName?>

As with the import command, indexName is not required, when flushing Scout will ask you to confirm that you really want to clear all the data in your index.

Credits

Brought to you by Rias


Free

Package Name

To install this plugin, search for its package name on the Plugin Store and click “Install”.


Information

  • Version 1.2.1
  • Last Update 5/17/2019
  • Active Installs 831
  • Compatibility Craft 3
  • License MIT
  • Categories Performance, Integrations

Report an issue