API Reference / Laravel / Split large records
Feb. 12, 2019

Split large records

Split large records

For performance reasons, objects in Algolia should be 10kb or less. Large records can be split into smaller records by splitting on a logical chunk such as paragraphs or sentences.

To split an attribute, your searchable class must implement a splitAttribute method - this means that if you want to split the body attribute, the method name will be splitBody.

Split 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);
    }
}

Split using a splitter

Of course, sometimes you will 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 splitters

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. The declared dependencies will automatically be resolved and injected into the splitter instance.

Writing a splitter is simple. Create a new class that implements Algolia\ScoutExtended\Contracts\SplitterContract, and 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 allows you to force the 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, it will be created when you run the scout:optimize or scout:sync Artisan commands.

Did you find this page helpful?