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

[FEATURE] Query time boosting #1120

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
1 change: 1 addition & 0 deletions .github/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
ignoreErrors:
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::countByPid\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findByIsListed\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findByIndexIndexed\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findByIsSortable\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findOneByFeUserId\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findOneByIndexName\(\)\.#'
Expand Down
18 changes: 6 additions & 12 deletions Classes/Common/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class Indexer
'sortables' => [],
'indexed' => [],
'stored' => [],
'tokenized' => [],
'fieldboost' => []
'tokenized' => []
];

/**
Expand Down Expand Up @@ -305,11 +304,6 @@ protected static function loadIndexConf(int $pid): void
if ($indexing['index_autocomplete']) {
self::$fields['autocomplete'][] = $indexing['index_name'];
}
if ($indexing['index_boost'] > 0.0) {
self::$fields['fieldboost'][$indexing['index_name']] = floatval($indexing['index_boost']);
} else {
self::$fields['fieldboost'][$indexing['index_name']] = false;
}
}
self::$fieldsLoaded = true;
}
Expand Down Expand Up @@ -357,8 +351,8 @@ protected static function processLogical(Document $document, array $logicalUnit)
}
// There can be only one toplevel unit per UID, independently of backend configuration
$solrDoc->setField('toplevel', $logicalUnit['id'] == $doc->toplevelId ? true : false);
$solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']);
$solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']);
$solrDoc->setField('title', $metadata['title'][0]);
$solrDoc->setField('volume', $metadata['volume'][0]);
// verify date formatting
if(strtotime($metadata['date'][0])) {
$solrDoc->setField('date', self::getFormattedDate($metadata['date'][0]));
Expand Down Expand Up @@ -445,7 +439,7 @@ protected static function processPhysical(Document $document, int $page, array $
}
}
$solrDoc->setField('toplevel', false);
$solrDoc->setField('type', $physicalUnit['type'], self::$fields['fieldboost']['type']);
$solrDoc->setField('type', $physicalUnit['type']);
$solrDoc->setField('collection', $doc->metadataArray[$doc->toplevelId]['collection']);
$solrDoc->setField('location', $document->getLocation());

Expand Down Expand Up @@ -520,7 +514,7 @@ private static function processMetadata($document, $metadata, &$solrDoc): array
!empty($data)
&& substr($indexName, -8) !== '_sorting'
) {
$solrDoc->setField(self::getIndexFieldName($indexName, $document->getPid()), $data, self::$fields['fieldboost'][$indexName]);
$solrDoc->setField(self::getIndexFieldName($indexName, $document->getPid()), $data);
if (in_array($indexName, self::$fields['sortables'])) {
// Add sortable fields to index.
$solrDoc->setField($indexName . '_sorting', $metadata[$indexName . '_sorting'][0]);
Expand Down Expand Up @@ -626,7 +620,7 @@ private static function getSolrDocument(Query $updateQuery, Document $document,
$solrDoc->setField('partof', $document->getPartof());
$solrDoc->setField('root', $document->getCurrentDocument()->rootId);
$solrDoc->setField('sid', $unit['id']);
$solrDoc->setField('type', $unit['type'], self::$fields['fieldboost']['type']);
$solrDoc->setField('type', $unit['type']);
$solrDoc->setField('collection', $document->getCurrentDocument()->metadataArray[$document->getCurrentDocument()->toplevelId]['collection']);
$solrDoc->setField('fulltext', $fullText);
return $solrDoc;
Expand Down
24 changes: 23 additions & 1 deletion Classes/Common/Solr/SolrSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class SolrSearch implements \Countable, \Iterator, \ArrayAccess, QueryResultInte
*/
private ?QueryResult $listedMetadata;

/**
* @access private
* @var QueryResult|null
*/
private ?QueryResult $indexedMetadata;

/**
* @access private
* @var array
Expand Down Expand Up @@ -90,13 +96,14 @@ class SolrSearch implements \Countable, \Iterator, \ArrayAccess, QueryResultInte
*
* @return void
*/
public function __construct(DocumentRepository $documentRepository, $collections, array $settings, array $searchParams, QueryResult $listedMetadata = null)
public function __construct(DocumentRepository $documentRepository, $collections, array $settings, array $searchParams, QueryResult $listedMetadata = null, QueryResult $indexedMetadata = null)
{
$this->documentRepository = $documentRepository;
$this->collections = $collections;
$this->settings = $settings;
$this->searchParams = $searchParams;
$this->listedMetadata = $listedMetadata;
$this->indexedMetadata = $indexedMetadata;
}

