+use PublicInbox::Smsg;
+use PublicInbox::Over;
+my $QP_FLAGS;
+our %X = map { $_ => 0 } qw(BoolWeight Database Enquire QueryParser Stem);
+our $Xap; # 'Search::Xapian' or 'Xapian'
+my $NVRP; # '$Xap::'.('NumberValueRangeProcessor' or 'NumberRangeProcessor')
+my $ENQ_ASCENDING;
+
+sub load_xapian () {
+ return 1 if defined $Xap;
+ # n.b. PI_XAPIAN is intended for development use only. We still
+ # favor Search::Xapian since that's what's available in current
+ # Debian stable (10.x) and derived distros.
+ for my $x (($ENV{PI_XAPIAN} // 'Search::Xapian'), 'Xapian') {
+ eval "require $x";
+ next if $@;
+
+ $x->import(qw(:standard));
+ $Xap = $x;
+
+ # `version_string' was added in Xapian 1.1
+ my $xver = eval('v'.eval($x.'::version_string()')) //
+ eval('v'.eval($x.'::xapian_version_string()'));
+
+ # NumberRangeProcessor was added in Xapian 1.3.6,
+ # NumberValueRangeProcessor was removed for 1.5.0+,
+ # favor the older /Value/ variant since that's what our
+ # (currently) preferred Search::Xapian supports
+ $NVRP = $x.'::'.($x eq 'Xapian' && $xver ge v1.5 ?
+ 'NumberRangeProcessor' : 'NumberValueRangeProcessor');
+ $X{$_} = $Xap.'::'.$_ for (keys %X);
+
+ # ENQ_ASCENDING doesn't seem exported by SWIG Xapian.pm,
+ # so lets hope this part of the ABI is stable because it's
+ # just an integer:
+ $ENQ_ASCENDING = $x eq 'Xapian' ?
+ 1 : Search::Xapian::ENQ_ASCENDING();
+
+ # for Smsg:
+ *PublicInbox::Smsg::sortable_unserialise =
+ $Xap.'::sortable_unserialise';
+ # n.b. FLAG_PURE_NOT is expensive not suitable for a public
+ # website as it could become a denial-of-service vector
+ # FLAG_PHRASE also seems to cause performance problems chert
+ # (and probably earlier Xapian DBs). glass seems fine...
+ # TODO: make this an option, maybe?
+ # or make indexlevel=medium as default
+ $QP_FLAGS = FLAG_PHRASE() | FLAG_BOOLEAN() | FLAG_LOVEHATE() |
+ FLAG_WILDCARD();
+ return 1;
+ }
+ undef;
+}
+
+# This is English-only, everything else is non-standard and may be confused as
+# a prefix common in patch emails
+our $LANG = 'english';
+
+# note: the non-X term prefix allocations are shared with
+# Xapian omega, see xapian-applications/omega/docs/termprefixes.rst