]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Search.pm
doc: lei-q: document SEARCH TERMS prefixes
[public-inbox.git] / lib / PublicInbox / Search.pm
1 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # based on notmuch, but with no concept of folders, files or flags
4 #
5 # Read-only search interface for use by the web and NNTP interfaces
6 package PublicInbox::Search;
7 use strict;
8 use v5.10.1;
9 use parent qw(Exporter);
10 our @EXPORT_OK = qw(retry_reopen int_val get_pct xap_terms);
11 use List::Util qw(max);
12 use POSIX qw(strftime);
13 use Carp ();
14
15 # values for searching, changing the numeric value breaks
16 # compatibility with old indices (so don't change them it)
17 use constant {
18         TS => 0, # Received: in Unix time (IMAP INTERNALDATE, JMAP receivedAt)
19         YYYYMMDD => 1, # redundant with DT below
20         DT => 2, # Date: YYYYMMDDHHMMSS (IMAP SENT*, JMAP sentAt)
21
22         # added for public-inbox 1.6.0+
23         BYTES => 3, # IMAP RFC822.SIZE
24         UID => 4, # IMAP UID == NNTP article number == Xapian docid
25         THREADID => 5, # RFC 8474, RFC 8621
26
27         # TODO
28         # REPLYCNT => ?, # IMAP ANSWERED
29
30         # SCHEMA_VERSION history
31         # 0 - initial
32         # 1 - subject_path is lower-cased
33         # 2 - subject_path is id_compress in the index, only
34         # 3 - message-ID is compressed if it includes '%' (hack!)
35         # 4 - change "Re: " normalization, avoid circular Reference ghosts
36         # 5 - subject_path drops trailing '.'
37         # 6 - preserve References: order in document data
38         # 7 - remove references and inreplyto terms
39         # 8 - remove redundant/unneeded document data
40         # 9 - disable Message-ID compression (SHA-1)
41         # 10 - optimize doc for NNTP overviews
42         # 11 - merge threads when vivifying ghosts
43         # 12 - change YYYYMMDD value column to numeric
44         # 13 - fix threading for empty References/In-Reply-To
45         #      (commit 83425ef12e4b65cdcecd11ddcb38175d4a91d5a0)
46         # 14 - fix ghost root vivification
47         # 15 - see public-inbox-v2-format(5)
48         #      further bumps likely unnecessary, we'll suggest in-place
49         #      "--reindex" use for further fixes and tweaks:
50         #
51         #      public-inbox v1.5.0 adds (still SCHEMA_VERSION=15):
52         #      * "lid:" and "l:" for List-Id searches
53         #
54         #      v1.6.0 adds BYTES, UID and THREADID values
55         SCHEMA_VERSION => 15,
56 };
57
58 use PublicInbox::Smsg;
59 use PublicInbox::Over;
60 our $QP_FLAGS;
61 our %X = map { $_ => 0 } qw(BoolWeight Database Enquire QueryParser Stem Query);
62 our $Xap; # 'Search::Xapian' or 'Xapian'
63 our $NVRP; # '$Xap::'.('NumberValueRangeProcessor' or 'NumberRangeProcessor')
64
65 # ENQ_DESCENDING and ENQ_ASCENDING weren't in SWIG Xapian.pm prior to 1.4.16,
66 # let's hope the ABI is stable
67 our $ENQ_DESCENDING = 0;
68 our $ENQ_ASCENDING = 1;
69
70 sub load_xapian () {
71         return 1 if defined $Xap;
72         # n.b. PI_XAPIAN is intended for development use only.  We still
73         # favor Search::Xapian since that's what's available in current
74         # Debian stable (10.x) and derived distros.
75         for my $x (($ENV{PI_XAPIAN} // 'Search::Xapian'), 'Xapian') {
76                 eval "require $x";
77                 next if $@;
78
79                 $x->import(qw(:standard));
80                 $Xap = $x;
81
82                 # `version_string' was added in Xapian 1.1
83                 my $xver = eval('v'.eval($x.'::version_string()')) //
84                                 eval('v'.eval($x.'::xapian_version_string()'));
85
86                 # NumberRangeProcessor was added in Xapian 1.3.6,
87                 # NumberValueRangeProcessor was removed for 1.5.0+,
88                 # favor the older /Value/ variant since that's what our
89                 # (currently) preferred Search::Xapian supports
90                 $NVRP = $x.'::'.($x eq 'Xapian' && $xver ge v1.5 ?
91                         'NumberRangeProcessor' : 'NumberValueRangeProcessor');
92                 $X{$_} = $Xap.'::'.$_ for (keys %X);
93
94                 *sortable_serialise = $x.'::sortable_serialise';
95                 *sortable_unserialise = $x.'::sortable_unserialise';
96                 # n.b. FLAG_PURE_NOT is expensive not suitable for a public
97                 # website as it could become a denial-of-service vector
98                 # FLAG_PHRASE also seems to cause performance problems chert
99                 # (and probably earlier Xapian DBs).  glass seems fine...
100                 # TODO: make this an option, maybe?
101                 # or make indexlevel=medium as default
102                 $QP_FLAGS = FLAG_PHRASE() | FLAG_BOOLEAN() | FLAG_LOVEHATE() |
103                                 FLAG_WILDCARD();
104                 return 1;
105         }
106         undef;
107 }
108
109 # This is English-only, everything else is non-standard and may be confused as
110 # a prefix common in patch emails
111 our $LANG = 'english';
112
113 # note: the non-X term prefix allocations are shared with
114 # Xapian omega, see xapian-applications/omega/docs/termprefixes.rst
115 my %bool_pfx_external = (
116         mid => 'Q', # Message-ID (full/exact), this is mostly uniQue
117         lid => 'G', # newsGroup (or similar entity), just inside <>
118         dfpre => 'XDFPRE',
119         dfpost => 'XDFPOST',
120         dfblob => 'XDFPRE XDFPOST',
121 );
122
123 my $non_quoted_body = 'XNQ XDFN XDFA XDFB XDFHH XDFCTX XDFPRE XDFPOST';
124 my %prob_prefix = (
125         # for mairix compatibility
126         s => 'S',
127         m => 'XM', # 'mid:' (bool) is exact, 'm:' (prob) can do partial
128         l => 'XL', # 'lid:' (bool) is exact, 'l:' (prob) can do partial
129         f => 'A',
130         t => 'XTO',
131         tc => 'XTO XCC',
132         c => 'XCC',
133         tcf => 'XTO XCC A',
134         a => 'XTO XCC A',
135         b => $non_quoted_body . ' XQUOT',
136         bs => $non_quoted_body . ' XQUOT S',
137         n => 'XFN',
138
139         q => 'XQUOT',
140         nq => $non_quoted_body,
141         dfn => 'XDFN',
142         dfa => 'XDFA',
143         dfb => 'XDFB',
144         dfhh => 'XDFHH',
145         dfctx => 'XDFCTX',
146
147         # default:
148         '' => 'XM S A XQUOT XFN ' . $non_quoted_body,
149 );
150
151 # not documenting m: and mid: for now, the using the URLs works w/o Xapian
152 # not documenting lid: for now, either, it is probably redundant with l:,
153 # especially since we don't offer boolean searches for To/Cc/From
154 # headers, either
155 our @HELP = (
156         's:' => 'match within Subject  e.g. s:"a quick brown fox"',
157         'd:' => <<EOF,
158 match date-time range, git "approxidate" formats supported
159 Open-ended ranges such as `d:last.week..' and
160 `d:..2.days.ago' are supported
161 EOF
162         'b:' => 'match within message body, including text attachments',
163         'nq:' => 'match non-quoted text within message body',
164         'q:' => 'match quoted text within message body',
165         'n:' => 'match filename of attachment(s)',
166         't:' => 'match within the To header',
167         'c:' => 'match within the Cc header',
168         'f:' => 'match within the From header',
169         'a:' => 'match within the To, Cc, and From headers',
170         'tc:' => 'match within the To and Cc headers',
171         'l:' => 'match contents of the List-Id header',
172         'bs:' => 'match within the Subject and body',
173         'dfn:' => 'match filename from diff',
174         'dfa:' => 'match diff removed (-) lines',
175         'dfb:' => 'match diff added (+) lines',
176         'dfhh:' => 'match diff hunk header context (usually a function name)',
177         'dfctx:' => 'match diff context lines',
178         'dfpre:' => 'match pre-image git blob ID',
179         'dfpost:' => 'match post-image git blob ID',
180         'dfblob:' => 'match either pre or post-image git blob ID',
181         'rt:' => <<EOF,
182 match received time, like `d:' if sender's clock was correct
183 EOF
184 );
185 chomp @HELP;
186
187 sub xdir ($;$) {
188         my ($self, $rdonly) = @_;
189         if ($rdonly || !defined($self->{shard})) {
190                 $self->{xpfx};
191         } else { # v2 + extindex only:
192                 "$self->{xpfx}/$self->{shard}";
193         }
194 }
195
196 # returns all shards as separate Xapian::Database objects w/o combining
197 sub xdb_shards_flat ($) {
198         my ($self) = @_;
199         my $xpfx = $self->{xpfx};
200         my (@xdb, $slow_phrase);
201         load_xapian();
202         $self->{qp_flags} //= $QP_FLAGS;
203         if ($xpfx =~ m!/xapian[0-9]+\z!) {
204                 @xdb = ($X{Database}->new($xpfx));
205                 $self->{qp_flags} |= FLAG_PHRASE() if !-f "$xpfx/iamchert";
206         } else {
207                 opendir(my $dh, $xpfx) or return (); # not initialized yet
208                 # We need numeric sorting so shard[0] is first for reading
209                 # Xapian metadata, if needed
210                 my $last = max(grep(/\A[0-9]+\z/, readdir($dh))) // return ();
211                 for (0..$last) {
212                         my $shard_dir = "$self->{xpfx}/$_";
213                         push @xdb, $X{Database}->new($shard_dir);
214                         $slow_phrase ||= -f "$shard_dir/iamchert";
215                 }
216                 $self->{qp_flags} |= FLAG_PHRASE() if !$slow_phrase;
217         }
218         @xdb;
219 }
220
221 # v2 Xapian docids don't conflict, so they're identical to
222 # NNTP article numbers and IMAP UIDs.
223 # https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
224 sub mdocid {
225         my ($nshard, $mitem) = @_;
226         my $docid = $mitem->get_docid;
227         int(($docid - 1) / $nshard) + 1;
228 }
229
230 sub mset_to_artnums {
231         my ($self, $mset) = @_;
232         my $nshard = $self->{nshard};
233         [ map { mdocid($nshard, $_) } $mset->items ];
234 }
235
236 sub xdb ($) {
237         my ($self) = @_;
238         $self->{xdb} // do {
239                 my @xdb = $self->xdb_shards_flat or return;
240                 $self->{nshard} = scalar(@xdb);
241                 my $xdb = shift @xdb;
242                 $xdb->add_database($_) for @xdb;
243                 $self->{xdb} = $xdb;
244         };
245 }
246
247 sub new {
248         my ($class, $ibx) = @_;
249         ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
250         my $xap = $ibx->version > 1 ? 'xap' : 'public-inbox/xapian';
251         my $xpfx = "$ibx->{inboxdir}/$xap".SCHEMA_VERSION;
252         my $self = bless { xpfx => $xpfx }, $class;
253         $self->{altid} = $ibx->{altid} if defined($ibx->{altid});
254         $self;
255 }
256
257 sub reopen {
258         my ($self) = @_;
259         if (my $xdb = $self->{xdb}) {
260                 $xdb->reopen;
261         }
262         $self; # make chaining easier
263 }
264
265 # Convert git "approxidate" ranges to something usable with our
266 # Xapian indices.  At the moment, Xapian only offers a C++-only API
267 # and neither the SWIG nor XS bindings allow us to use custom code
268 # to parse dates (and libgit2 doesn't expose git__date_parse, either,
269 # so we're running git-rev-parse(1)).
270 # This replaces things we need to send to $git->date_parse with
271 # "\0".$strftime_format.['+'|$idx]."\0" placeholders
272 sub date_parse_prepare {
273         my ($to_parse, $pfx, $range) = @_;
274         # are we inside a parenthesized statement?
275         my $end = $range =~ s/([\)\s]*)\z// ? $1 : '';
276         my @r = split(/\.\./, $range, 2);
277
278         # expand "d:20101002" => "d:20101002..20101003" and like
279         # n.b. git doesn't do YYYYMMDD w/o '-', it needs YYYY-MM-DD
280         # We upgrade "d:" to "dt:" to iff using approxidate
281         if ($pfx eq 'd') {
282                 my $fmt = "\0%Y%m%d";
283                 if (!defined($r[1])) {
284                         if ($r[0] =~ /\A([0-9]{4})([0-9]{2})([0-9]{2})\z/) {
285                                 push @$to_parse, "$1-$2-$3";
286                                 # we could've handled as-is, but we need
287                                 # to parse anyways for "d+" below
288                         } else {
289                                 push @$to_parse, $r[0];
290                                 if ($r[0] !~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/) {
291                                         $pfx = 'dt';
292                                         $fmt = "\0%Y%m%d%H%M%S";
293                                 }
294                         }
295                         $r[0] = "$fmt+$#$to_parse\0";
296                         $r[1] = "$fmt+\0";
297                 } else {
298                         for my $x (@r) {
299                                 next if $x eq '' || $x =~ /\A[0-9]{8}\z/;
300                                 push @$to_parse, $x;
301                                 if ($x !~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/) {
302                                         $pfx = 'dt';
303                                 }
304                                 $x = "$fmt$#$to_parse\0";
305                         }
306                         if ($pfx eq 'dt') {
307                                 for (@r) {
308                                         s/\0%Y%m%d/\0%Y%m%d%H%M%S/;
309                                         s/\A([0-9]{8})\z/${1}000000/;
310                                 }
311                         }
312                 }
313         } elsif ($pfx eq 'dt') {
314                 if (!defined($r[1])) { # git needs gaps and not /\d{14}/
315                         if ($r[0] =~ /\A([0-9]{4})([0-9]{2})([0-9]{2})
316                                         ([0-9]{2})([0-9]{2})([0-9]{2})\z/x) {
317                                 push @$to_parse, "$1-$2-$3 $4:$5:$6";
318                         } else {
319                                 push @$to_parse, $r[0];
320                         }
321                         $r[0] = "\0%Y%m%d%H%M%S$#$to_parse\0";
322                         $r[1] = "\0%Y%m%d%H%M%S+\0";
323                 } else {
324                         for my $x (@r) {
325                                 next if $x eq '' || $x =~ /\A[0-9]{14}\z/;
326                                 push @$to_parse, $x;
327                                 $x = "\0%Y%m%d%H%M%S$#$to_parse\0";
328                         }
329                 }
330         } else { # "rt", let git interpret "YYYY", deal with Y10K later :P
331                 for my $x (@r) {
332                         next if $x eq '' || $x =~ /\A[0-9]{5,}\z/;
333                         push @$to_parse, $x;
334                         $x = "\0%s$#$to_parse\0";
335                 }
336                 $r[1] //= "\0%s+\0"; # add 1 day
337         }
338         "$pfx:".join('..', @r).$end;
339 }
340
341 sub date_parse_finalize {
342         my ($git, $to_parse) = @_;
343         # git-rev-parse can handle any number of args up to system
344         # limits (around (4096*32) bytes on Linux).
345         my @r = $git->date_parse(@$to_parse);
346         # n.b. git respects TZ, times stored in SQLite/Xapian are always UTC,
347         # and gmtime doesn't seem to do the right thing when TZ!=UTC
348         my ($i, $t);
349         $_[2] =~ s/\0(%[%YmdHMSs]+)([0-9\+]+)\0/
350                 $t = $2 eq '+' ? ($r[$i]+86400) : $r[$i=$2+0];
351                 $1 eq '%s' ? $t : strftime($1, gmtime($t))/sge;
352 }
353
354 # n.b. argv never has NUL, though we'll need to filter it out
355 # if this $argv isn't from a command execution
356 sub query_argv_to_string {
357         my (undef, $git, $argv) = @_;
358         my $to_parse;
359         my $tmp = join(' ', map {;
360                 if (s!\b(d|rt|dt):(\S+)\z!date_parse_prepare(
361                                                 $to_parse //= [], $1, $2)!sge) {
362                         $_;
363                 } elsif (/\s/) {
364                         s/(.*?)\b(\w+:)// ? qq{$1$2"$_"} : qq{"$_"};
365                 } else {
366                         $_
367                 }
368         } @$argv);
369         date_parse_finalize($git, $to_parse, $tmp) if $to_parse;
370         $tmp
371 }
372
373 # this is for the WWW "q=" query parameter and "lei q --stdin"
374 # it can't do d:"5 days ago", but it will do d:5.days.ago
375 sub query_approxidate {
376         my (undef, $git) = @_; # $_[2] = $query_string (modified in-place)
377         my $DQ = qq<"\x{201c}\x{201d}>; # Xapian can use curly quotes
378         $_[2] =~ tr/\x00/ /; # Xapian doesn't do NUL, we use it as a placeholder
379         my ($terms, $phrase, $to_parse);
380         $_[2] =~ s{([^$DQ]*)([$DQ][^$DQ]*[$DQ])?}{
381                 ($terms, $phrase) = ($1, $2);
382                 $terms =~ s!\b(d|rt|dt):(\S+)!
383                         date_parse_prepare($to_parse //= [], $1, $2)!sge;
384                 $terms.($phrase // '');
385                 }sge;
386         date_parse_finalize($git, $to_parse, $_[2]) if $to_parse;
387 }
388
389 # read-only
390 sub mset {
391         my ($self, $query_string, $opts) = @_;
392         $opts ||= {};
393         my $qp = $self->{qp} //= $self->qparse_new;
394         my $query = $qp->parse_query($query_string, $self->{qp_flags});
395         _do_enquire($self, $query, $opts);
396 }
397
398 sub retry_reopen {
399         my ($self, $cb, @arg) = @_;
400         for my $i (1..10) {
401                 if (wantarray) {
402                         my @ret = eval { $cb->($self, @arg) };
403                         return @ret unless $@;
404                 } else {
405                         my $ret = eval { $cb->($self, @arg) };
406                         return $ret unless $@;
407                 }
408                 # Exception: The revision being read has been discarded -
409                 # you should call Xapian::Database::reopen()
410                 if (ref($@) =~ /\bDatabaseModifiedError\b/) {
411                         reopen($self);
412                 } else {
413                         # let caller decide how to spew, because ExtMsg queries
414                         # get wonky and trigger:
415                         # "something terrible happened at .../Xapian/Enquire.pm"
416                         Carp::croak($@);
417                 }
418         }
419         Carp::croak("Too many Xapian database modifications in progress\n");
420 }
421
422 sub _do_enquire {
423         my ($self, $query, $opts) = @_;
424         retry_reopen($self, \&_enquire_once, $query, $opts);
425 }
426
427 # returns true if all docs have the THREADID value
428 sub has_threadid ($) {
429         my ($self) = @_;
430         (xdb($self)->get_metadata('has_threadid') // '') eq '1';
431 }
432
433 sub _enquire_once { # retry_reopen callback
434         my ($self, $query, $opts) = @_;
435         my $xdb = xdb($self);
436         if (defined(my $eidx_key = $opts->{eidx_key})) {
437                 $query = $X{Query}->new(OP_FILTER(), $query, 'O'.$eidx_key);
438         }
439         if (defined(my $uid_range = $opts->{uid_range})) {
440                 my $range = $X{Query}->new(OP_VALUE_RANGE(), UID,
441                                         sortable_serialise($uid_range->[0]),
442                                         sortable_serialise($uid_range->[1]));
443                 $query = $X{Query}->new(OP_FILTER(), $query, $range);
444         }
445         my $enquire = $X{Enquire}->new($xdb);
446         $enquire->set_query($query);
447         $opts ||= {};
448         my $rel = $opts->{relevance} // 0;
449         if ($rel == -2) { # ORDER BY docid/UID (highest first)
450                 $enquire->set_weighting_scheme($X{BoolWeight}->new);
451                 $enquire->set_docid_order($ENQ_DESCENDING);
452         } elsif ($rel == -1) { # ORDER BY docid/UID (lowest first)
453                 $enquire->set_weighting_scheme($X{BoolWeight}->new);
454                 $enquire->set_docid_order($ENQ_ASCENDING);
455         } elsif ($rel == 0) {
456                 $enquire->set_sort_by_value_then_relevance(TS, !$opts->{asc});
457         } else { # rel > 0
458                 $enquire->set_sort_by_relevance_then_value(TS, !$opts->{asc});
459         }
460
461         # `mairix -t / --threads' or JMAP collapseThreads
462         if ($opts->{threads} && has_threadid($self)) {
463                 $enquire->set_collapse_key(THREADID);
464         }
465         $enquire->get_mset($opts->{offset} || 0, $opts->{limit} || 50);
466 }
467
468 sub mset_to_smsg {
469         my ($self, $ibx, $mset) = @_;
470         my $nshard = $self->{nshard};
471         my $i = 0;
472         my %order = map { mdocid($nshard, $_) => ++$i } $mset->items;
473         my @msgs = sort {
474                 $order{$a->{num}} <=> $order{$b->{num}}
475         } @{$ibx->over->get_all(keys %order)};
476         wantarray ? ($mset->get_matches_estimated, \@msgs) : \@msgs;
477 }
478
479 # read-write
480 sub stemmer { $X{Stem}->new($LANG) }
481
482 # read-only
483 sub qparse_new {
484         my ($self) = @_;
485
486         my $xdb = xdb($self);
487         my $qp = $X{QueryParser}->new;
488         $qp->set_default_op(OP_AND());
489         $qp->set_database($xdb);
490         $qp->set_stemmer(stemmer($self));
491         $qp->set_stemming_strategy(STEM_SOME());
492         my $cb = $qp->can('set_max_wildcard_expansion') //
493                 $qp->can('set_max_expansion'); # Xapian 1.5.0+
494         $cb->($qp, 100);
495         $cb = $qp->can('add_valuerangeprocessor') //
496                 $qp->can('add_rangeprocessor'); # Xapian 1.5.0+
497         $cb->($qp, $NVRP->new(YYYYMMDD, 'd:'));
498         $cb->($qp, $NVRP->new(DT, 'dt:'));
499
500         # for IMAP, undocumented for WWW and may be split off go away
501         $cb->($qp, $NVRP->new(BYTES, 'z:'));
502         $cb->($qp, $NVRP->new(TS, 'rt:'));
503         $cb->($qp, $NVRP->new(UID, 'uid:'));
504
505         while (my ($name, $prefix) = each %bool_pfx_external) {
506                 $qp->add_boolean_prefix($name, $_) foreach split(/ /, $prefix);
507         }
508
509         # we do not actually create AltId objects,
510         # just parse the spec to avoid the extra DB handles for now.
511         if (my $altid = $self->{altid}) {
512                 my $user_pfx = $self->{-user_pfx} = [];
513                 for (@$altid) {
514                         # $_ = 'serial:gmane:/path/to/gmane.msgmap.sqlite3'
515                         # note: Xapian supports multibyte UTF-8, /^[0-9]+$/,
516                         # and '_' with prefixes matching \w+
517                         /\Aserial:(\w+):/ or next;
518                         my $pfx = $1;
519                         push @$user_pfx, "$pfx:", <<EOF;
520 alternate serial number  e.g. $pfx:12345 (boolean)
521 EOF
522                         # gmane => XGMANE
523                         $qp->add_boolean_prefix($pfx, 'X'.uc($pfx));
524                 }
525                 chomp @$user_pfx;
526         }
527
528         while (my ($name, $prefix) = each %prob_prefix) {
529                 $qp->add_prefix($name, $_) foreach split(/ /, $prefix);
530         }
531         $qp;
532 }
533
534 sub help {
535         my ($self) = @_;
536         $self->{qp} //= $self->qparse_new; # parse altids
537         my @ret = @HELP;
538         if (my $user_pfx = $self->{-user_pfx}) {
539                 push @ret, @$user_pfx;
540         }
541         \@ret;
542 }
543
544 sub int_val ($$) {
545         my ($doc, $col) = @_;
546         my $val = $doc->get_value($col) or return; # undefined is '' in Xapian
547         sortable_unserialise($val) + 0; # PV => IV conversion
548 }
549
550 sub get_pct ($) { # mset item
551         # Capped at "99%" since "100%" takes an extra column in the
552         # thread skeleton view.  <xapian/mset.h> says the value isn't
553         # very meaningful, anyways.
554         my $n = $_[0]->get_percent;
555         $n > 99 ? 99 : $n;
556 }
557
558 sub xap_terms ($$;@) {
559         my ($pfx, $xdb_or_doc, @docid) = @_; # @docid may be empty ()
560         my %ret;
561         my $end = $xdb_or_doc->termlist_end(@docid);
562         my $cur = $xdb_or_doc->termlist_begin(@docid);
563         for (; $cur != $end; $cur++) {
564                 $cur->skip_to($pfx);
565                 last if $cur == $end;
566                 my $tn = $cur->get_termname;
567                 $ret{substr($tn, length($pfx))} = undef if !index($tn, $pfx);
568         }
569         wantarray ? sort(keys(%ret)) : \%ret;
570 }
571
572 # get combined docid from over.num:
573 # (not generic Xapian, only works with our sharding scheme)
574 sub num2docid ($$) {
575         my ($self, $num) = @_;
576         my $nshard = $self->{nshard};
577         ($num - 1) * $nshard + $num % $nshard + 1;
578 }
579
580 1;