Skip to content

Commit

Permalink
fix: update ResultList for supporting type-spaced document keys
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 19, 2022
1 parent 47ae540 commit 89fe0df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
15 changes: 11 additions & 4 deletions src/ElasticaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ public function createIndex()
function () {
$index = $this->getIndex();
$config = $this->getIndexConfig() ?: [];
$forceRecreate = !empty($config);
return $index->create($config, $forceRecreate);

try {
$output = $index->create($config);
return $output;
} catch (\Throwable $e) {
throw new Exception($e);
}
}
);
}
Expand Down Expand Up @@ -370,13 +375,15 @@ public function getVersion(): string
public function define($recreate = false)
{
$index = $this->getIndex();
$exists = $index->exists();

if ($index->exists() && $recreate) {
if ($exists && $recreate) {
// Delete the existing index so it can be recreated from scratch
$index->delete();
$exists = false;
}

if (!$index->exists()) {
if (!$exists) {
$this->createIndex();
}

Expand Down
26 changes: 19 additions & 7 deletions src/ResultList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Exception;
use LogicException;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
Expand Down Expand Up @@ -95,9 +96,7 @@ public function __clone()
*/
public function getIDs()
{
/**
* @var $found Result[]
*/
/** @var $found Result[] */
$found = $this->getResults();

$ids = [];
Expand Down Expand Up @@ -202,17 +201,28 @@ public function toArray()
: false;

if (empty($type)) {
throw new LogicException("type field not available");
Injector::inst()->get(LoggerInterface::class)
->warn('no type field found on result: '. $item->getId());

continue;
}

if (!array_key_exists($type, $needed)) {
$needed[$type] = [$item->getId()];

$retrieved[$type] = [];
} else {
$needed[$type][] = $item->getId();
}
}

foreach ($needed as $class => $ids) {
foreach ($needed as $class => $documentIds) {
$ids = array_map(function($documentId) {
$parts = preg_split('/_/', $documentId);

return end($parts);
}, $documentIds);

foreach (DataObject::get($class)->byIDs($ids) as $record) {
$retrieved[$class][$record->ID] = $record;
}
Expand All @@ -225,10 +235,12 @@ public function toArray()
: false;

if (empty($type)) {
throw new LogicException("type field not available");
continue;
}

$id = $item->getId();
$documentId = $item->getId();
$parts = preg_split('/_/', $documentId);
$id = end($parts);

if (!isset($retrieved[$type][$id])) {
continue;
Expand Down

0 comments on commit 89fe0df

Please sign in to comment.