Skip to content

Commit

Permalink
(#586) Add basic support for pages: to search
Browse files Browse the repository at this point in the history
  • Loading branch information
Difegue committed Mar 16, 2023
1 parent 4369bfb commit e73f6c7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lib/LANraragi/Model/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ sub check_cache {
sub search_uncached {

my ( $category_id, $filter, $sortkey, $sortorder, $newonly, $untaggedonly ) = @_;
my $redis = LANraragi::Model::Config->get_redis_search;
my $logger = get_logger( "Search Core", "lanraragi" );
my $redis = LANraragi::Model::Config->get_redis_search;
my $redis_db = LANraragi::Model::Config->get_redis;
my $logger = get_logger( "Search Core", "lanraragi" );

# Compute search filters
my @tokens = compute_search_filter($filter);

# Prepare array: For each token, we'll have a list of matching archive IDs.
# We intersect those lists as we proceed to get the final result.
# Start with all our IDs.
my @filtered = LANraragi::Model::Config->get_redis->keys('????????????????????????????????????????');
my @filtered = $redis_db->keys('????????????????????????????????????????');

# If we're using a category, we'll need to get its source data first.
my %category = LANraragi::Model::Category::get_category($category_id);
Expand Down Expand Up @@ -152,6 +153,31 @@ sub search_uncached {

my @ids = ();

# Specific case for pagecount searches
# You can search for galleries with a specific number of pages with pages:20, or with a page range: pages:>20 pages:<=30.
if ( $tag =~ /^pages:(>|<|>=|<=)?(\d+)$/ ) {
my $operator = $1;
my $pagecount = $2;

$logger->debug("Searching for IDs with pages $operator $pagecount");

# If no operator is specified, we assume it's an exact match
$operator = "=" if !$operator;

# Go through all IDs in @filtered and check if they have the right pagecount
# This could be sped up with an index, but it's probably not worth it.
foreach my $id (@filtered) {
my $count = $redis_db->hget( $id, "pagecount" );
if ( ( $operator eq "=" && $count == $pagecount )
|| ( $operator eq ">" && $count > $pagecount )
|| ( $operator eq ">=" && $count >= $pagecount )
|| ( $operator eq "<" && $count < $pagecount )
|| ( $operator eq "<=" && $count <= $pagecount ) ) {
push @ids, $id;
}
}
}

# For exact tag searches, just check if an index for it exists
if ( $isexact && $redis->exists("INDEX_$tag") ) {

Expand Down Expand Up @@ -246,6 +272,8 @@ sub search_uncached {
}
}

$redis->quit();
$redis_db->quit();
return @filtered;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/search.t
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ ok( $filtered eq 1 && $ids[0] eq "e4c422fd10943dc169e3489a38cdbf57101a5f7e", qq(
( $total, $filtered, @ids ) = LANraragi::Model::Search::do_search( "", "", 0, 0, 0, 0, 1 );
ok( $filtered eq 2 && $ids[0] eq "4857fd2e7c00db8b0af0337b94055d8445118630", qq(Search with untagged filter applied) );

$search = qq(pages:>150);
do_test_search();
is( $ids[0], "e69e43e1355267f7d32a4f9b7f2fe108d2401ebg", qq(Pagecount search ($search)) );

done_testing();
1 change: 1 addition & 0 deletions tools/Documentation/basic-operations/searching.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The search bar in LANraragi tries to not be too dumb and will actively suggest t

If you want to queue multiple terms/tags, you have to use **commas** to separate them, much like how tags are entered in the metadata field when editing an archive.
You can mix both tags and a specific title in a search if you want.
You can search for galleries with a specific number of pages with `pages:20`, or with a page range: `pages:>20, pages:<=30`.

You can also use the following special characters in a search:

Expand Down

0 comments on commit e73f6c7

Please sign in to comment.