/**
Expand Down Expand Up @@ -664,6 +671,21 @@ protected function searchSolr($parameters = [], $enableCache = true)
if ($enableCache === false || ($entry = $cache->get($cacheIdentifier)) === false) {
$selectQuery = $solr->service->createSelect($parameters);

$edismax = $selectQuery->getEDisMax();

$queryFields = '';

if ($this->indexedMetadata) {
foreach ($this->indexedMetadata as $metadata) {
if ($metadata->getIndexIndexed()) {
$listMetadataRecord = $metadata->getIndexName() . '_' . ($metadata->getIndexTokenized() ? 't' : 'u') . ($metadata->getIndexStored() ? 's' : 'u') . 'i';
$queryFields .= $listMetadataRecord . '^' . $metadata->getIndexBoost() . ' ';
}
}
}

$edismax->setQueryFields($queryFields);

$grouping = $selectQuery->getGrouping();
$grouping->addField('uid');
$grouping->setLimit(100); // Results in group (TODO: check)
Expand Down
5 changes: 4 additions & 1 deletion Classes/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,16 @@ public function showAction(Collection $collection): void
// get all metadata records to be shown in results
$listedMetadata = $this->metadataRepository->findByIsListed(true);

// get all indexed metadata fields
$indexedMetadata = $this->metadataRepository->findByIndexIndexed(true);

// get all sortable metadata records
$sortableMetadata = $this->metadataRepository->findByIsSortable(true);

// get all documents of given collection
$solrResults = null;
if (is_array($searchParams) && !empty($searchParams)) {
$solrResults = $this->documentRepository->findSolrByCollection($collection, $this->settings, $searchParams, $listedMetadata);
$solrResults = $this->documentRepository->findSolrByCollection($collection, $this->settings, $searchParams, $listedMetadata, $indexedMetadata);

$itemsPerPage = $this->settings['list']['paginate']['itemsPerPage'];
if (empty($itemsPerPage)) {
Expand Down
5 changes: 4 additions & 1 deletion Classes/Controller/ListViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ public function mainAction(): void
// get all metadata records to be shown in results
$listedMetadata = $this->metadataRepository->findByIsListed(true);

// get all indexed metadata fields
$indexedMetadata = $this->metadataRepository->findByIndexIndexed(true);

$solrResults = null;
$numResults = 0;
if (is_array($this->searchParams) && !empty($this->searchParams)) {
$solrResults = $this->documentRepository->findSolrByCollections($collections, $this->settings, $this->searchParams, $listedMetadata);
$solrResults = $this->documentRepository->findSolrByCollections($collections, $this->settings, $this->searchParams, $listedMetadata, $indexedMetadata);
$numResults = $solrResults->getNumFound();

$itemsPerPage = $this->settings['list']['paginate']['itemsPerPage'];
Expand Down
5 changes: 4 additions & 1 deletion Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,14 @@ public function mainAction(): void
// get all metadata records to be shown in results
$listedMetadata = $this->metadataRepository->findByIsListed(true);

// get all indexed metadata fields
$indexedMetadata = $this->metadataRepository->findByIndexIndexed(true);

$solrResults = null;
$numResults = 0;
// Do not execute the Solr search if used together with ListView plugin.
if (!$listViewSearch) {
$solrResults = $this->documentRepository->findSolrWithoutCollection($this->settings, $this->searchParams, $listedMetadata);
$solrResults = $this->documentRepository->findSolrWithoutCollection($this->settings, $this->searchParams, $listedMetadata, $indexedMetadata);
$numResults = $solrResults->getNumFound();

$itemsPerPage = $this->settings['list']['paginate']['itemsPerPage'];
Expand Down
19 changes: 11 additions & 8 deletions Classes/Domain/Repository/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,13 @@ public function findChildrenOfEach(array $uids)
* @param array $settings
* @param array $searchParams
* @param QueryResult $listedMetadata
* @param QueryResult $indexedMetadata
*
* @return SolrSearch
*/
public function findSolrByCollection(Collection $collection, $settings, $searchParams, $listedMetadata = null)
public function findSolrByCollection(Collection $collection, $settings, $searchParams, $listedMetadata = null, $indexedMetadata = null)
{
return $this->findSolr([$collection], $settings, $searchParams, $listedMetadata);
return $this->findSolr([$collection], $settings, $searchParams, $listedMetadata, $indexedMetadata);
}

/**
Expand All @@ -596,12 +597,13 @@ public function findSolrByCollection(Collection $collection, $settings, $searchP
* @param array $settings
* @param array $searchParams
* @param QueryResult $listedMetadata
* @param QueryResult $indexedMetadata
*
* @return SolrSearch
*/
public function findSolrByCollections($collections, $settings, $searchParams, $listedMetadata = null): SolrSearch
public function findSolrByCollections($collections, $settings, $searchParams, $listedMetadata = null, $indexedMetadata = null): SolrSearch
{
return $this->findSolr($collections, $settings, $searchParams, $listedMetadata);
return $this->findSolr($collections, $settings, $searchParams, $listedMetadata, $indexedMetadata);
}

/**
Expand All @@ -612,12 +614,13 @@ public function findSolrByCollections($collections, $settings, $searchParams, $l
* @param array $settings
* @param array $searchParams
* @param QueryResult $listedMetadata
* @param QueryResult $indexedMetadata
*
* @return SolrSearch
*/
public function findSolrWithoutCollection($settings, $searchParams, $listedMetadata = null): SolrSearch
public function findSolrWithoutCollection($settings, $searchParams, $listedMetadata = null, $indexedMetadata = null): SolrSearch
{
return $this->findSolr([], $settings, $searchParams, $listedMetadata);
return $this->findSolr([], $settings, $searchParams, $listedMetadata, $indexedMetadata);
}

/**
Expand All @@ -632,13 +635,13 @@ public function findSolrWithoutCollection($settings, $searchParams, $listedMetad
*
* @return SolrSearch
*/
private function findSolr($collections, $settings, $searchParams, $listedMetadata = null): SolrSearch
private function findSolr($collections, $settings, $searchParams, $listedMetadata = null, $indexedMetadata = null): SolrSearch
{
// set settings global inside this repository
// (may be necessary when SolrSearch calls back)
$this->settings = $settings;

$search = new SolrSearch($this, $collections, $settings, $searchParams, $listedMetadata);
$search = new SolrSearch($this, $collections, $settings, $searchParams, $listedMetadata, $indexedMetadata);
$search->prepare();
return $search;
}
Expand Down