🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Framework integration / Laravel / Advanced use cases

For performance and relevance reasons, objects in Algolia should be 10 KB or less. You can create smaller records by splitting big records into logical chunks such as paragraphs or sentences.

To split an attribute, your Searchable class must implement a split${Attribute} method—this means that if you want to split the body attribute, the method name should be splitBody.

Splitting directly on the Searchable class

The most basic way to split a record is by doing it directly on the Searchable class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use Searchable;

    /**
     * Splits the given value.
     *
     * @param  string $value
     * @return array
     */
    public function splitBody($value)
    {
        return explode('. ', $value);
    }
}

If all split methods for a specific model return an empty array, no records are sent to Algolia.

Splitting using a splitter

Of course, sometimes you need to isolate the splitting logic into a dedicated class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
namespace App;

use Illuminate\Database\Eloquent\Model;
use Algolia\ScoutExtended\Splitters\HtmlSplitter;

class Article extends Model
{
    use Searchable;

    /**
     * Splits the given value.
     *
     * @param  string $value
     * @return string
     */
    public function splitBody($value)
    {
        return HtmlSplitter::class; // You can also return an instance instead of the class name.
    }
}

Writing a splitter

One of the primary benefits of creating a splitter class is the ability to type-hint any dependencies your splitter may need in its constructor. Laravel automatically resolves and injects these dependencies into your splitter instance.

To write a splitter, create a new class that implements the Algolia\ScoutExtended\Contracts\SplitterContract. The split method should split the given $value as needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace App\Search\Splitters;

use App\Contracts\SplitterServiceContract;
use Algolia\ScoutExtended\Contracts\SplitterContract;

class CustomSplitter implements SplitterContract
{
     /**
     * @var \App\Contracts\SplitterServiceContract
     */
    protected $service;

     /**
     * Creates a new instance of the class.
     *
     * @param  \App\Contracts\SplitterServiceContract $service
     *
     * @return void
     */
    public function __construct(SplitterService $service)
    {
         $this->service = $service;
    }

    /**
     * Splits the given value.
     *
     * @param  object $searchable
     * @param  mixed $value
     *
     * @return array
     */
    public function split($searchable, $value): array
    {
        $values = $this->service->split($searchable->articleType, $value);

        return $values;
    }
}

Distinct

Distinct functionality lets you force Algolia to return distinct results based on one attribute defined in attributeForDistinct. Using this attribute, you can limit the number of returned records that contain the same value in that attribute.

To use the distinct functionality, you should configure the attributeForDistinct in your config/scout-articles.php configuration file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    // ...
    /*
    |--------------------------------------------------------------------------
    | Distinct
    |--------------------------------------------------------------------------
    |
    | Using this attribute, you can limit the number of returned records that contain the same
    | value in that attribute. For example, if the distinct attribute is the series_name and
    | several hits (Episodes) have the same value for series_name (Laravel From Scratch).
    |
    | Supported(distinct): Boolean
    | Supported(attributeForDistinct): Null, String
    | Example(attributeForDistinct): 'slug'
    */

    'distinct' => true,
    'attributeForDistinct' => 'slug',
    // ...

Remember, if the config/scout-articles.php file doesn’t exist, the scout:optimize or scout:sync Artisan commands automatically create this file.

Did you find this page helpful?