One of the most useful features of the listings and search system is the ability to add custom data into the document. Typically, this will be data to be consumed in the code that forms the storefront listings page.
To extend a document, use the add() method, to define a closure that will return an array of data to merge into the document structure. This is typically done from within a ServiceProvider. If the code is unique to a particular store it could be placed in the boot method of the AppServiceProvider. If it is to be included as part of a module, then it can be added in the setup method of the module's ServiceProvider.
\Aero\Search\Elastic\Documents\ListingDocument::add('search-result', function ($document) {
$colors = $document->getModel()->visibleVariants()->pluck('attributes')->flatten()->filter(function ($attribute) {
return strpos(strtolower($attribute->group->name), 'color') !== false;
})->pluck('name')->unique()->values()->all();
return [
'colors' => $colors,
];
});
The available section of the document that can be extended are:
- search-result
- search-data