1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # read-only counterpart to MiscIdx
5 package PublicInbox::MiscSearch;
8 use PublicInbox::Search qw(retry_reopen);
10 # Xapian value columns:
13 # avoid conflicting with message Search::prob_prefix for UI/UX reasons
15 description => 'S', # $INBOX_DIR/description
19 infourl => 'XINFOURL',
21 '' => 'S A XLISTID XNAME XURL XINFOURL'
25 my ($class, $dir) = @_;
27 xdb => $PublicInbox::Search::X{Database}->new($dir)
34 my $xdb = $self->{xdb};
35 my $qp = $PublicInbox::Search::X{QueryParser}->new;
36 $qp->set_default_op(PublicInbox::Search::OP_AND());
37 $qp->set_database($xdb);
38 $qp->set_stemmer(PublicInbox::Search::stemmer($self));
39 $qp->set_stemming_strategy(PublicInbox::Search::STEM_SOME());
40 my $cb = $qp->can('set_max_wildcard_expansion') //
41 $qp->can('set_max_expansion'); # Xapian 1.5.0+
43 $cb = $qp->can('add_valuerangeprocessor') //
44 $qp->can('add_rangeprocessor'); # Xapian 1.5.0+
45 while (my ($name, $prefix) = each %PROB_PREFIX) {
46 $qp->add_prefix($name, $_) for split(/ /, $prefix);
48 $qp->add_boolean_prefix('type', 'T');
52 sub misc_enquire_once { # retry_reopen callback
53 my ($self, $qr, $opt) = @_;
54 my $eq = $PublicInbox::Search::X{Enquire}->new($self->{xdb});
56 my $desc = !$opt->{asc};
57 my $rel = $opt->{relevance} // 0;
58 if ($rel == -1) { # ORDER BY docid/UID
59 $eq->set_docid_order($PublicInbox::Search::ENQ_ASCENDING);
60 $eq->set_weighting_scheme($PublicInbox::Search::X{BoolWeight}->new);
62 $eq->set_sort_by_relevance_then_value($MODIFIED, $desc);
64 $eq->set_sort_by_value_then_relevance($MODIFIED, $desc);
66 $eq->get_mset($opt->{offset} || 0, $opt->{limit} || 200);
70 my ($self, $qs, $opt) = @_;
72 my $qp = $self->{qp} //= mi_qp_new($self);
73 $qs = 'type:inbox' if $qs eq '';
74 my $qr = $qp->parse_query($qs, $PublicInbox::Search::QP_FLAGS);
75 $opt->{relevance} = 1 unless exists $opt->{relevance};
76 retry_reopen($self, \&misc_enquire_once, $qr, $opt);
80 my ($self, $ibx) = @_;
81 my $xdb = $self->{xdb};
82 my $eidx_key = $ibx->eidx_key; # may be {inboxdir}, so private
83 my $head = $xdb->postlist_begin('Q'.$eidx_key);
84 my $tail = $xdb->postlist_end('Q'.$eidx_key);
86 my $doc = $xdb->get_document($head->get_docid);
94 my ($self, $ibx) = @_;
95 retry_reopen($self, \&ibx_data_once, $ibx);