1. Go to this page and download the library: Download rias/craft-scout library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rias / craft-scout example snippets
return [
/*
* Scout listens to numerous Element events to keep them updated in
* their respective indices. You can disable these and update
* your indices manually using the commands.
*/
'sync' => true,
/*
*
* @depcretio
* By default Scout handles all indexing in a queued job, you can disable
* this so the indices are updated as soon as the elements are updated
*
* Disabling the `queue` option will no longer be supported in the next version of Scout
*
*/
'queue' => true,
/*
* The connection timeout (in seconds), increase this only if necessary
*/
'connect_timeout' => 1,
/*
* The batch size Scout uses when importing a large amount of elements
*/
'batch_size' => 1000,
/*
* By default Scout will index elements related to the element being save (that are in the same index).
* Disabling this can improve performance on larger sites that have lots of relations.
*/
'indexRelations' => true,
/**
* The element types to look for when indexRelations is enabled.
* By default, all Craft elements are checked for relations.
* Use this to avoid unnecessary queries to Elements that aren't
* used by your indices or to check custom Elements that may be
* related to your indices
*/
'relatedElementTypes' => [],
/*
* The Algolia Application ID, this id can be found in your Algolia Account
* https://www.algolia.com/api-keys. This id is used to update records.
*/
'application_id' => '$ALGOLIA_APPLICATION_ID',
/*
* The Algolia Admin API key, this key can be found in your Algolia Account
* https://www.algolia.com/api-keys. This key is used to update records.
*/
'admin_api_key' => '$ALGOLIA_ADMIN_API_KEY',
/*
* The Algolia search API key, this key can be found in your Algolia Account
* https://www.algolia.com/api-keys. This search key is not used in Scout
* but can be used through the Scout variable in your template files.
*/
'search_api_key' => '$ALGOLIA_SEARCH_API_KEY', //optional
/*
* A collection of indices that Scout should sync to, these can be configured
* by using the \rias\scout\ScoutIndex::create('IndexName') command. Each
* index should define an ElementType, criteria and a transformer.
*/
'indices' => [],
];
return [
'indices' => [
\rias\scout\ScoutIndex::create('Blog')
// Scout uses this by default, so this is optional
->elementType(\craft\elements\Entry::class)
// If you don't define a siteId, the primary site is used
->criteria(function (\craft\elements\db\EntryQuery $query) {
return $query->section('blog');
})
/*
* The element gets passed into the transform function, you can omit this
* and Scout will use the \rias\scout\ElementTransformer class instead
*/
->transformer(function (\craft\elements\Entry $entry) {
return [
'title' => $entry->title,
'body' => $entry->body,
];
})
/*
* You can use this to define index settings that get synced when you call
* the ./craft scout/settings/update console command. This way you can
* keep your index settings in source control. The IndexSettings
* object provides autocompletion for all Algolia's settings
*/
->indexSettings(
\rias\scout\IndexSettings::create()
->minWordSizefor1Typo(4)
)
],
];
// Can be set to a function
->transformer(function(craft\elements\Entry $entry) {
return [
'title' => $entry->title,
'id' => $entry->id,
'url' => $entry->url,
];
}),
// Or a string/array that defines a Transformer class configuration
->transformer('MyTransformerClassName'),
// Or a Transformer class instance
->transformer(new MyTransformerClassName()),
use craft\elements\Entry;
use League\Fractal\TransformerAbstract;
class MyTransformerClassName extends TransformerAbstract
{
public function transform(Entry $entry)
{
return [
// ...
];
}
}
ScoutIndex::create()
->transform(function (Entry $entry) {
// Check if entry is valid for indexing
$isValid = yourCustomValidation($entry);
// If entry fails validation, return empty array
if (! $isValid) {
return [];
}
// Return normal data attributes
return [
'name' => $entry->title,
...
'lorem' => $entry->lorem,
'ipsum' => $entry->ipsum,
];
});
use rias\scout\behaviors\SearchableBehavior;
use rias\scout\events\ShouldBeSearchableEvent;
Event::on(
SearchableBehavior::class,
SearchableBehavior::EVENT_SHOULD_BE_SEARCHABLE,
function (ShouldBeSearchableEvent $event) {
$event->shouldBeSearchable = false;
});
use rias\scout\events\AfterIndexImport;
use rias\scout\jobs\ImportIndex;
Event::on(
ImportIndex::class,
ImportIndex::EVENT_AFTER_INDEX_IMPORT,
function (AfterIndexImport $event) {
// Your code here
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.