Skip to content

Commit

Permalink
Merge changes from #276 into a new branch
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Apr 12, 2024
1 parent a1fb769 commit 8c65144
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 47 deletions.
36 changes: 34 additions & 2 deletions src/ScoutIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use yii\base\BaseObject;

/**
* @property-read ElementQuery $criteria
* @property-read array|ElementQuery $criteria
*/
class ScoutIndex extends BaseObject
{
Expand All @@ -24,6 +24,9 @@ class ScoutIndex extends BaseObject
/** @var <class-string> */
public $elementType = Entry::class;

/** @var bool */
public $enforceElementType = true;

/** @var callable|string|array|\League\Fractal\TransformerAbstract */
public $transformer;

Expand All @@ -33,7 +36,7 @@ class ScoutIndex extends BaseObject
/** @var bool */
public $replicaIndex = false;

/** @var callable|ElementQuery */
/** @var callable|ElementQuery|ElementQuery[] */
private $_criteria;

public function __construct(string $indexName, $config = [])
Expand Down Expand Up @@ -67,6 +70,35 @@ public function criteria(callable $criteria): self
return $this;
}


/**
* @throws Exception
*/
public function getElements(callable $getElements): self
{
$elementQueries = $getElements();

if ($elementQueries instanceof ElementQuery) {
$elementQueries = [$elementQueries];
}

// loop through $elementQueries and check that they are all ElementQuery objects
foreach ($elementQueries as $elementQuery) {
if (!$elementQuery instanceof ElementQuery) {
throw new Exception('You must return a valid ElementQuery or array of ElementQuery objects from the getElements function.');
}

if (is_null($elementQuery->siteId)) {
$elementQuery->siteId = Craft::$app->getSites()->getPrimarySite()->id;
}
}

$this->enforceElementType = false;
$this->criteria = $elementQueries;

return $this;
}

