From: Eric Wong Date: Wed, 5 Mar 2025 23:26:47 +0000 (+0000) Subject: search: unoverload {relevance} in options X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f7885ca7e36b74c97dd4eaea3f29dfa580c51cf;p=thirdparty%2Fpublic-inbox.git search: unoverload {relevance} in options Take the lead from xap_helper and split out handling of {asc} and {sort_col} fields while keeping the {relevance} field as a boolean. As with xap_helper, {sort_col} < 0 means Xapian::BoolWeight will be used for docid ordering. --- diff --git a/lib/PublicInbox/ExtMsg.pm b/lib/PublicInbox/ExtMsg.pm index d994b7836..dd7240fb2 100644 --- a/lib/PublicInbox/ExtMsg.pm +++ b/lib/PublicInbox/ExtMsg.pm @@ -33,7 +33,7 @@ sub search_partial ($$) { my ($ibx, $mid) = @_; return if length($mid) < $MIN_PARTIAL_LEN; my $srch = $ibx->isrch or return; - my $opt = { limit => PARTIAL_MAX, relevance => -1 }; + my $opt = { limit => PARTIAL_MAX, sort_col => -1, asc => 1 }; my @try = ("m:$mid*"); my $chop = $mid; if ($chop =~ s/(\W+)(\w*)\z//) { diff --git a/lib/PublicInbox/IMAP.pm b/lib/PublicInbox/IMAP.pm index e72c646c4..c3b0b41e5 100644 --- a/lib/PublicInbox/IMAP.pm +++ b/lib/PublicInbox/IMAP.pm @@ -1117,7 +1117,8 @@ sub search_common { my $srch = $self->{ibx}->isrch or return "$tag BAD search not available for mailbox\r\n"; my $opt = { - relevance => -1, + sort_col => -1, + asc => 1, limit => UID_SLICE, uid_range => $range_info }; diff --git a/lib/PublicInbox/Isearch.pm b/lib/PublicInbox/Isearch.pm index 5f22c2f2f..8fe3c54a1 100644 --- a/lib/PublicInbox/Isearch.pm +++ b/lib/PublicInbox/Isearch.pm @@ -83,7 +83,7 @@ sub mset_to_artnums { my $docids = PublicInbox::Search::mset_to_artnums($self->{es}, $mset); my $ibx_id = $self->{-ibx_id} //= _ibx_id($self); my $qmarks = join(',', map { '?' } @$docids); - if ($opt && ($opt->{relevance} // 0) == -1) { # -1 => ENQ_ASCENDING + if ($opt->{asc} && ($opt->{sort_col} // 0) < 0) { # docid ascending my $range = ''; my @r; if (my $r = $opt->{uid_range}) { diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm index eadf811f4..787cd3a51 100644 --- a/lib/PublicInbox/LeiQuery.pm +++ b/lib/PublicInbox/LeiQuery.pm @@ -45,7 +45,7 @@ sub _start_query { # used by "lei q" and "lei up" }; # descending docid order is cheapest, MUA controls sorting order - $self->{mset_opt}->{relevance} //= -2 if $l2m || $opt->{threads}; + $self->{mset_opt}->{sort_col} = -1 if $l2m || $opt->{threads}; my $tot = $self->{mset_opt}->{total} //= $self->{opt}->{limit} // 10000; $self->{mset_opt}->{limit} = $tot > 10000 ? 10000 : $tot; @@ -138,7 +138,7 @@ sub lei_q { if ($sort eq 'relevance') { $mset_opt{relevance} = 1; } elsif ($sort eq 'docid') { - $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2; + $mset_opt{sort_col} = -1; } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) { # the default } else { diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm index 9931f0172..3fe7841c5 100644 --- a/lib/PublicInbox/LeiUp.pm +++ b/lib/PublicInbox/LeiUp.pm @@ -25,7 +25,7 @@ sub up1 ($$) { my $cli_exclude = delete $lei->{opt}->{exclude}; my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return; my $f = $lss->{'-f'}; - my $mset_opt = $lei->{mset_opt} = { relevance => -2 }; + my $mset_opt = $lei->{mset_opt} = { sort_col => -1 }; my $q = $lss->{-cfg}->get_all('lei.q') // die("lei.q unset in $f (out=$out)\n"); my $lse = $lei->{lse} // die 'BUG: {lse} missing'; diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm index a5974a399..084eeb614 100644 --- a/lib/PublicInbox/Mbox.pm +++ b/lib/PublicInbox/Mbox.pm @@ -249,7 +249,7 @@ sub mbox_all { return mbox_all_ids($ctx) if $qstr !~ /\S/; my $srch = $ctx->{srch} = $ctx->{ibx}->isrch or return PublicInbox::WWW::need($ctx, 'Search'); - my $opt = $ctx->{qopts} = { relevance => -2 }; # ORDER BY docid DESC + my $opt = $ctx->{qopts} = { sort_col => -1 }; # ORDER BY docid DESC # {threadid} limits results to a given thread # {threads} collapses results from messages in the same thread, diff --git a/lib/PublicInbox/MiscSearch.pm b/lib/PublicInbox/MiscSearch.pm index f244e664c..663c53524 100644 --- a/lib/PublicInbox/MiscSearch.pm +++ b/lib/PublicInbox/MiscSearch.pm @@ -116,7 +116,7 @@ sub ibx_cache_load { sub _nntpd_cache_load { # retry_reopen callback my ($self) = @_; - my $opt = { limit => $self->{xdb}->get_doccount * 10, relevance => -1 }; + my $opt = { limit => $self->{xdb}->get_doccount * 10, sort_col => -1 }; my $mset = mset($self, 'type:newsgroup type:inbox', $opt); my $cache = {}; for my $it ($mset->items) { diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm index 9b35c8d4e..4fa028858 100644 --- a/lib/PublicInbox/Search.pm +++ b/lib/PublicInbox/Search.pm @@ -466,20 +466,9 @@ sub xh_opt ($$) { my @ret; push @ret, '-o', $opt->{offset} if $opt->{offset}; push @ret, '-m', $lim; - my $rel = $opt->{relevance} // 0; - if ($rel == -2) { # ORDER BY docid/UID (highest first) - push @ret, '-k', '-1'; - } elsif ($rel == -1) { # ORDER BY docid/UID (lowest first) - push @ret, '-k', '-1'; - push @ret, '-a'; - } elsif ($rel == 0) { - push @ret, '-k', $opt->{sort_col} // TS; - push @ret, '-a' if $opt->{asc}; - } else { # rel > 0 - push @ret, '-r'; - push @ret, '-k', $opt->{sort_col} // TS; - push @ret, '-a' if $opt->{asc}; - } + push @ret, '-r' if $opt->{relevance}; + push @ret, '-k', $opt->{sort_col} // TS; + push @ret, '-a' if $opt->{asc}; push @ret, '-t' if $opt->{threads}; push @ret, '-T', $opt->{threadid} if defined $opt->{threadid}; push @ret, '-O', $opt->{eidx_key} if defined $opt->{eidx_key}; @@ -514,19 +503,15 @@ sub do_enquire { # shared with CodeSearch and MiscSearch my $enq = $X{Enquire}->new(xdb($self)); $enq->set_query($qry); my $col = $opt->{sort_col} // TS; - my $rel = $opt->{relevance} // 0; - if ($rel == -2) { # ORDER BY docid/UID (highest first) - $enq->set_weighting_scheme($X{BoolWeight}->new); - $enq->set_docid_order($ENQ_DESCENDING); - } elsif ($rel == -1) { # ORDER BY docid/UID (lowest first) + if ($col < 0) { $enq->set_weighting_scheme($X{BoolWeight}->new); - $enq->set_docid_order($ENQ_ASCENDING); - } elsif ($rel == 0) { - $enq->set_sort_by_value_then_relevance($col, !$opt->{asc}); - } else { # rel > 0 + $enq->set_docid_order( + $opt->{asc} ? $ENQ_ASCENDING : $ENQ_DESCENDING); + } elsif ($opt->{relevance}) { $enq->set_sort_by_relevance_then_value($col, !$opt->{asc}); + } else { + $enq->set_sort_by_value_then_relevance($col, !$opt->{asc}); } - # `lei q -t / --threads' or JMAP collapseThreads; but don't collapse # on `-tt' ({threads} > 1) which sets the Flagged|Important keyword (($opt->{threads} // 0) == 1 && has_threadid($self)) and diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 8464ae1b6..5746e4346 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -50,7 +50,7 @@ sub sres_top_html { my $opt = { limit => $q->{l}, offset => $o, - relevance => $q->{r}, + relevance => !!$q->{r}, threads => $q->{t}, asc => $asc, }; diff --git a/lib/PublicInbox/WwwListing.pm b/lib/PublicInbox/WwwListing.pm index 5681377f2..101da9b72 100644 --- a/lib/PublicInbox/WwwListing.pm +++ b/lib/PublicInbox/WwwListing.pm @@ -93,7 +93,7 @@ sub add_misc_ibx { # MiscSearch->retry_reopen callback $asc = 1; $o = -($o + 1); # so [-1] is the last element, like Perl lists } - my $r = $q->{r}; + my $r = !!$q->{r}; my $opt = { offset => $o, asc => $asc, diff --git a/lib/PublicInbox/XapHelper.pm b/lib/PublicInbox/XapHelper.pm index d3c98f031..e98553c87 100644 --- a/lib/PublicInbox/XapHelper.pm +++ b/lib/PublicInbox/XapHelper.pm @@ -80,7 +80,12 @@ sub cmd_dump_ibx { $req->{A} or die 'dump_ibx requires -A PREFIX'; term_length_extract $req; my $max = $req->{'m'} // $req->{srch}->{xdb}->get_doccount; - my $opt = { relevance => -1, limit => $max, offset => $req->{o} // 0 }; + my $opt = { + sort_col => -1, + asc => 1, + limit => $max, + offset => $req->{o} // 0 + }; $opt->{eidx_key} = $req->{O} if defined $req->{O}; my $mset = $req->{srch}->mset($qry_str, $opt); $req->{0}->autoflush(1); @@ -131,8 +136,12 @@ sub cmd_dump_roots { while (defined(my $oidhex = shift @x)) { $root2off->{$oidhex} = shift @x; } - my $opt = { relevance => -1, limit => $req->{'m'}, - offset => $req->{o} // 0 }; + my $opt = { + sort_col => -1, + asc => 1, + limit => $req->{'m'}, + offset => $req->{o} // 0 + }; my $mset = $req->{srch}->mset($qry_str, $opt); $req->{0}->autoflush(1); $req->{wbuf} = ''; @@ -159,8 +168,10 @@ sub cmd_mset { # to be used by WWW + IMAP $qry_str // die 'usage: mset [OPTIONS] QRY_STR'; my $opt = { limit => $req->{'m'}, offset => $req->{o} // 0 }; $opt->{relevance} = 1 if $req->{r}; + $opt->{asc} = 1 if $req->{a}; $opt->{threads} = 1 if defined $req->{t}; $opt->{git_dir} = $req->{g} if defined $req->{g}; + $opt->{sort_col} = $req->{k} if defined $req->{k}; $opt->{eidx_key} = $req->{O} if defined $req->{O}; my @uid_range = @$req{qw(u U)}; $opt->{uid_range} = \@uid_range if grep(defined, @uid_range) == 2; diff --git a/t/lei_xsearch.t b/t/lei_xsearch.t index 977fb1e92..879256dc0 100644 --- a/t/lei_xsearch.t +++ b/t/lei_xsearch.t @@ -61,7 +61,7 @@ for my $mi ($mset->items) { } is(scalar(@msgs), $nr, 'smsgs retrieved for all'); -$mset = $lxs->mset('z:1..', { relevance => -2, limit => 1 }); +$mset = $lxs->mset('z:1..', { sort_col => -1, limit => 1 }); is($mset->size, 1, 'one result'); my @ibxish = $lxs->locals;