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;
8 use Search::Xapian qw/:standard/;
9 use PublicInbox::SearchMsg;
11 use PublicInbox::MID qw/mid_clean mid_compress/;
13 # This is English-only, everything else is non-standard and may be confused as
14 # a prefix common in patch emails
15 our $REPLY_RE = qr/^re:\s+/i;
16 our $LANG = 'english';
19 # SCHEMA_VERSION history
21 # 1 - subject_path is lower-cased
22 # 2 - subject_path is mid_compress 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
27 # 7 - remove references and inreplyto terms
28 # 8 - remove redundant/unneeded document data
29 # 9 - disable Message-ID compression
32 # n.b. FLAG_PURE_NOT is expensive not suitable for a public website
33 # as it could become a denial-of-service vector
34 QP_FLAGS => FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE|FLAG_WILDCARD,
38 my %bool_pfx_internal = (
39 type => 'T', # "mail" or "ghost"
40 thread => 'G', # newsGroup (or similar entity - e.g. a web forum name)
43 my %bool_pfx_external = (
45 mid => 'Q', # uniQue id (Message-ID)
50 s => 'S', # for mairix compatibility
51 m => 'Q', # 'mid' is exact, 'm' can do partial
54 my %all_pfx = (%bool_pfx_internal, %bool_pfx_external, %prob_prefix);
56 sub xpfx { $all_pfx{$_[0]} }
59 my %meta_pfx = (mid => 1, thread => 1, path => 1);
60 while (my ($k, $v) = each %all_pfx) {
61 $PFX2TERM_RMAP{$v} = $k if $meta_pfx{$k};
64 my $mail_query = Search::Xapian::Query->new(xpfx('type') . 'mail');
67 my (undef, $git_dir) = @_;
68 "$git_dir/public-inbox/xapian" . SCHEMA_VERSION;
72 my ($class, $git_dir) = @_;
73 my $dir = $class->xdir($git_dir);
74 my $db = Search::Xapian::Database->new($dir);
75 bless { xdb => $db, git_dir => $git_dir }, $class;
78 sub reopen { $_[0]->{xdb}->reopen }
82 my ($self, $query_string, $opts) = @_;
86 unless ($query_string eq '') {
87 $query = $self->qp->parse_query($query_string, QP_FLAGS);
88 $opts->{relevance} = 1 unless exists $opts->{relevance};
91 $self->do_enquire($query, $opts);
95 my ($self, $mid, $opts) = @_;
96 my $smsg = eval { $self->lookup_message($mid) };
98 return { total => 0, msgs => [] } unless $smsg;
99 my $qtid = Search::Xapian::Query->new(xpfx('thread').$smsg->thread_id);
100 my $path = mid_compress($smsg->path);
101 my $qsub = Search::Xapian::Query->new(xpfx('path').$path);
102 my $query = Search::Xapian::Query->new(OP_OR, $qtid, $qsub);
103 $self->do_enquire($query, $opts);
107 my ($self, $query, $opts) = @_;
108 my $enquire = $self->enquire;
109 if (defined $query) {
110 $query = Search::Xapian::Query->new(OP_AND,$query,$mail_query);
112 $query = $mail_query;
114 $enquire->set_query($query);
116 my $desc = !$opts->{asc};
117 if ($opts->{relevance}) {
118 $enquire->set_sort_by_relevance_then_value(TS, $desc);
120 $enquire->set_sort_by_value_then_relevance(TS, $desc);
122 my $offset = $opts->{offset} || 0;
123 my $limit = $opts->{limit} || 50;
124 my $mset = $enquire->get_mset($offset, $limit);
125 return $mset if $opts->{mset};
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);
175 my $doc_id = $self->find_unique_doc_id('mid', $mid);
177 if (defined $doc_id) {
179 my $doc = $self->{xdb}->get_document($doc_id);
180 $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
181 $smsg->doc_id($doc_id);
186 sub find_unique_doc_id {
187 my ($self, $term, $value) = @_;
189 my ($begin, $end) = $self->find_doc_ids($term, $value);
191 return undef if $begin->equal($end); # not found
193 my $rv = $begin->get_docid;
197 $begin->equal($end) or die "Term '$term:$value' is not unique\n";
201 # returns begin and end PostingIterator
203 my ($self, $term, $value) = @_;
205 $self->find_doc_ids_for_term(xpfx($term) . $value);
208 # returns begin and end PostingIterator
209 sub find_doc_ids_for_term {
210 my ($self, $term) = @_;
211 my $db = $self->{xdb};
213 ($db->postlist_begin($term), $db->postlist_end($term));
216 # normalize subjects so they are suitable as pathnames for URLs
219 $subj = subject_normalized($subj);
220 $subj =~ s![^a-zA-Z0-9_\.~/\-]+!_!g;
224 sub subject_normalized {
226 $subj =~ s/\A\s+//s; # no leading space
227 $subj =~ s/\s+\z//s; # no trailing space
228 $subj =~ s/\s+/ /gs; # no redundant spaces
229 $subj =~ s/\.+\z//; # no trailing '.'
230 $subj =~ s/$REPLY_RE//igo; # remove reply prefix
235 sub subject_summary {
238 if (length($subj) > $max) {
239 my @subj = split(/\s+/, $subj);
243 while ($l = shift @subj) {
244 my $new = $subj . $l . ' ';
245 last if length($new) >= $max;
249 my $r = scalar @subj ? ' ...' : '';
252 # subject has one REALLY long word, and NOT spam? wtf
253 @subj = ($l =~ /\A(.{1,72})/);
254 $subj = $subj[0] . ' ...';
262 $self->{enquire} ||= Search::Xapian::Enquire->new($self->{xdb});