5 .. contents:: Table of contents
10 The ``Xapian::RangeProcessor`` class was introduced in Xapian 1.3.6, and
11 provides a powerful and flexible way to parse range queries in the users'
12 query string. It's a replacement for the older ``Xapian::ValueRangeProcessor``
13 class (which dates back to Xapian 1.0.0).
15 This document describes the ``Xapian::RangeProcessor`` class and
16 its standard subclasses, how to create your own subclasses, and how
17 these classes are used with ``Xapian::QueryParser``.
19 ``Xapian::RangeProcessor`` itself supports parsing string ranges, optionally
20 only recognising ranges with a specified prefix or suffix. There are
21 standard subclasses supporting ranges of dates (``Xapian::DateRangeProcessor``)
22 and of numbers (``Xapian::NumberRangeProcessor``). User subclasses
23 can support custom range types.
25 ``Xapian::QueryParser`` maintains a list of ``Xapian::RangeProcessor`` objects
26 which it tries in order for each range specified in the query until one accepts
27 it, or all have been tried (in which case an error is reported).
29 So you can support multiple filters distinguished by a prefix or suffix. For
30 example, if you want to support range filters on price and weight, you can do
33 Xapian::QueryParser qp;
34 Xapian::NumberRangeProcessor price_proc(0, "$");
35 Xapian::NumberRangeProcessor weight_proc(1, "kg", Xapian::RP_SUFFIX);
36 qp.add_rangeprocessor(&price_proc);
37 qp.add_rangeprocessor(&weight_proc);
39 Then the user can enter queries like::
41 laptop $300..800 ..1.5kg
43 A common way to use this feature is with a prefix string which is a "field
44 name" followed by a colon, for example::
46 created:1/1/1999..1/1/2003
48 Each ``Xapian::RangeProcessor`` is passed the start and end of the
49 range. If it doesn't understand the range, it should
50 ``Xapian::Query(Xapian::Query::OP_INVALID)``. If it does understand the range,
51 it should return a query object matching the range (which will often use query
52 operator ``Xapian::Query::OP_VALUE_RANGE`` but can be any query).
54 In Xapian 1.2.1 and later, ``Xapian::QueryParser`` supports open-ended
55 ranges - if the start of the range is empty, that means any value less than
56 the end, and similarly if the end is empty, that means any value greater
57 than the start. The start and end can't both be empty.
62 This understands any range passed which has the specified prefix or suffix.
63 If no prefix or suffix is specified it will match any range (so it's not
64 useful to specify further ``RangeProcessor`` objects after such an object
67 For example, suppose you have stored author names in value number 4, and want
68 the user to be able to filter queries by specifying ranges of values such as::
72 To do this, you can use a ``RangeProcessor`` like so::
74 Xapian::QueryParser qp;
75 Xapian::RangeProcessor author_proc(4);
76 qp.add_rangeprocessor(&author_proc);
78 The parsed query will use ``OP_VALUE_RANGE``, so ``query.get_description()``
81 Xapian::Query(mars:(pos=1) FILTER (VALUE_RANGE 4 asimov bradbury)
83 The ``VALUE_RANGE`` subquery will only match documents where value 4 is
84 >= asimov and <= bradbury (using a string comparison).
89 This class allows you to implement date range searches. As well as the value
90 number to search, you can tell it whether to prefer US-style month/day/year
91 or European-style day/month/year (by using the ``Xapian::RP_DATE_PREFER_MDY``
92 flag), and specify the epoch year to use for interpreting 2 digit years (the
93 default is day/month/year with an epoch of 1970). The best choice of settings
94 depends on the expectations of your users. As these settings are only applied
95 at search time, you can also easily offer different versions of your search
96 front-end with different settings if that is useful.
98 For example, if your users are American and the dates present in your database
99 can extend a decade or so into the future, you might use something like this
100 which specifies to prefer US-style dates and that the epoch year is 1930 (so
101 02/01/29 is February 1st 2029 while 02/01/30 is February 1st 1930)::
103 Xapian::QueryParser qp;
104 Xapian::DateRangeProcessor date_proc(0, Xapian::RP_DATE_PREFER_MDY, 1930);
105 qp.add_rangeprocessor(&date_proc);
107 The dates are converted to the format YYYYMMDD, so the values you index also
108 need to also be in this format - for example, if ``doc_time`` is a ``time_t``::
111 if (strftime(buf, sizeof(buf), "%Y%m%d", gmtime(&doc_time))) {
112 doc.add_value(0, buf);
118 This class allows you to implement numeric range searches. The numbers used
119 may be any number which is representable as a double, but requires that the
120 stored values which the range is being applied have been converted to strings
121 at index time using the ``Xapian::sortable_serialise()`` method::
123 Xapian::Document doc;
124 doc.add_value(0, Xapian::sortable_serialise(price));
126 This method produces strings which will sort in numeric order, so you can use
127 it if you want to be able to sort based on the value in numeric order, too.
132 You can easily create your own subclasses of ``Xapian::RangeProcessor``.
133 Your subclass needs to implement a method
134 ``Xapian::Query operator()(const std::string &begin, const std::string &end)``
135 so for example you could implement a better version of the author range
136 described above which only matches ranges with a prefix (e.g.
137 ``author:asimov..bradbury``) and lower-cases the names::
139 struct AuthorRangeProcessor : public Xapian::RangeProcessor {
140 AuthorRangeProcessor() : RangeProcessor(4, "author:") { }
142 Xapian::valueno operator()(const std::string& b, const std::string& e) {
143 // Let the base class do the prefix check.
144 return RangeProcessor::operator()(Xapian::Unicode::tolower(b),
145 Xapian::Unicode::tolower(e));
149 If you want to support open-ended ranges, you need to handle begin or end
150 being empty suitably. ``Xapian::QueryParser`` won't call your subclass
151 with *both* begin and end being empty.
153 Using Several RangeProcessors
154 =============================
156 If you want to allow the user to specify different types of ranges, you can
157 specify multiple ``RangeProcessor`` objects to use. Just add them in
158 the order you want them to be checked::
160 Xapian::QueryParser qp;
161 AuthorRangeProcessor author_proc();
162 qp.add_rangeprocessor(&author_proc);
163 Xapian::DateRangeProcessor date_proc(0, 0, 1930);
164 qp.add_rangeprocessor(&date_proc);
166 And then you can parse queries such as
167 ``mars author:Asimov..Bradbury 01/01/1960..31/12/1969`` successfully.