From cff5900f5220d596fdf7b0aa770cb96cf85be95d Mon Sep 17 00:00:00 2001 From: darrell-k Date: Wed, 11 Sep 2024 19:21:13 +0100 Subject: [PATCH] further improvements --- Slim/Control/Queries.pm | 73 +++++++++++++++------------- Slim/Plugin/FullTextSearch/Plugin.pm | 10 +--- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/Slim/Control/Queries.pm b/Slim/Control/Queries.pm index ce7de93ea3..ed78b9b43b 100644 --- a/Slim/Control/Queries.pm +++ b/Slim/Control/Queries.pm @@ -519,7 +519,6 @@ sub albumsQuery { name => 'albumsSearch', search => $fromSearch, type => 'album', - createPrimaryKey => 1, }); $sql .= "JOIN albumsSearch ON albums.id = albumsSearch.id "; } else { @@ -3187,7 +3186,6 @@ sub searchQuery { name => 'quickSearch', search => $search, type => $type, - createPrimaryKey => 1, checkLargeResultset => sub { my $isLarge = shift; return ($isLarge && $isLarge > ($index + $quantity)) ? ('ORDER BY fulltextweight DESC LIMIT ' . $isLarge) : ''; @@ -4656,7 +4654,7 @@ sub worksQuery { # get them all by default my $where = {}; - my $w = ["tracks.work IS NOT NULL"]; + my $w = []; my $p = []; my $columns = "works.title, works.id, composer.name, composer.id, composer.namesort, works.titlesort, GROUP_CONCAT(DISTINCT albums.artwork), GROUP_CONCAT(DISTINCT albums.id)"; @@ -4670,22 +4668,24 @@ sub worksQuery { JOIN albums ON tracks.album = albums.id '; if (specified($search)) { + if ( Slim::Schema->canFulltextSearch ) { Slim::Plugin::FullTextSearch::Plugin->createHelperTable({ name => 'worksSearch', search => $search, type => 'work', - createPrimaryKey => 1, }); - $sql .= "JOIN worksSearch ON works.id = worksSearch.id "; - Slim::Plugin::FullTextSearch::Plugin->createHelperTable({ name => 'albumsSearch', search => $search, type => 'album', - createPrimaryKey => 1, }); - $sql .= "JOIN albumsSearch ON albums.id = albumsSearch.id "; + $sql = 'SELECT %s FROM workssearch + join works on works.id=workssearch.id + join tracks on tracks.work=workssearch.id + join albums on albums.id=tracks.album + join albumsSearch on albumsSearch.id=albums.id + join contributors composer on composer.id=works.composer '; } else { my $strings = Slim::Utils::Text::searchStringSplit($search); if ( ref $strings->[0] eq 'ARRAY' ) { @@ -4701,43 +4701,48 @@ sub worksQuery { push @{$p}, @{$strings}; } } - } - if ( defined $workID && $workID != -1 ) { - push @{$w}, "works.id = ?"; - push @{$p}, $workID; - } + } else { - if ( defined $year ) { - push @{$w}, "tracks.year = ?"; - push @{$p}, $year; - } + push @{$w}, "tracks.work IS NOT NULL"; - if ( defined $roleID ) { - my @roles = split(',', $roleID); - if (scalar @roles) { - push @{$p}, map { Slim::Schema::Contributor->typeToRole($_) } @roles; - push @{$w}, 'contributor_track.role IN (' . join(', ', map {'?'} @roles) . ')'; + if ( defined $workID && $workID != -1 ) { + push @{$w}, "works.id = ?"; + push @{$p}, $workID; + } + + if ( defined $year ) { + push @{$w}, "tracks.year = ?"; + push @{$p}, $year; + } + + if ( defined $roleID ) { + my @roles = split(',', $roleID); + if (scalar @roles) { + push @{$p}, map { Slim::Schema::Contributor->typeToRole($_) } @roles; + push @{$w}, 'contributor_track.role IN (' . join(', ', map {'?'} @roles) . ')'; + } + } + + if (defined $artistID) { + push @{$w}, "contributors.id = ?"; + push @{$p}, $artistID; + } + + if (defined $genreID) { + my @genreIDs = split(/,/, $genreID); + $sql .= 'JOIN genre_track ON genre_track.track = tracks.id '; + push @{$w}, 'genre_track.genre IN (' . join(', ', map {'?'} @genreIDs) . ')'; + push @{$p}, @genreIDs; } - } - if (defined $artistID) { - push @{$w}, "contributors.id = ?"; - push @{$p}, $artistID; } if (defined $libraryID) { - push @{$w}, 'EXISTS (SELECT * FROM library_album WHERE library_album.album = albums.id AND library_album.library = ?)'; + push @{$w}, 'EXISTS (SELECT 1 FROM library_album WHERE library_album.album = albums.id AND library_album.library = ?)'; push @{$p}, $libraryID; } - if (defined $genreID) { - my @genreIDs = split(/,/, $genreID); - $sql .= 'JOIN genre_track ON genre_track.track = tracks.id '; - push @{$w}, 'genre_track.genre IN (' . join(', ', map {'?'} @genreIDs) . ')'; - push @{$p}, @genreIDs; - } - if ( @{$w} ) { $sql .= 'WHERE '; $sql .= join( ' AND ', @{$w} ); diff --git a/Slim/Plugin/FullTextSearch/Plugin.pm b/Slim/Plugin/FullTextSearch/Plugin.pm index f1a218637e..420c6501b5 100644 --- a/Slim/Plugin/FullTextSearch/Plugin.pm +++ b/Slim/Plugin/FullTextSearch/Plugin.pm @@ -266,7 +266,6 @@ sub createHelperTable { my $name = $args->{name}; my $type = $args->{type}; - my $createPrimaryKey = $args->{createPrimaryKey} || 0; my ($tokens, $isLarge); my $orderOrLimit = ''; @@ -288,19 +287,14 @@ sub createHelperTable { $orderOrLimit = 'LIMIT 0' if !$tokens; # The first 32 bytes of the ID are either an MD5 of the ID, or some buster to make it "non searchable" - remove that prefix - my $searchSQL = "SELECT SUBSTR(fulltext.id, 33) AS id, FULLTEXTWEIGHT(matchinfo(fulltext)) AS fulltextweight FROM fulltext WHERE fulltext MATCH 'type:$type $tokens' $orderOrLimit"; - if ($createPrimaryKey) { - $dbh->do("CREATE $temp TABLE $name (id integer primary key, fulltextweight integer)"); - $searchSQL = "INSERT INTO $name $searchSQL"; - } else { - $searchSQL = "CREATE $temp TABLE $name AS $searchSQL"; - } + my $searchSQL = "INSERT INTO $name SELECT SUBSTR(fulltext.id, 33) AS id, FULLTEXTWEIGHT(matchinfo(fulltext)) AS fulltextweight FROM fulltext WHERE fulltext MATCH 'type:$type $tokens' $orderOrLimit"; if ( main::DEBUGLOG ) { my $log2 = $sqllog->is_debug ? $sqllog : $log; $log2->is_debug && $log2->debug( "Fulltext search query ($type): $searchSQL" ); } + $dbh->do("CREATE $temp TABLE $name (id INTEGER PRIMARY KEY, fulltextweight INTEGER)"); $dbh->do($searchSQL); }