Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor versions search, part 2 #3300

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions module/VuFind/src/VuFind/RecordDriver/Feature/VersionAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

namespace VuFind\RecordDriver\Feature;

use VuFindSearch\Command\WorkExpressionsCommand;
use VuFindSearch\Command\SearchCommand;
use VuFindSearch\Query\WorkKeysQuery;

/**
* Logic for record versions support.
Expand Down Expand Up @@ -70,7 +71,7 @@ public function getOtherVersionCount()
}

if (!isset($this->otherVersionsCount)) {
if (!$this->tryMethod('getWorkKeys')) {
if (!($keys = $this->tryMethod('getWorkKeys'))) {
if (!($this instanceof VersionAwareInterface)) {
throw new \Exception(
'VersionAwareTrait requires VersionAwareInterface'
Expand All @@ -79,13 +80,11 @@ public function getOtherVersionCount()
return false;
}

$params = new \VuFindSearch\ParamBag();
$params->add('rows', 0);
$command = new WorkExpressionsCommand(
$command = new SearchCommand(
$this->getSourceIdentifier(),
$this->getUniqueID(),
false,
$params
new WorkKeysQuery($this->getUniqueID(), false, $keys),
0,
0
);
$results = $this->searchService->invoke($command)->getResult();
$this->otherVersionsCount = $results->getTotal();
Expand All @@ -104,23 +103,18 @@ public function getOtherVersionCount()
*/
public function getVersions($includeSelf = false, $count = 20, $offset = 0)
{
if (null === $this->searchService || !$this->getWorkKeys()) {
if (null === $this->searchService || !($keys = $this->getWorkKeys())) {
return false;
}

if (!isset($this->otherVersions)) {
$params = new \VuFindSearch\ParamBag();
$params->add('rows', $count);
$params->add('start', $offset);
$command = new WorkExpressionsCommand(
$command = new SearchCommand(
$this->getSourceIdentifier(),
$this->getUniqueID(),
$includeSelf,
$params
new WorkKeysQuery($this->getUniqueID(), false, $keys),
$offset,
$count
);
$this->otherVersions = $this->searchService->invoke(
$command
)->getResult();
$this->otherVersions = $this->searchService->invoke($command)->getResult();
}
return $this->otherVersions;
}
Expand Down
3 changes: 2 additions & 1 deletion module/VuFind/src/VuFind/Search/QueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function deminify(array $search)
$type = $search['s'] ?? null;
if ('w' === $type) {
// WorkKeysQuery
return new WorkKeysQuery($search['l'], $search['i']);
return new WorkKeysQuery($search['l'], $search['i'], $search['k'] ?? []);
}
// Use array_key_exists since null is also valid
if ('b' === $type || array_key_exists('l', $search)) {
Expand Down Expand Up @@ -271,6 +271,7 @@ public static function minify(AbstractQuery $query, $topLevel = true)
return [
'l' => $query->getId(),
'i' => $query->getIncludeSelf(),
'k' => $query->getWorkKeys(),
's' => 'w',
];
}
Expand Down
2 changes: 1 addition & 1 deletion module/VuFind/tests/fixtures/searches/workkeys/min
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a:3:{s:1:"l";s:2:"id";s:1:"i";b:1;s:1:"s";s:1:"w";}
a:4:{s:1:"l";s:2:"id";s:1:"i";b:1;s:1:"k";a:2:{i:0;s:6:"AT foo";i:1;s:6:"UT bar";}s:1:"s";s:1:"w";}
Binary file modified module/VuFind/tests/fixtures/searches/workkeys/query
Binary file not shown.
92 changes: 48 additions & 44 deletions module/VuFindSearch/src/VuFindSearch/Backend/Solr/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use VuFindSearch\Feature\RandomInterface;
use VuFindSearch\Feature\RetrieveBatchInterface;
use VuFindSearch\Feature\SimilarInterface;
use VuFindSearch\Feature\WorkExpressionsInterface;
use VuFindSearch\ParamBag;
use VuFindSearch\Query\AbstractQuery;
use VuFindSearch\Query\WorkKeysQuery;
Expand All @@ -64,8 +63,7 @@ class Backend extends AbstractBackend implements
RetrieveBatchInterface,
RandomInterface,
ExtraRequestDetailsInterface,
GetIdsInterface,
WorkExpressionsInterface
GetIdsInterface
{
/**
* Limit for records per query in a batch retrieval.
Expand Down Expand Up @@ -137,7 +135,7 @@ public function search(
ParamBag $params = null
) {
if ($query instanceof WorkKeysQuery) {
return $this->workExpressions($query->getId(), $query->getIncludeSelf(), $params);
return $this->workKeysSearch($query, $offset, $limit, $params);
}
$json = $this->rawJsonSearch($query, $offset, $limit, $params);
$collection = $this->createRecordCollection($json);
Expand Down Expand Up @@ -418,46 +416,6 @@ public function alphabeticBrowse(
return $this->deserialize($response);
}

/**
* Return work expressions.
*
* @param string $id Id of record to compare with
* @param array $includeSelf Whether to include the record to compare with in the results
* @param ParamBag $defaultParams Search backend parameters
*
* @return RecordCollectionInterface
*/
public function workExpressions(
string $id,
bool $includeSelf,
ParamBag $defaultParams = null
): RecordCollectionInterface {
$recordResponse = $this->connector->retrieve($id);
$recordCollection = $this->createRecordCollection($recordResponse);
$record = $recordCollection->first();
if (!$record || !($workKeys = $record->getWorkKeys())) {
return $this->createRecordCollection('{}');
}

$params = $defaultParams ? clone $defaultParams
: new \VuFindSearch\ParamBag();
$this->injectResponseWriter($params);
$params->set('q', "{!terms f=work_keys_str_mv separator=\"\u{001f}\"}" . implode("\u{001f}", $workKeys));
if (!$includeSelf) {
$params->add('fq', sprintf('-id:"%s"', addcslashes($id, '"')));
}
if (!$params->hasParam('rows')) {
$params->add('rows', 100);
}
if (!$params->hasParam('sort')) {
$params->add('sort', 'publishDateSort desc, title_sort asc');
}
$response = $this->connector->search($params);
$collection = $this->createRecordCollection($response);
$this->injectSourceIdentifier($collection);
return $collection;
}

/**
* Write a document to Solr. Return an array of details about the updated index.
*
Expand Down Expand Up @@ -664,4 +622,50 @@ protected function injectResponseWriter(ParamBag $params)
$params->set('wt', ['json']);
$params->set('json.nl', ['arrarr']);
}

/**
* Return work expressions.
*
* @param WorkKeysQuery $query Search query
* @param int $offset Search offset
* @param int $limit Search limit
* @param ParamBag $defaultParams Search backend parameters
*
* @return RecordCollectionInterface
*/
protected function workKeysSearch(
WorkKeysQuery $query,
int $offset,
int $limit,
ParamBag $defaultParams = null
): RecordCollectionInterface {
$id = $query->getId();
if ('' === $id) {
throw new BackendException('Record ID empty in work keys query');
}
if (!($workKeys = $query->getWorkKeys())) {
$recordResponse = $this->connector->retrieve($id);
$recordCollection = $this->createRecordCollection($recordResponse);
$record = $recordCollection->first();
if (!$record || !($workKeys = $record->tryMethod('getWorkKeys'))) {
return $this->createRecordCollection('{}');
}
}

$params = $defaultParams ? clone $defaultParams : new \VuFindSearch\ParamBag();
$this->injectResponseWriter($params);
$params->set('q', "{!terms f=work_keys_str_mv separator=\"\u{001f}\"}" . implode("\u{001f}", $workKeys));
if (!$query->getIncludeSelf()) {
$params->add('fq', sprintf('-id:"%s"', addcslashes($id, '"')));
}
$params->set('rows', $limit);
$params->set('start', $offset);
if (!$params->hasParam('sort')) {
$params->add('sort', 'publishDateSort desc, title_sort asc');
}
$response = $this->connector->search($params);
$collection = $this->createRecordCollection($response);
$this->injectSourceIdentifier($collection);
return $collection;
}
}

This file was deleted.

This file was deleted.

Loading