From 615a0db6d81ac2c32958c2faf90d762ca528bd24 Mon Sep 17 00:00:00 2001 From: Lars Boldt Date: Fri, 23 Nov 2018 09:47:05 +0100 Subject: [PATCH] Adds siteid to splitElementIndex (#41) * Adds siteid to splitElementIndex, reintroduces deindex on element delete but also adds a siteid check for deindexing * Comply to styleci --- src/Scout.php | 17 +++++++++++++- src/models/AlgoliaIndex.php | 44 ++++++++++++++++++++++++++++++----- src/services/ScoutService.php | 15 ++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/Scout.php b/src/Scout.php index c0ea606..9276670 100644 --- a/src/Scout.php +++ b/src/Scout.php @@ -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); } } ); @@ -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. * diff --git a/src/models/AlgoliaIndex.php b/src/models/AlgoliaIndex.php index 88b8ce8..504838d 100644 --- a/src/models/AlgoliaIndex.php +++ b/src/models/AlgoliaIndex.php @@ -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. * @@ -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 = []; @@ -133,7 +165,7 @@ protected function indexElement($element) /** * @param $element */ - protected function deindexElement($element) + public function deindexElement($element) { $config = [ 'indexName' => $this->indexName, @@ -141,7 +173,7 @@ protected function deindexElement($element) $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)); @@ -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); diff --git a/src/services/ScoutService.php b/src/services/ScoutService.php index 6baca54..5cabca3 100644 --- a/src/services/ScoutService.php +++ b/src/services/ScoutService.php @@ -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); + } + } }