Skip to content

Commit

Permalink
pkp#8710 Fixed sorting and numeric search (submission ID)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed Aug 25, 2023
1 parent 819508c commit 3ff69bf
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions classes/submission/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ public function getQueryBuilder(): Builder
$q->whereIn('s.context_id', $this->contextIds);
}

// Prepare keywords, but allows short words
$keywords = collect(Application::getSubmissionSearchIndex()->filterKeywords($this->searchPhrase, false, true))->unique();
// Prepare keywords (allows short and numeric words)
$keywords = collect(Application::getSubmissionSearchIndex()->filterKeywords($this->searchPhrase, false, true, true))->unique();

// Setup the order by
switch ($this->orderBy) {
case self::ORDERBY_DATE_PUBLISHED:
$q->addSelect(['po.date_published']);
Expand Down Expand Up @@ -342,13 +344,18 @@ public function getQueryBuilder(): Builder
}
// Retrieves the number of matches for all keywords
$orderByMatchCount = DB::table('submission_search_objects', 'sso')
->join("submission_search_object_keywords AS ssok", "ssok.object_id", '=', 'sso.object_id')
->join("submission_search_keyword_list AS sskl", "sskl.keyword_id", '=', "ssok.keyword_id")
->whereIn("sskl.keyword_text", $keywords->map(fn () => DB::raw("CONCAT(LOWER(?), '%')")))->addBinding($keywords->toArray())
->join('submission_search_object_keywords AS ssok', 'ssok.object_id', '=', 'sso.object_id')
->join('submission_search_keyword_list AS sskl', 'sskl.keyword_id', '=', 'ssok.keyword_id')
->where(fn (Builder $q) =>
$keywords->map(fn (string $keyword) => $q
->orWhere('sskl.keyword_text', '=', DB::raw('LOWER(?)'))
->addBinding($keyword)
)
)
->whereColumn('s.submission_id', '=', 'sso.submission_id')
->selectRaw('COUNT(0)');
// Retrieves the number of distinct matched keywords
$orderByDistinctKeyword = (clone $orderByMatchCount)->groupBy('sskl.keyword_id');
$orderByDistinctKeyword = (clone $orderByMatchCount)->select(DB::raw('COUNT(DISTINCT sskl.keyword_id)'));
$q->orderBy($orderByDistinctKeyword, $this->orderDirection)
->orderBy($orderByMatchCount, $this->orderDirection);
break;
Expand Down Expand Up @@ -432,6 +439,7 @@ public function getQueryBuilder(): Builder

// Search phrase
if ($keywords->count()) {
$likePattern = DB::raw("CONCAT('%', LOWER(?), '%')");
if(!empty($this->assignedTo)) {
// Holds a single random row to check whether we have any assignment
$q->leftJoinSub(fn (Builder $q) => $q
Expand All @@ -442,16 +450,15 @@ public function getQueryBuilder(): Builder
'any_assignment', 'any_assignment.value', '=', DB::raw('1')
);
}
$likePattern = DB::raw("CONCAT('%', LOWER(?), '%')");
// Builds the filters
$q->where(fn (Builder $q) => $keywords
->map(fn (string $keyword) => $q
// Look for matches on the indexed data
->orWhereExists(fn (Builder $query) => $query
->from('submission_search_objects', 'sso')
->join('submission_search_object_keywords AS ssok', 'sso.object_id', '=', 'ssok.object_id')
->join("submission_search_keyword_list AS sskl", "sskl.keyword_id", '=', "ssok.keyword_id")
->where("sskl.keyword_text", '=', DB::raw("CONCAT(LOWER(?), '%')"))->addBinding($keyword)
->join('submission_search_keyword_list AS sskl', 'sskl.keyword_id', '=', 'ssok.keyword_id')
->where('sskl.keyword_text', '=', DB::raw('LOWER(?)'))->addBinding($keyword)
->whereColumn('s.submission_id', '=', 'sso.submission_id')
// Don't permit reviewers to search on author names
->when(!empty($this->assignedTo), fn (Builder $q) => $q
Expand Down Expand Up @@ -493,13 +500,16 @@ public function getQueryBuilder(): Builder
->where(DB::raw('LOWER(aus.setting_value)'), 'LIKE', $likePattern)
->addBinding($keyword)
)
// Search for exact submission ID
// Search for the exact submission ID
->when(
($numericWords = $keywords->filter(fn (string $keyword) => ctype_digit($keyword)))->count(),
fn (Builder $query) => $query->orWhereIn('s.submission_id', $numericWords)
)
)
);
} elseif (strlen($this->searchPhrase)) {
// If there's search text, but no keywords could be extracted from it, force the query to return nothing
$q->whereRaw('1 = 0');
}

if (isset($this->categoryIds)) {
Expand Down

0 comments on commit 3ff69bf

Please sign in to comment.