Skip to content

Commit

Permalink
Adds siteid to splitElementIndex (#41)
Browse files Browse the repository at this point in the history
* Adds siteid to splitElementIndex, reintroduces deindex on element delete but also adds a siteid check for deindexing

* Comply to styleci
  • Loading branch information
larsboldt authored and Rias committed Nov 23, 2018
1 parent 7e587e7 commit 615a0db
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
17 changes: 16 additions & 1 deletion src/Scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function (ModelEvent $event) {
Element::EVENT_BEFORE_DELETE,
function (ModelEvent $event) {
if ($this->settings->sync) {
$this->indexElements($event->sender);
$this->deindexElements($event->sender);
}
}
);
Expand Down Expand Up @@ -147,6 +147,21 @@ protected function indexElements($elements)
self::$plugin->scoutService->indexElements($elements);
}

/**
* @param $elements
*
* @throws \AlgoliaSearch\AlgoliaException
* @throws \Exception
*/
protected function deindexElements($elements)
{
if (!is_array($elements)) {
$elements = [$elements];
}

self::$plugin->scoutService->deindexElements($elements);
}

/**
* Get all possible elements related to another element.
*
Expand Down
44 changes: 38 additions & 6 deletions src/models/AlgoliaIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ public function canIndexElement(Element $element)
return $this->getElementQuery($element)->count();
}

/**
* Determines if the supplied element can be deindexed in this index.
*
* @param $element Element
*
* @return bool
*/
public function canDeindexElement(Element $element)
{
if (isset($this->criteria['siteId']) && (int) $element->site->id !== (int) $this->criteria['siteId']) {
return false;
}

return $this->getElementQuery($element)->count();
}

/**
* Transforms the supplied element using the transformer method in config.
*
Expand Down Expand Up @@ -106,13 +122,29 @@ public function indexElements($elements)
if ($this->elementType === get_class($element)) {
if ($this->canIndexElement($element)) {
$this->indexElement($element);
} else {
} elseif ($this->canDeindexElement($element)) {
$this->deindexElement($element);
}
}
}
}

/**
* Adds or removes the supplied element from the index.
*
* @param $elements array
*
* @throws \yii\base\InvalidConfigException
*/
public function deindexElements($elements)
{
foreach ($elements as $element) {
if ($this->elementType === get_class($element) && $this->canDeindexElement($element)) {
$this->deindexElement($element);
}
}
}

protected function indexElement($element)
{
$elementConfigs = [];
Expand All @@ -133,15 +165,15 @@ protected function indexElement($element)
/**
* @param $element
*/
protected function deindexElement($element)
public function deindexElement($element)
{
$config = [
'indexName' => $this->indexName,
];

$config['objectID'] = $this->getSiteElementId($element);
if (count($this->splitElementIndex) > 0) {
$config['distinctId'] = $element->id;
$config['distinctId'] = $this->getSiteElementId($element);
}

Craft::$app->queue->push(new DeIndexElement($config));
Expand All @@ -153,18 +185,18 @@ protected function deindexElement($element)
protected function splitElementConfig($element)
{
$transformedElement = $this->transformElement($element);
$transformedElement['distinctId'] = $element->id;
$transformedElement['distinctId'] = $this->getSiteElementId($element);

$elementConfigs = [];
$i = 1;
foreach ($this->splitElementIndex as $indexElement) {
$transformedElement['objectID'] = $element->id.'_'.$i;
$transformedElement['objectID'] = $this->getSiteElementId($element).'_'.$i;

if ($transformedElement[$indexElement] !== null) {
if (is_array($transformedElement[$indexElement])) {
foreach ($transformedElement[$indexElement] as $key => $value) {
if ((is_array($value) && count($value) > 0) || (!is_array($value) && $value !== null)) {
$transformedElement['objectID'] = $element->id.'_'.$i;
$transformedElement['objectID'] = $this->getSiteElementId($element).'_'.$i;

$splitElement = array_filter($transformedElement, function ($item) {
return !in_array($item, $this->splitElementIndex, true);
Expand Down
15 changes: 15 additions & 0 deletions src/services/ScoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@ public function indexElements($elements)
$algoliaIndex->indexElements($elements);
}
}

/**
* Passes the supplied elements to each configured index.
*
* @param $elements array
*
* @throws \AlgoliaSearch\AlgoliaException
* @throws \Exception
*/
public function deindexElements($elements)
{
foreach ($this->getMappings() as $algoliaIndex) {
$algoliaIndex->deindexElements($elements);
}
}
}

0 comments on commit 615a0db

Please sign in to comment.