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
31 QP_FLAGS => FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE|FLAG_WILDCARD,
35 my %bool_pfx_internal = (
36 type => 'T', # "mail" or "ghost"
37 mid => 'Q', # uniQue id (Message-ID)
40 my %bool_pfx_external = (
42 thread => 'G', # newsGroup (or similar entity - e.g. a web forum name)
49 my %all_pfx = (%bool_pfx_internal, %bool_pfx_external, %prob_prefix);
51 sub xpfx { $all_pfx{$_[0]} }
54 my %meta_pfx = (mid => 1, thread => 1, path => 1);
55 while (my ($k, $v) = each %all_pfx) {
56 $PFX2TERM_RMAP{$v} = $k if $meta_pfx{$k};
59 my $mail_query = Search::Xapian::Query->new(xpfx('type') . 'mail');
62 my (undef, $git_dir) = @_;
63 "$git_dir/public-inbox/xapian" . SCHEMA_VERSION;
67 my ($class, $git_dir) = @_;
68 my $dir = $class->xdir($git_dir);
69 my $db = Search::Xapian::Database->new($dir);
70 bless { xdb => $db, git_dir => $git_dir }, $class;
73 sub reopen { $_[0]->{xdb}->reopen }
77 my ($self, $query_string, $opts) = @_;
81 unless ($query_string eq '') {
82 $query = $self->qp->parse_query($query_string, QP_FLAGS);
83 $opts->{relevance} = 1 unless exists $opts->{relevance};
86 $self->do_enquire($query, $opts);
89 sub get_subject_path {
90 my ($self, $path, $opts) = @_;
91 my $query = $self->qp->parse_query("path:".mid_compress($path), 0);
92 $self->do_enquire($query, $opts);
96 my ($self, $mid, $opts) = @_;
97 my $smsg = eval { $self->lookup_message($mid) };
99 return { total => 0, msgs => [] } unless $smsg;
101 my $qtid = $qp->parse_query('thread:'.$smsg->thread_id, 0);
102 my $qsub = $qp->parse_query('path:'.mid_compress($smsg->path), 0);
103 my $query = Search::Xapian::Query->new(OP_OR, $qtid, $qsub);
104 $self->do_enquire($query, $opts);
110 my ($self, $query, $opts) = @_;
111 my $enquire = $self->enquire;
112 if (defined $query) {
113 $query = Search::Xapian::Query->new(OP_AND,$query,$mail_query);
115 $query = $mail_query;
117 $enquire->set_query($query);
118 if ($opts->{relevance}) {
119 $enquire->set_sort_by_relevance_then_value(TS, 1);
121 $enquire->set_sort_by_value_then_relevance(TS, 1);
124 my $offset = $opts->{offset} || 0;
125 my $limit = $opts->{limit} || 50;
126 my $mset = $enquire->get_mset($offset, $limit);
127 return $mset if $opts->{mset};
129 PublicInbox::SearchMsg->load_doc($_->get_document);
132 { total => $mset->get_matches_estimated, msgs => \@msgs }
136 sub stemmer { Search::Xapian::Stem->new($LANG) }
142 my $qp = $self->{query_parser};
146 $qp = Search::Xapian::QueryParser->new;
147 $qp->set_default_op(OP_AND);
148 $qp->set_database($self->{xdb});
149 $qp->set_stemmer($self->stemmer);
150 $qp->set_stemming_strategy(STEM_SOME);
151 $qp->add_valuerangeprocessor($self->ts_range_processor);
152 $qp->add_valuerangeprocessor($self->date_range_processor);
154 while (my ($name, $prefix) = each %bool_pfx_external) {
155 $qp->add_boolean_prefix($name, $prefix);
158 while (my ($name, $prefix) = each %prob_prefix) {
159 $qp->add_prefix($name, $prefix);
162 $self->{query_parser} = $qp;
165 sub ts_range_processor {
166 $_[0]->{tsrp} ||= Search::Xapian::NumberValueRangeProcessor->new(TS);
169 sub date_range_processor {
170 $_[0]->{drp} ||= Search::Xapian::DateValueRangeProcessor->new(TS);
174 my ($self, $mid) = @_;
175 $mid = mid_clean($mid);
177 my $doc_id = $self->find_unique_doc_id('mid', $mid);
179 if (defined $doc_id) {
181 my $doc = $self->{xdb}->get_document($doc_id);
182 $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
183 $smsg->doc_id($doc_id);
188 sub find_unique_doc_id {
189 my ($self, $term, $value) = @_;
191 my ($begin, $end) = $self->find_doc_ids($term, $value);
193 return undef if $begin->equal($end); # not found
195 my $rv = $begin->get_docid;
199 $begin->equal($end) or die "Term '$term:$value' is not unique\n";
203 # returns begin and end PostingIterator
205 my ($self, $term, $value) = @_;
207 $self->find_doc_ids_for_term(xpfx($term) . $value);
210 # returns begin and end PostingIterator
211 sub find_doc_ids_for_term {
212 my ($self, $term) = @_;
213 my $db = $self->{xdb};
215 ($db->postlist_begin($term), $db->postlist_end($term));
218 # normalize subjects so they are suitable as pathnames for URLs
221 $subj = subject_normalized($subj);
222 $subj =~ s![^a-zA-Z0-9_\.~/\-]+!_!g;
226 sub subject_normalized {
228 $subj =~ s/\A\s+//s; # no leading space
229 $subj =~ s/\s+\z//s; # no trailing space
230 $subj =~ s/\s+/ /gs; # no redundant spaces
231 $subj =~ s/\.+\z//; # no trailing '.'
232 $subj =~ s/$REPLY_RE//igo; # remove reply prefix
237 sub subject_summary {
240 if (length($subj) > $max) {
241 my @subj = split(/\s+/, $subj);
245 while ($l = shift @subj) {
246 my $new = $subj . $l . ' ';
247 last if length($new) >= $max;
251 my $r = scalar @subj ? ' ...' : '';
254 # subject has one REALLY long word, and NOT spam? wtf
255 @subj = ($l =~ /\A(.{1,72})/);
256 $subj = $subj[0] . ' ...';
264 $self->{enquire} ||= Search::Xapian::Enquire->new($self->{xdb});