X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/bc472ca2d7f0f01b035cb17a414c9e7eef9a5576..refs/pull/3391/head:/app/Entities/Tools/SearchRunner.php diff --git a/app/Entities/Tools/SearchRunner.php b/app/Entities/Tools/SearchRunner.php index 3bcd6c054..a0a44f3a5 100644 --- a/app/Entities/Tools/SearchRunner.php +++ b/app/Entities/Tools/SearchRunner.php @@ -9,6 +9,7 @@ use BookStack\Entities\Models\BookChild; use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Page; use BookStack\Entities\Models\SearchTerm; +use Illuminate\Database\Connection; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -56,6 +57,8 @@ class SearchRunner * Search all entities in the system. * The provided count is for each entity to search, * Total returned could be larger and not guaranteed. + * + * @return array{total: int, count: int, has_more: bool, results: Entity[]} */ public function searchEntities(SearchOptions $searchOpts, string $entityType = 'all', int $page = 1, int $count = 20, string $action = 'view'): array { @@ -141,14 +144,14 @@ class SearchRunner $relations = ['tags']; if ($entityModelInstance instanceof BookChild) { - $relations['book'] = function(BelongsTo $query) { - $query->visible(); + $relations['book'] = function (BelongsTo $query) { + $query->scopes('visible'); }; } if ($entityModelInstance instanceof Page) { - $relations['chapter'] = function(BelongsTo $query) { - $query->visible(); + $relations['chapter'] = function (BelongsTo $query) { + $query->scopes('visible'); }; } @@ -237,6 +240,8 @@ class SearchRunner * Create a select statement, with prepared bindings, for the given * set of scored search terms. * + * @param array $scoredTerms + * * @return array{statement: string, bindings: string[]} */ protected function selectForScoredTerms(array $scoredTerms): array @@ -258,6 +263,13 @@ class SearchRunner ]; } + /** + * For the terms in the given search options, query their popularity across all + * search terms then provide that back as score adjustment multiplier applicable + * for their rarity. Returns an array of float multipliers, keyed by term. + * + * @return array + */ protected function getTermAdjustments(SearchOptions $options): array { if (isset($this->termAdjustmentCache[$options])) { @@ -301,7 +313,7 @@ class SearchRunner if (empty($termCounts)) { return []; } - + $multipliers = []; $max = max(array_values($termCounts)); @@ -345,7 +357,9 @@ class SearchRunner // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will // search the value as a string which prevents being able to do number-based operations // on the tag values. We ensure it has a numeric value and then cast it just to be sure. - $tagValue = (float) trim($query->getConnection()->getPdo()->quote($tagValue), "'"); + /** @var Connection $connection */ + $connection = $query->getConnection(); + $tagValue = (float) trim($connection->getPdo()->quote($tagValue), "'"); $query->whereRaw("value ${tagOperator} ${tagValue}"); } else { $query->where('value', $tagOperator, $tagValue);