1 # Copyright (C) 2015, all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 # based on notmuch, but with no concept of folders, files or flags
4 package PublicInbox::Search;
7 use PublicInbox::SearchMsg;
8 use Search::Xapian qw/:standard/;
10 use PublicInbox::MID qw/mid_clean mid_compressed/;
12 # This is English-only, everything else is non-standard and may be confused as
13 # a prefix common in patch emails
14 our $REPLY_RE = qr/^re:\s+/i;
15 our $LANG = 'english';
19 # SCHEMA_VERSION history
21 # 1 - subject_path is lower-cased
22 # 2 - subject_path is mid_compressed in the index, only
23 # 3 - message-ID is compressed if it includes '%' (hack!)
24 # 4 - change "Re: " normalization, avoid circular Reference ghosts
25 # 5 - subject_path drops trailing '.'
26 # 6 - preserve References: order in document data
28 QP_FLAGS => FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE|FLAG_WILDCARD,
32 my %bool_pfx_internal = (
33 type => 'T', # "mail" or "ghost"
34 mid => 'Q', # uniQue id (Message-ID or mid_compressed)
37 my %bool_pfx_external = (
39 thread => 'G', # newsGroup (or similar entity - e.g. a web forum name)
40 references => 'XREFS',
48 my %all_pfx = (%bool_pfx_internal, %bool_pfx_external, %prob_prefix);
50 sub xpfx { $all_pfx{$_[0]} }
53 my %meta_pfx = (mid => 1, thread => 1, path => 1, type => 1);
54 while (my ($k, $v) = each %all_pfx) {
55 $PFX2TERM_RMAP{$v} = $k if $meta_pfx{$k};
58 my $mail_query = Search::Xapian::Query->new(xpfx('type') . 'mail');
61 my (undef, $git_dir) = @_;
62 "$git_dir/public-inbox/xapian" . SCHEMA_VERSION;
66 my ($class, $git_dir) = @_;
67 my $dir = $class->xdir($git_dir);
68 my $db = Search::Xapian::Database->new($dir);
69 bless { xdb => $db, git_dir => $git_dir }, $class;
72 sub reopen { $_[0]->{xdb}->reopen }
76 my ($self, $query_string, $opts) = @_;
77 my $query = $self->qp->parse_query($query_string, QP_FLAGS);
79 $self->do_enquire($query, $opts);
82 sub get_subject_path {
83 my ($self, $path, $opts) = @_;
84 my $query = $self->qp->parse_query("path:".mid_compressed($path), 0);
85 $self->do_enquire($query);
88 # given a message ID, get followups to a message
90 my ($self, $mid, $opts) = @_;
91 $mid = mid_clean($mid);
92 $mid = mid_compressed($mid);
94 my $irt = $qp->parse_query("inreplyto:$mid", 0);
95 my $ref = $qp->parse_query("references:$mid", 0);
96 my $query = Search::Xapian::Query->new(OP_OR, $irt, $ref);
98 $self->do_enquire($query);
102 my ($self, $mid, $opts) = @_;
103 my $smsg = eval { $self->lookup_message($mid) };
105 return { total => 0, msgs => [] } unless $smsg;
107 my $qtid = $qp->parse_query('thread:'.$smsg->thread_id);
108 my $qsub = $qp->parse_query('path:'.mid_compressed($smsg->path));
109 my $query = Search::Xapian::Query->new(OP_OR, $qtid, $qsub);
110 $self->do_enquire($query, $opts);
116 my ($self, $query, $opts) = @_;
117 my $enquire = $self->enquire;
119 $query = Search::Xapian::Query->new(OP_AND, $query, $mail_query);
120 $enquire->set_query($query);
121 $enquire->set_sort_by_relevance_then_value(TS, 0);
123 my $offset = $opts->{offset} || 0;
124 my $limit = $opts->{limit} || 50;
125 my $mset = $enquire->get_mset($offset, $limit);
127 PublicInbox::SearchMsg->load_doc($_->get_document);
130 { total => $mset->get_matches_estimated, msgs => \@msgs }
134 sub stemmer { Search::Xapian::Stem->new($LANG) }
140 my $qp = $self->{query_parser};
144 $qp = Search::Xapian::QueryParser->new;
145 $qp->set_default_op(OP_AND);
146 $qp->set_database($self->{xdb});
147 $qp->set_stemmer($self->stemmer);
148 $qp->set_stemming_strategy(STEM_SOME);
149 $qp->add_valuerangeprocessor($self->ts_range_processor);
150 $qp->add_valuerangeprocessor($self->date_range_processor);
152 while (my ($name, $prefix) = each %bool_pfx_external) {
153 $qp->add_boolean_prefix($name, $prefix);
156 while (my ($name, $prefix) = each %prob_prefix) {
157 $qp->add_prefix($name, $prefix);
160 $self->{query_parser} = $qp;
163 sub ts_range_processor {
164 $_[0]->{tsrp} ||= Search::Xapian::NumberValueRangeProcessor->new(TS);
167 sub date_range_processor {
168 $_[0]->{drp} ||= Search::Xapian::DateValueRangeProcessor->new(TS);
172 my ($self, $mid) = @_;
173 $mid = mid_clean($mid);
174 $mid = mid_compressed($mid);
176 my $doc_id = $self->find_unique_doc_id('mid', $mid);
178 if (defined $doc_id) {
180 my $doc = $self->{xdb}->get_document($doc_id);
181 $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
182 $smsg->doc_id($doc_id);
187 sub find_unique_doc_id {
188 my ($self, $term, $value) = @_;
190 my ($begin, $end) = $self->find_doc_ids($term, $value);
192 return undef if $begin->equal($end); # not found
194 my $rv = $begin->get_docid;
198 $begin->equal($end) or die "Term '$term:$value' is not unique\n";
202 # returns begin and end PostingIterator
204 my ($self, $term, $value) = @_;
206 $self->find_doc_ids_for_term(xpfx($term) . $value);
209 # returns begin and end PostingIterator
210 sub find_doc_ids_for_term {
211 my ($self, $term) = @_;
212 my $db = $self->{xdb};
214 ($db->postlist_begin($term), $db->postlist_end($term));
217 # normalize subjects so they are suitable as pathnames for URLs
220 $subj = subject_normalized($subj);
221 $subj =~ s![^a-zA-Z0-9_\.~/\-]+!_!g;
225 sub subject_normalized {
227 $subj =~ s/\A\s+//s; # no leading space
228 $subj =~ s/\s+\z//s; # no trailing space
229 $subj =~ s/\s+/ /gs; # no redundant spaces
230 $subj =~ s/\.+\z//; # no trailing '.'
231 $subj =~ s/$REPLY_RE//igo; # remove reply prefix
237 $self->{enquire} ||= Search::Xapian::Enquire->new($self->{xdb});