Skip to content

Commit

Permalink
further improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
darrell-k committed Sep 11, 2024
1 parent c95e89e commit cff5900
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
73 changes: 39 additions & 34 deletions Slim/Control/Queries.pm
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ sub albumsQuery {
name => 'albumsSearch',
search => $fromSearch,
type => 'album',
createPrimaryKey => 1,
});
$sql .= "JOIN albumsSearch ON albums.id = albumsSearch.id ";
} else {
Expand Down Expand Up @@ -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) : '';
Expand Down Expand Up @@ -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)";
Expand All @@ -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' ) {
Expand All @@ -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} );
Expand Down
10 changes: 2 additions & 8 deletions Slim/Plugin/FullTextSearch/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ sub createHelperTable {

my $name = $args->{name};
my $type = $args->{type};
my $createPrimaryKey = $args->{createPrimaryKey} || 0;

my ($tokens, $isLarge);
my $orderOrLimit = '';
Expand All @@ -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);
}

Expand Down

0 comments on commit cff5900

Please sign in to comment.