Skip to content

Commit

Permalink
[BUGFIX] Fix SolrSearch implementation (#978)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer committed Sep 15, 2023
1 parent eb1bffd commit b7f2404
Showing 1 changed file with 67 additions and 27 deletions.
94 changes: 67 additions & 27 deletions Classes/Common/SolrSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,41 @@
*/
class SolrSearch implements \Countable, \Iterator, \ArrayAccess, QueryResultInterface
{
protected $result;
/**
* @var DocumentRepository
*/
private $documentRepository;

/**
* @var QueryResult|Collection
*/
private $collection;

/**
* @var array
*/
private $settings;

/**
* @var array
*/
private $searchParams;

/**
* @var QueryResult
*/
private $listedMetadata;

/**
* @var array
*/
private $params;

private $result;

/**
* @var int
*/
protected $position = 0;

/**
Expand All @@ -47,7 +81,7 @@ public function getNumLoadedDocuments()
return count($this->result['documents']);
}

public function count()
public function count(): int
{
if ($this->result === null) {
return 0;
Expand All @@ -66,22 +100,22 @@ public function key()
return $this->position;
}

public function next()
public function next(): void
{
$this->position++;
}

public function rewind()
public function rewind(): void
{
$this->position = 0;
}

public function valid()
public function valid(): bool
{
return isset($this[$this->position]);
}

public function offsetExists($offset)
public function offsetExists($offset): bool
{
$idx = $this->result['document_keys'][$offset];
return isset($this->result['documents'][$idx]);
Expand Down Expand Up @@ -116,12 +150,12 @@ public function offsetGet($offset)
return $document;
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
throw new \Exception("SolrSearch: Modifying result list is not supported");
}

public function offsetUnset($offset)
public function offsetUnset($offset): void
{
throw new \Exception("SolrSearch: Modifying result list is not supported");
}
Expand Down Expand Up @@ -541,25 +575,7 @@ protected function searchSolr($parameters = [], $enableCache = true)

foreach ($uidGroup as $group) {
foreach ($group as $record) {
$resultDocument = new ResultDocument($record, $highlighting, $fields);

$document = [
'id' => $resultDocument->getId(),
'page' => $resultDocument->getPage(),
'snippet' => $resultDocument->getSnippets(),
'thumbnail' => $resultDocument->getThumbnail(),
'title' => $resultDocument->getTitle(),
'toplevel' => $resultDocument->getToplevel(),
'type' => $resultDocument->getType(),
'uid' => !empty($resultDocument->getUid()) ? $resultDocument->getUid() : $parameters['uid'],
'highlight' => $resultDocument->getHighlightsIds(),
];
foreach ($parameters['listMetadataRecords'] as $indexName => $solrField) {
if (!empty($record->$solrField)) {
$document['metadata'][$indexName] = $record->$solrField;
}
}
$resultSet['documents'][] = $document;
$resultSet['documents'][] = $this->getDocument($record, $highlighting, $fields, $parameters);
}
}

Expand All @@ -573,4 +589,28 @@ protected function searchSolr($parameters = [], $enableCache = true)
}
return $resultSet;
}

private function getDocument($record, $highlighting, $fields, $parameters) {
$resultDocument = new ResultDocument($record, $highlighting, $fields);

$document = [
'id' => $resultDocument->getId(),
'page' => $resultDocument->getPage(),
'snippet' => $resultDocument->getSnippets(),
'thumbnail' => $resultDocument->getThumbnail(),
'title' => $resultDocument->getTitle(),
'toplevel' => $resultDocument->getToplevel(),
'type' => $resultDocument->getType(),
'uid' => !empty($resultDocument->getUid()) ? $resultDocument->getUid() : $parameters['uid'],
'highlight' => $resultDocument->getHighlightsIds(),
];

foreach ($parameters['listMetadataRecords'] as $indexName => $solrField) {
if (!empty($record->$solrField)) {
$document['metadata'][$indexName] = $record->$solrField;
}
}

return $document;
}
}

0 comments on commit b7f2404

Please sign in to comment.