diff --git a/lib/Artwork.php b/lib/Artwork.php index 9e47f026..874a521c 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -834,31 +834,6 @@ class Artwork extends PropertiesBase{ ', [$this->ArtworkId]); } - public function Contains(string $query): bool{ - $searchString = $this->Name; - - $searchString .= ' ' . $this->Artist->Name; - $searchString .= ' ' . implode(' ', $this->Artist->AlternateNames); - - foreach($this->Tags as $tag){ - $searchString .= ' ' . $tag->Name; - } - - // Remove diacritics and non-alphanumeric characters - $searchString = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($searchString))); - $query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($query))); - - if($query == ''){ - return false; - } - - if(mb_stripos($searchString, $query) !== false){ - return true; - } - - return false; - } - // *********** // ORM METHODS // *********** diff --git a/lib/Library.php b/lib/Library.php index bac49510..c1cf7d82 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -7,6 +7,7 @@ use function Safe\filesize; use function Safe\glob; use function Safe\ksort; use function Safe\preg_replace; +use function Safe\preg_split; use function Safe\shell_exec; use function Safe\sleep; use function Safe\usort; @@ -212,28 +213,34 @@ class Library{ $orderBy = 'art.CompletedYear desc'; } + // Remove diacritics and non-alphanumeric characters + $query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($query ?? ''))); + + $tokenizedQuery = '\b(' . implode('|', preg_split('/\b|\s+/', $query, -1, PREG_SPLIT_NO_EMPTY)) . ')\b'; + + $params[] = $tokenizedQuery; // art.Name + $params[] = $tokenizedQuery; // art.EbookWwwFilesystemPath + $params[] = $tokenizedQuery; // a.Name + $params[] = $tokenizedQuery; // aan.Name + $params[] = $tokenizedQuery; // t.Name + $artworks = Db::Query(' SELECT art.* from Artworks art - inner join Artists a using (ArtistId) - where ' . $statusCondition . - ' order by ' . $orderBy, $params, 'Artwork'); + inner join Artists a using (ArtistId) + left join ArtistAlternateNames aan using (ArtistId) + left join ArtworkTags at using (ArtworkId) + left join Tags t using (TagId) + where ' . $statusCondition . ' + and (art.Name regexp ? + or art.EbookWwwFilesystemPath regexp ? + or a.Name regexp ? + or aan.Name regexp ? + or t.Name regexp ?) + group by art.ArtworkId + order by ' . $orderBy, $params, 'Artwork'); - $matches = $artworks; - - if($query !== null){ - $filteredMatches = []; - - foreach($matches as $artwork){ - if($artwork->Contains($query)){ - $filteredMatches[] = $artwork; - } - } - - $matches = $filteredMatches; - } - - return $matches; + return $artworks; } /**