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.
For example, if you want to split the body
attribute, the method name should 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:
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.
Split using a splitter
Sometimes you need to isolate the splitting logic into a dedicated class.
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.
}
}
Write 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:
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
The distinct feature 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 distinct, configure the attributeForDistinct
in your config/scout-articles.php
configuration file:
// ...
/*
|--------------------------------------------------------------------------
| 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',
// ...