Skip to content

Instantly share code, notes, and snippets.

@timkelty
Last active May 19, 2021 03:16
Show Gist options
  • Save timkelty/867d97cca3b8763228950748b562920f to your computer and use it in GitHub Desktop.
Save timkelty/867d97cca3b8763228950748b562920f to your computer and use it in GitHub Desktop.
<?php
use craft\elements\db\EntryQuery;
use craft\elements\Entry;
use modules\appmodule\transformers\ProductTransformer;
use rias\scout\IndexSettings;
use rias\scout\ScoutIndex;
use Tightenco\Collect\Support\Collection;
$indexPrefix = getenv('ALGOLIA_INDEX_PREFIX') ?: CRAFT_ENVIRONMENT . '_';
$baseProductsIndexSettings = IndexSettings::create()
->searchableAttributes([
'title',
'manufacturer.title',
'productCategories.title',
'richText',
])
->attributesForFaceting([
'manufacturer',
'reviewRatingAvg',
'price.min',
'length.min',
'weight.min',
'seatingConfiguration',
'cockpitType',
'skillLevel',
'structure',
'idealPaddlerSize',
'maxCapacity',
'volume',
'width',
'maxWidth',
'pfdEntryStyle',
'uscgType',
'shaftType',
'shaftMaterial',
'bladeMaterial',
'featherAngles',
'numberOfPieces',
// Required for filters (not facets)
'productCategories',
]);
$indices = [
ScoutIndex::create($indexPrefix.'products')
->elementType(Entry::class)
->criteria(function (EntryQuery $query) {
return $query->section('products');
})
->transformer(ProductTransformer::class)
->indexSettings(
(clone $baseProductsIndexSettings)
->replicas(
[
$indexPrefix.'productsByTitleAsc',
$indexPrefix.'productsByPriceAsc',
$indexPrefix.'productsByPriceDesc',
$indexPrefix.'productsByReviewRatingDesc',
]
)
),
];
// https://github.com/riasvdv/craft-scout/issues/115
if (Craft::$app->request->getIsConsoleRequest() &&
in_array('scout/settings/update', Craft::$app->request->params)
) {
$indices = array_merge(
$indices,
[
ScoutIndex::create($indexPrefix.'productsByTitleAsc')
->indexSettings(
(clone $baseProductsIndexSettings)
->customRanking([
'asc(title)',
])
),
ScoutIndex::create($indexPrefix.'productsByPriceAsc')
->indexSettings(
(clone $baseProductsIndexSettings)
->customRanking([
'asc(price.min)',
])
),
ScoutIndex::create($indexPrefix.'productsByPriceDesc')
->indexSettings(
(clone $baseProductsIndexSettings)
->customRanking([
'desc(price.min)',
])
),
ScoutIndex::create($indexPrefix.'productsByReviewRatingDesc')
->indexSettings(
(clone $baseProductsIndexSettings)
->customRanking([
'desc(reviewRating)',
])
),
]
);
}
return [
'*' => [
'sync' => true,
'queue' => true,
'connect_timeout' => 1,
'batch_size' => 100,
'application_id' => getenv('ALGOLIA_APPLICATION_ID'),
'admin_api_key' => getenv('ALGOLIA_WRITE_API_KEY'),
'search_api_key' => getenv('ALGOLIA_SEARCH_API_KEY'),
'indices' => $indices,
],
'local' => [
'sync' => false,
]
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment