]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MiscSearch.pm
*search: simplify retry_reopen users
[public-inbox.git] / lib / PublicInbox / MiscSearch.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # read-only counterpart to MiscIdx
5 package PublicInbox::MiscSearch;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::Search qw(retry_reopen);
9
10 # Xapian value columns:
11 our $MODIFIED = 0;
12
13 # avoid conflicting with message Search::prob_prefix for UI/UX reasons
14 my %PROB_PREFIX = (
15         description => 'S', # $INBOX_DIR/description
16         address => 'A',
17         listid => 'XLISTID',
18         url => 'XURL',
19         infourl => 'XINFOURL',
20         name => 'XNAME',
21         '' => 'S A XLISTID XNAME XURL XINFOURL'
22 );
23
24 sub new {
25         my ($class, $dir) = @_;
26         bless {
27                 xdb => $PublicInbox::Search::X{Database}->new($dir)
28         }, $class;
29 }
30
31 # read-only
32 sub mi_qp_new ($) {
33         my ($self) = @_;
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+
42         $cb->($qp, 100);
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);
47         }
48         $qp->add_boolean_prefix('type', 'T');
49         $qp;
50 }
51
52 sub misc_enquire_once { # retry_reopen callback
53         my ($self, $qr, $opt) = @_;
54         my $eq = $PublicInbox::Search::X{Enquire}->new($self->{xdb});
55         $eq->set_query($qr);
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);
61         } elsif ($rel) {
62                 $eq->set_sort_by_relevance_then_value($MODIFIED, $desc);
63         } else {
64                 $eq->set_sort_by_value_then_relevance($MODIFIED, $desc);
65         }
66         $eq->get_mset($opt->{offset} || 0, $opt->{limit} || 200);
67 }
68
69 sub mset {
70         my ($self, $qs, $opt) = @_;
71         $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);
77 }
78
79 sub ibx_data_once {
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);
85         if ($head != $tail) {
86                 my $doc = $xdb->get_document($head->get_docid);
87                 $doc->get_data;
88         } else {
89                 undef;
90         }
91 }
92
93 sub inbox_data {
94         my ($self, $ibx) = @_;
95         retry_reopen($self, \&ibx_data_once, $ibx);
96 }
97
98 1;