/**
* Leverage magic method calling to get the $criteria property, allowing
* lazy calling the Criteria callable.
Expand Down
24 changes: 22 additions & 2 deletions src/behaviors/SearchableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class SearchableBehavior extends Behavior

public function validatesCriteria(ScoutIndex $scoutIndex): bool
{
if (is_iterable($scoutIndex->criteria)) {
foreach($scoutIndex->criteria as $query) {
$criteria = clone $query;

if (!$criteria->id($this->owner->id)->exists()) {
return false;
}
}
return true;

}

$criteria = clone $scoutIndex->criteria;

return $criteria
Expand All @@ -52,12 +64,20 @@ public function getIndices(): Collection
->getSettings()
->getIndices()
->filter(function(ScoutIndex $scoutIndex) {
if(is_iterable($scoutIndex->criteria)){
$criteriaSiteIds = collect($scoutIndex->criteria)->map(function($criteria){
return Arr::wrap($criteria->siteId);
})->flatten()->unique()->values()->toArray();
} else {
$criteriaSiteIds = Arr::wrap($scoutIndex->criteria->siteId);
}

$siteIds = array_map(function($siteId) {
return (int) $siteId;
}, Arr::wrap($scoutIndex->criteria->siteId));
}, $criteriaSiteIds);

return $scoutIndex->elementType === get_class($this->owner)
&& ($scoutIndex->criteria->siteId === '*'
&& ($criteriaSiteIds === '*'
|| in_array((int) $this->owner->siteId, $siteIds));
});
}
Expand Down
45 changes: 35 additions & 10 deletions src/console/controllers/scout/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,41 @@ public function actionImport($index = '')
]));
$this->stdout("Added ImportIndex job for '{$engine->scoutIndex->indexName}' to the queue".PHP_EOL, Console::FG_GREEN);
} else {
$totalElements = $engine->scoutIndex->criteria->count();
$elementsUpdated = 0;
$batch = $engine->scoutIndex->criteria->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
$elementsUpdated += count($elements);
$this->stdout("Updated {$elementsUpdated}/{$totalElements} element(s) in {$engine->scoutIndex->indexName}\n", Console::FG_GREEN);
// check if $engine->scoutIndex->criteria is iterable
if (is_iterable($engine->scoutIndex->criteria)) {
// use array_reduce to get the count of elements
$totalElements = array_reduce($engine->scoutIndex->criteria, function ($carry, $query) {
return $carry + $query->count();
}, 0);



foreach($engine->scoutIndex->criteria as $query) {
$elementsUpdated = 0;
$batch = $query->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
$elementsUpdated += count($elements);
$this->stdout("Updated {$elementsUpdated}/{$totalElements} element(s) in {$engine->scoutIndex->indexName} via " . get_class($query) . "\n", Console::FG_GREEN);
}
}

} else {
$totalElements = $engine->scoutIndex->criteria->count();

$elementsUpdated = 0;
$batch = $engine->scoutIndex->criteria->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
$elementsUpdated += count($elements);
$this->stdout("Updated {$elementsUpdated}/{$totalElements} element(s) in {$engine->scoutIndex->indexName}\n", Console::FG_GREEN);
}
}
}
});
Expand Down
37 changes: 30 additions & 7 deletions src/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,36 @@ public function actionImport()
return $this->redirect(UrlHelper::url('utilities/' . ScoutUtility::id()));
}

$elementsCount = $engine->scoutIndex->criteria->count();
$batch = $engine->scoutIndex->criteria->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
// check if $engine->scoutIndex->criteria is iterable
if (is_iterable($engine->scoutIndex->criteria)) {
// use array_reduce to get the count of elements
$elementsCount = array_reduce($engine->scoutIndex->criteria, function ($carry, $query) {
return $carry + $query->count();
}, 0);

$elementsUpdated = 0;

foreach($engine->scoutIndex->criteria as $query) {
$batch = $query->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
}
}

} else {
$elementsCount = $engine->scoutIndex->criteria->count();

$elementsUpdated = 0;
$batch = $engine->scoutIndex->criteria->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
}
}

Craft::$app->getSession()->setNotice("Updated {$elementsCount} element(s) in {$engine->scoutIndex->indexName}");
Expand Down
10 changes: 5 additions & 5 deletions src/engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public function update($elements)
return;
}

$elements = new Collection(Arr::wrap($elements));

$elements = $elements->filter(function(Element $element) {
return get_class($element) === $this->scoutIndex->elementType;
});
if($this->scoutIndex->enforceElementType) {
$elements = $elements->filter(function (Element $element) {
return get_class($element) === $this->scoutIndex->elementType;
});
}

if ($elements->isEmpty()) {
return;
Expand Down
42 changes: 33 additions & 9 deletions src/jobs/ImportIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,40 @@ public function execute($queue): void
return;
}

$elementsCount = $engine->scoutIndex->criteria->count();
$elementsUpdated = 0;
$batch = $engine->scoutIndex->criteria->batch(
Scout::$plugin->getSettings()->batch_size
);
// check if $engine->scoutIndex->criteria is iterable
if (is_iterable($engine->scoutIndex->criteria)) {
// use array_reduce to get the count of elements
$elementsCount = array_reduce($engine->scoutIndex->criteria, function ($carry, $query) {
return $carry + $query->count();
}, 0);

$elementsUpdated = 0;

foreach($engine->scoutIndex->criteria as $query) {
$batch = $query->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
$elementsUpdated += count($elements);
$this->setProgress($queue, $elementsUpdated / $elementsCount);
}
}

} else {
$elementsCount = $engine->scoutIndex->criteria->count();

$elementsUpdated = 0;
$batch = $engine->scoutIndex->criteria->batch(
Scout::$plugin->getSettings()->batch_size
);

foreach ($batch as $elements) {
$engine->update($elements);
$elementsUpdated += count($elements);
$this->setProgress($queue, $elementsUpdated / $elementsCount);
foreach ($batch as $elements) {
$engine->update($elements);
$elementsUpdated += count($elements);
$this->setProgress($queue, $elementsUpdated / $elementsCount);
}
}

$event = new AfterIndexImport([
Expand Down
36 changes: 24 additions & 12 deletions src/utilities/ScoutUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Craft;
use craft\base\Utility;
use Illuminate\Support\Arr;
use rias\scout\engines\Engine;
use rias\scout\Scout;

Expand Down Expand Up @@ -38,22 +39,33 @@ public static function contentHtml(): string
];

if (!$engine->scoutIndex->replicaIndex) {
$sites = 'all';
if ($engine->scoutIndex->criteria->siteId != '*') {
$sites = [];
if (is_array($engine->scoutIndex->criteria->siteId)) {
foreach ($engine->scoutIndex->criteria->siteId as $id) {
$sites[] = Craft::$app->getSites()->getSiteById($id);
}
} else {
$sites = $engine->scoutIndex->criteria->siteId;
}

// loop through criteria to get siteId from each criteria
$engineCriteria = collect(Arr::wrap($engine->scoutIndex->criteria));

$criteriaSites = $engineCriteria->map(function ($criteria) {
return $criteria->siteId;
})->flatten()->unique()->values()->toArray();

if (count($criteriaSites) === 1 && $criteriaSites[0] === '*') {
$sites = 'all';
} else {
$sites = collect($criteriaSites)->map(function ($siteId) {
return Craft::$app->getSites()->getSiteById($siteId);
})->implode('name', ', ');
}

$totalElements = $engineCriteria->reduce(function ($carry, $criteria) {
return $carry + $criteria->count();
}, 0);

$elementType = $engine->scoutIndex->enforceElementType ? $engine->scoutIndex->elementType : 'Mixed Element Types';

$stats = array_merge($stats, [
'elementType' => $engine->scoutIndex->elementType,
'elementType' => $elementType,
'sites' => $sites,
'indexed' => $engine->getTotalRecords(),
'elements' => $engine->scoutIndex->criteria->count(),
'elements' => $totalElements,
]);
}
return $stats;
Expand Down

0 comments on commit 8c65144

Please sign in to comment.