File: overview.html

package info (click to toggle)
xapian-core 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 17,528 kB
  • ctags: 13,287
  • sloc: cpp: 99,474; sh: 10,626; ansic: 7,551; perl: 758; makefile: 611; python: 40
file content (1007 lines) | stat: -rw-r--r-- 39,429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Xapian: Overview</TITLE>
</HEAD>
<BODY BGCOLOR="white" TEXT="black">

<H1>Overview</H1>

<P>
This document provides an introduction to the native C++ Xapian API.
This API provides programmers with the ability to index and search through
(potentially very large) bodies of data using probabilistic methods.
</P>

<P>
<EM>Note:</EM>
The portion of the API currently documented here covers only the part
of Xapian concerned with searching through existing databases, not that
concerned with creating them.
</P>

<P>
This document assumes you already have Xapian installed, so if you
haven't, it is a good idea to read <A HREF="install.html">Installing Xapian</A> first.
</P>

<P>
You may also wish to read
the <A HREF="quickstart.html">QuickStart</A> reference, for some simple
worked examples of Xapian usage, and the
<A HREF="intro_ir.html">Introduction to Information Retrieval</A> for a
background into the Information Retrieval theories behind Xapian.
</P>

<P>
This document does not detail the exact calling conventions (parameters
passed, return value, exceptions thrown, etc...) for each method in the API.
For such documentation, you should refer to the automatically extracted
documentation, which is generated from detailed comments in the source code,
and should thus remain up-to-date and accurate.  This documentation is
generated using the
<EM><A HREF="http://www.doxygen.org/">Doxygen</A></EM>
application.  To save you having to generate this documentation yourself,
we include the <A HREF="apidoc/html/index.html">built version</A>
in our distributions, and also keep the
<A HREF="http://xapian.org/docs/apidoc/html/index.html">latest version</A> on our website.
</P>

<H2>Design Principles</H2>

<P>
API classes are either very lightweight or a wrapper around a reference counted
pointer (this style of class design is sometimes known as PIMPL for "Private
IMPLementation").  In either case copying is a cheap operation as classes
are at most a few words of memory.
</P>

<P>
API objects keep a reference to other objects they rely on so the user
doesn't need to worry about whether an object is still valid or not.
</P>

<P>
Where appropriate, API classes can be used as containers and iterators just
like those in the C++ STL.
</P>

<H2>Errors and exceptions</H2>

<P>
Error reporting is often relegated to the back of manuals such as this.
However, it is extremely important to understand the errors which may be
caused by the operations which you are trying to perform.
</P>

<P>
This becomes particularly relevant when using a large system, with such
possibilities as databases which are being updated while you search
through them, and distributed enquiry systems.
</P>

<P>
Errors in Xapian are all reported by means of exceptions.  All exceptions
thrown by Xapian will be subclasses of
<A HREF="apidoc/html/classXapian_1_1Error.html"><CODE>Xapian::Error</CODE></A>.  Note that
<CODE>Xapian::Error</CODE> is an abstract class; thus you must catch exceptions
by reference rather than by value.
</P>

<P>
There are two flavours of error, derived from <CODE>Xapian::Error</CODE>:
<UL><LI>
<A HREF="apidoc/html/classXapian_1_1LogicError.html"><CODE>Xapian::LogicError</CODE></A>
- for error conditions due to programming errors, such as a misuse of the
API.  A finished application should not receive these errors (though it
would still be sensible to catch them).
</LI><LI>
<A HREF="apidoc/html/classXapian_1_1RuntimeError.html"><CODE>Xapian::RuntimeError</CODE></A>
- for error conditions due to run time problems, such as failure to open
a database.  You must always be ready to cope with such errors.
</LI></UL>
</P>

<P>
Each of these flavours is further subdivided, such that any particular
error condition can be trapped by catching the appropriate exception.
If desired, a human readable explanation of the error can be retrieved
by calling
<A HREF="apidoc/html/classXapian_1_1Error.html"><CODE>Xapian::Error::get_msg()</CODE></A>.
</P>

<P>
In addition, standard system errors may occur: these will be reported by
throwing appropriate exceptions.  Most notably, if the system runs out
of memory, a <CODE>std::bad_alloc()</CODE> exception will be thrown.
</P>

<H2>Terminology</H2>
<H3>Databases</H3>
<P>
These may also occasionally be called <EM>Indexes</EM>.  In Xapian (as
opposed to a database package) a database consists of little more than
indexed documents: this reflects the purpose of Xapian as an information
retrieval system, rather than an information storage system.
</P>
<P>
The exact contents of a database depend on the type (see
&quot;<A HREF="#database_types">Database Types</A>&quot; for more details
of the database types currently provided).
</P>

<H3>Queries</H3>
<P>
The information to be searched for is specified by a <EM>Query</EM>.  In
Xapian, queries are made up of a structured boolean tree, upon which
probabilistic weightings are imposed: when the search is performed, the
documents returned are filtered according to the boolean structure, and
weighted (and sorted) according to the probabilistic model of information
retrieval.
</P>

<H2>Memory handling</H2>
<P>
The user of Xapian does not usually need to worry about how Xapian performs
its memory allocation: Xapian objects can all be created and deleted as any
other C++ objects.  The convention is that whoever creates an object
is ultimately responsible for deleting it.  This becomes relevant when
passing a pointer to data to Xapian: Xapian will not assume that such
pointers remain valid across separate API calls, and it will be the
callers responsibility to delete the object pointed to, as and when
required.
</P>

<H2>The Xapian::Enquire class</H2>

<P>
The <A HREF="apidoc/html/classXapian_1_1Enquire.html"><CODE>Xapian::Enquire</CODE></A> class
is central to all searching operations.  It provides an interface for
<UL><LI>
Specifying the database, or databases, to search across.
</LI><LI>
Specifying a query to perform.
</LI><LI>
Specifying a set of documents which a user considers relevant.
</LI><LI>
Given the supplied information, returning a ranked set of documents for
the user.
</LI><LI>
Given the supplied information, suggesting a ranked set of terms to add to the
query.
</LI><LI>
Returning information about the documents which matched, such as their
associated data, and which terms from the query were found within them.
</LI></UL>
</P>
<P>
A typical enquiry session will consist of most of these operations, in
various orders.  The Xapian::Enquire class presents as few restrictions as
possible on the order in which operations should be performed.  Although
you must set the query before any operation which uses it, you can call
any of the other methods in any order.
</P>
<P>
Many operations performed by the Xapian::Enquire class are performed lazily (ie,
just before their results are needed).  This need not concern the user
except to note that, as a result, errors may not be reported as soon as
would otherwise be expected.
</P>

<H2>Specifying a database</H2>

<P>
When creating a Xapian::Enquire object, a database to search must be specified.
Databases are specified by creating a <A
HREF="apidoc/html/classXapian_1_1Database.html"><CODE>Xapian::Database</CODE> object</A>.
Generally, you can just construct the object, passing the pathname to the
database.  Xapian looks at the path and autodetects the database type.
</P>
<P>
In some cases (with the Remote backend, or if you want more control) you
need to use a factory function such as <CODE>Xapian::Flint::open()</CODE>
- each backend type has one or more.  The parameters the function
takes depend on the backend type, and whether we are creating a read-only
or a writable database.
</P>
</P>
<P>
You can also create a "stub database" file which list one or more databases.
These files are recognised by the autodetection in the Database constructor
(if the pathname is file rather than a directory, it's treated as a stub
database file) or you can open them explicitly using Xapian::Auto::open_stub().
The stub database format specifies one database per line.  For example:

<BLOCKQUOTE><CODE>
remote localhost:23876<br>
flint /var/spool/xapian/webindex<br>
</CODE></BLOCKQUOTE>

<A NAME="database_types"><H3>Database types</H3></A>
The current types understood by Xapian are:
</P>
<TABLE>
<TR><TD VALIGN="top"><B>auto</B></TD><TD>
<P>
This isn't an actual database format, but rather auto-detection of one of the
disk based backends ("flint" or "stub") from a single specified
file or directory path.
</P>
</TD></TR>
<TR><TD VALIGN="top"><B>brass</B></TD><TD>
<P>
Brass is the current development backend, and it is intended to be the default
backend in Xapian 1.4.x.
</P>
</TD></TR>
<TR><TD VALIGN="top"><B>chert</B></TD><TD>
<P>
Chert is the default backend in Xapian 1.2.x. It supports incremental
modifications, concurrent single-writer and multiple-reader access to
a database.  It's very efficient and highly scalable.
</P>
</TD></TR>
<TR><TD VALIGN="top"><B>flint</B></TD><TD>
<P>
Flint was the default backend in Xapian 1.0.x. It supports incremental
modifications, concurrent single-writer and multiple-reader access to
a database.  It's very efficient and highly scalable.  Flint takes lessons
learned from studying Quartz in action, and is appreciably faster
(both when indexing and searching), more compact, and features an
improved locking mechanism which automatically releases the lock
if a writing process dies.
</P>
<P>
For more information, see the <a href="http://wiki.xapian.org/FlintBackend">Xapian Wiki</a>.
</P>
</TD></TR>
<TR><TD VALIGN="top"><B>quartz</B></TD><TD>
<P>
Quartz was the default backend prior to Xapian 1.0, and has been removed as
of Xapian 1.1.0.  If you want to migrate an existing Quartz database to
Flint, <a
href="admin_notes.html#converting-a-quartz-database-to-a-flint-database`">see
the 'Admin Notes'</a> for a way to do this.
</P>
</TD></TR>
<TR><TD VALIGN="top"><B>inmemory</B></TD><TD>
This type is a database held entirely in memory.
It was originally written for testing purposes only, but may
prove useful for building up temporary small databases.
</TD></TR>
</TABLE>

<H3>Multiple databases</H3>

<P>
Xapian can search across several databases as easily as searching across a
single one.  Simply call
<A HREF="apidoc/html/classXapian_1_1Database.html"><CODE>Xapian::Database::add_database()</CODE></A>
for each database that you wish to search through.
</P>
<P>
You can also set up "pre-canned" listed of databases to search over using
a "stub database" - see above for details.
</P>
<!-- I don't really think this says anything useful...
<P>
Other operations, such as setting the query, may be performed before or after
this call.  It is even possible to perform a query, add a further database,
and then perform the query again to get the results with the extra database
(although this isn't very likely to be useful in practice).
</P>-->

<H2>Specifying a query</H2>

<P>
Xapian implements both boolean and probabilistic searching.
There are two obvious ways in which a pure boolean query can be combined
with a pure probabilistic query:
<UL><LI>
First perform the boolean search to create a subset of the whole document
collection, and then do the probabilistic search on this subset, or
</LI><LI>
Do the probabilistic search, and then filter out the resulting documents
with a boolean query.
</LI></UL>
There is in fact a subtle difference in these two approaches. In the first,
the collection statistics for the probabilistic query will be
determined by the document subset which is obtained by running the boolean
query. In the second, the collection statistics for the probabilistic
query are determined by the whole document collection. These differences
can affect the final result.

</P>
<P>
Suppose for example the boolean query is
being used to retrieve documents in English in a database
containing English and French documents.
A word like
&quot;<EM>grand</EM>&quot;,
exists in both languages (with similar meanings), but is more common in French
than English. In the English subset it could therefore be expected to have a higher
weight than it would get in the joint English and French databases.
</P>

<P>
Xapian takes the second approach simply because this can be implemented very
efficiently.  The first approach is more exact, but inefficient to implement.
</P>

<P>
Rather than implementing this approach as described above and first
performing the probabilistic search and then filtering the results, Xapian
actually performs both tasks
simultaneously.  This allows various optimisations to be performed, such
as giving up on calculating a boolean AND operation when the probabilistic
weights that could result from further documents can have no effect on the
result set.  These optimisations have been found to often give a several-fold
performance increase.  The performance is particularly good for queries
containing many terms.
</P>

<H3>A query for a single term</H3>
<P>
A search query is represented by a
<A HREF="apidoc/html/classXapian_1_1Query.html"><CODE>Xapian::Query</CODE></A>
object.  The simplest useful query is one which searches for a single term
(and several of these can be combined to form more complex queries).
A single term query can be created as follows (where <CODE>term</CODE> is a
<CODE>std::string</CODE> holding the term to be searched for):
</P>
<PRE>
Xapian::Query query(term);
</PRE>
<P>
A term in Xapian is represented simply by a string of binary characters.
Usually, when searching text, these characters will be the word which the
term represents, but during the information retrieval process Xapian
attaches no specific meaning to the term.
</P>
<P>
This constructor actually takes a couple of extra parameters, which may be
used to specify positional and frequency information for terms in the query:
<P>
<PRE>
Xapian::Query(const string &amp; tname_,
        Xapian::termcount wqf_ = 1,
        Xapian::termpos term_pos_ = 0)
</PRE>
<P>
The <CODE>wqf</CODE> (<B>W</B>ithin <B>Q</B>uery <B>F</B>requency) is
a measure of how common a term is in the query.  This isn't useful for
a single term query unless it is going to be combined to form a more
complex query.  In that case, it's particularly useful
when generating a query from an existing document, but may also be used
to increase the "importance" of a term in a query.  Another way to
increase the "importance" of a term is to use <code>OP_SCALE_WEIGHT</code>.
But if the intention is simply to ensure that a particular term is in the query
results, you should use a boolean AND or AND_MAYBE rather than setting a high wqf.
</P>
<P>
The <CODE>term_pos</CODE> represents the position of the term in the query.
Again, this isn't useful for a single term query by itself, but is used for
phrase searching, passage retrieval, and other operations
which require knowledge of the order of terms in the query (such as returning
the set of matching terms in a given document in the same order as they
occur in the query).  If such operations are not required, the default
value of 0 may be used.
</P>
<P>
Note that it may not make much sense to specify a wqf other than 1 when
supplying a term position (unless you are trying to affect the weighting,
as previously described).
</P>
<P>
Note also that the results of <CODE>Xapian::Query(tname, 2)</CODE> and
<CODE>Xapian::Query(Xapian::Query::OP_OR, Xapian::Query(tname), Xapian::Query(tname))</CODE>
are exactly equivalent.
</P>

<H3>Compound queries</H3>
<P>
Compound queries can be built up from single term queries by combining
them a connecting operator. Most operators can operate on either
a single term query or a compound query. You can combine pair-wise
using the following constructor:
</P>
<PRE>
Xapian::Query(Xapian::Query::op op_,
        const Xapian::Query &amp; left,
        const Xapian::Query &amp; right)
</PRE>
<P>
The two most commonly used operators are <CODE>Xapian::Query::OP_AND</CODE> and
<CODE>Xapian::Query::OP_OR</CODE>, which enable us to construct boolean queries made
up from the usual AND and OR operations. But in addition to this, a
probabilistic query in its simplest form, where we have a list of terms
which give rise to weights that need to be added together, is also made up
from a set of terms joined together with <CODE>Xapian::Query::OP_OR</CODE>.
</P>
<P>
The full set of available <CODE>Xapian::Query::op</CODE> operators is:
<TABLE>
<TR><TD VALIGN="top">
Xapian::Query::OP_AND
</TD><TD>
Return documents returned by both subqueries.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_OR
</TD><TD>
Return documents returned by either subquery.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_AND_NOT
</TD><TD>
Return documents returned by the left subquery but not the right subquery.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_FILTER
</TD><TD>
As Xapian::Query::OP_AND, but use only weights from left subquery.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_AND_MAYBE
</TD><TD>
Return documents returned by the left subquery, but adding
document weights from both subqueries.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_XOR
</TD><TD>
Return documents returned by one subquery only.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_NEAR
</TD><TD>
Return documents where the terms are with the specified distance of each other.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_PHRASE
</TD><TD>
Return documents where the terms are with the specified distance of each other
and in the given order.
</TD></TR><TR><TD VALIGN="top">
Xapian::Query::OP_ELITE_SET
</TD><TD>
Select an elite set of terms from the subqueries, and perform
a query with all those terms combined as an OR query.
</TD></TR>
</TABLE>
</P>


<H3>Understanding queries</H3>

<P>
Each term in the query has a weight in each document.  Each document may also
have an additional weight not associated with any of the terms.  By default
the probabilistic weighting scheme <a href="bm25.html">BM25</a>
is used to provide the formulae which
give these weights.
</P>
<P>
A query can be thought of as a tree structure. At each node is
an <CODE>Xapian::Query::op</CODE> operator, and on the left and right branch are two other queries.
At each leaf node is a term, t, transmitting documents and scores, D and
w<sub>D</sub>(t),
up the tree.
</P>
<P>
A Xapian::Query::OP_OR node transmits documents from both branches up the tree, summing the scores
when a document is found in both the left and right branch. For example,

<PRE>
                           docs       1    8    12    16    17    18
                           scores    7.3  4.1   3.2  7.6   3.8   4.7 ...
                             |
                             |
                   Xapian::Query::OP_OR
                         /       \
                        /         \
                       /           \
                      /             \
   docs     1   12   16   17         1   8   16   18
   scores  3.1 3.2  3.1  3.8 ...    4.2 4.1 4.5  4.7 ...
</PRE>

A Xapian::Query::OP_AND node transmits only the documents found on both
branches up the tree, again summing the scores,

<PRE>
                           docs       1   16
                           scores    7.3  7.6  ...
                             |
                             |
                   Xapian::Query::OP_AND
                         /       \
                        /         \
                       /           \
                      /             \
   docs     1   12   16   17         1   8   16   18
   scores  3.1 3.2  3.1  3.8 ...    4.2 4.1 4.5  4.7 ...
</PRE>

A Xapian::Query::OP_AND_NOT node transmits up the tree the documents on the
left branch which are not on the right branch. The scores are taken from the
left branch. For example, again summing the scores,

<PRE>
                           docs       12   17
                           scores    3.2  3.8 ...
                             |
                             |
                 Xapian::Query::OP_AND_NOT
                         /       \
                        /         \
                       /           \
                      /             \
   docs     1   12   16   17         1   8   16   18
   scores  3.1 3.2  3.1  3.8 ...    4.2 4.1 4.5  4.7 ...
</PRE>

A Xapian::Query::OP_AND_MAYBE node transmits the documents up the tree from the
left branch only, but adds in the score from the right branch for documents
which occur on both branches.  For example,

<PRE>
                           docs       1    12   16   17
                           scores    7.3  3.2  7.6  3.8 ...
                             |
                             |
                Xapian::Query::OP_AND_MAYBE
                         /       \
                        /         \
                       /           \
                      /             \
   docs     1   12   16   17         1   8   16   18
   scores  3.1 3.2  3.1  3.8 ...    4.2 4.1 4.5  4.7 ...
</PRE>

Xapian::Query::OP_FILTER is like Xapian::Query::OP_AND, but weights are only
transmitted from the left branch.  For example,

<PRE>
                           docs       1   16
                           scores    3.1  3.1  ...
                             |
                             |
                  Xapian::Query::OP_FILTER
                         /       \
                        /         \
                       /           \
                      /             \
   docs     1   12   16   17         1   8   16   18
   scores  3.1 3.2  3.1  3.8 ...    4.2 4.1 4.5  4.7 ...
</PRE>
Xapian::Query::OP_XOR is like Xapian::Query::OP_OR, but documents on both left
and right branches are not transmitted up the tree. For example,

<PRE>
                           docs       8    12    17    18
                           scores    4.1   3.2  3.8   4.7 ...
                             |
                             |
                      Xapian::Query::OP_XOR
                         /       \
                        /         \
                       /           \
                      /             \
   docs     1   12   16   17         1   8   16   18
   scores  3.1 3.2  3.1  3.8 ...    4.2 4.1 4.5  4.7 ...
</PRE>
</P>
<P>
A query can therefore be thought of as a process for generating an MSet from
the terms at the leaf nodes of the query. Each leaf node gives rise to a
posting list of documents with scores. Each higher level node gives rise to a
similar list, and the root node of the tree contains the final set of documents
with scores (or weights), which are candidates for going into the MSet. The
MSet contains the documents which get the highest weights, and they are held in
the MSet in weight order.
</P>
<P>
It is important to realise that within Xapian the structure of a query is
optimised for best performance, and it undergoes various transformations as the
query progresses. The precise way in which the query is built up is therefore
of little importance to Xapian - for example, you can AND together terms
pair-by-pair, or combine several using AND on a std::vector of terms, and
Xapian will build the same structure internally.
</P>

<H3>Using queries</H3>
<H4>Probabilistic queries </H4>
A plain probabilistic query is created by connecting terms together with
Xapian::Query::OP_OR operators. For example,

<PRE>
    Xapian::Query query("regulation"));
    query = Xapian::Query(Xapian::Query::OP_OR, query, Xapian::Query("import"));
    query = Xapian::Query(Xapian::Query::OP_OR, query, Xapian::Query("export"));
    query = Xapian::Query(Xapian::Query::OP_OR, query, Xapian::Query("canned"));
    query = Xapian::Query(Xapian::Query::OP_OR, query, Xapian::Query("fish"));
</PRE>

This creates a probabilistic query with terms `regulation', `import', `export',
`canned' and `fish'.
<P>
In fact this style of creation is so common that there is the shortcut
construction:

<PRE>
    vector &lt;string&gt; terms;
    terms.push_back("regulation");
    terms.push_back("import");
    terms.push_back("export");
    terms.push_back("canned");
    terms.push_back("fish");

    Xapian::Query query(Xapian::Query::OP_OR, terms.begin(), terms.end());
</PRE>
<H4>Boolean queries</H4>
Suppose now we have this Boolean query,
<PRE>
    ('EEC' - 'France') and ('1989' or '1991' or '1992') and 'Corporate Law'
</PRE>

This could be built up as bquery like this,

<PRE>
    Xapian::Query bquery1(Xapian::Query::OP_AND_NOT, "EEC", "France");

    Xapian::Query bquery2("1989");
    bquery2 = Xapian::Query(Xapian::Query::OP_OR, bquery2, "1991");
    bquery2 = Xapian::Query(Xapian::Query::OP_OR, bquery2, "1992");

    Xapian::Query bquery3("Corporate Law");

    Xapian::Query bquery(Xapian::Query::OP_AND, bquery1, Xapian::Query(Xapian::Query::OP_AND(bquery2, bquery3)));
</PRE>

and this can be attached as a filter to <code>query</code> to run the
probabilistic query with a Boolean filter,

<PRE>
    query = Xapian::Query(Xapian::Query::OP_FILTER, query, bquery);
</PRE>

If you want to run a pure boolean query, then set BoolWeight as the weighting
scheme (by calling Enquire::set_weighting_scheme() with argument BoolWeight()).
<H4>Plus and minus terms </H4>
<P>
A common requirement in search engine functionality is to run a
probabilistic query where some terms are required to index all the
retrieved documents (`+' terms), and others are required to
index none of the retrieved documents (`-' terms). For example,

<PRE>
    regulation import export +canned +fish -japan
</PRE>

the corresponding query can be set up by,

<PRE>
    vector &lt;string&gt; plus_terms;
    vector &lt;string&gt; minus_terms;
    vector &lt;string&gt; normal_terms;

    plus_terms.push_back("canned");
    plus_terms.push_back("fish");

    minus_terms.push_back("japan");

    normal_terms.push_back("regulation");
    normal_terms.push_back("import");
    normal_terms.push_back("export");

    Xapian::Query query(Xapian::Query::OP_AND_MAYBE,
                  Xapian::Query(Xapian::Query::OP_AND, plus_terms.begin(), plus_terms.end());
                  Xapian::Query(Xapian::Query::OP_OR, normal_terms.begin(), normal_terms.end()));

    query = Xapian::Query(Xapian::Query::OP_AND_NOT,
                    query,
                    Xapian::Query(Xapian::Query::OP_OR, minus_terms.begin(), minus_terms.end()));
</PRE>

<H3>Undefined queries</H3>
<P>
Performing a match with an undefined query matches nothing, which is sometimes
useful.  However an undefined query can't be used with operators to compose
a query.
</P>

<H2>Retrieving the results of a query</H2>

<P>
The Xapian::Enquire class does not require that a method be called in order to
perform the query.  Rather, you simply ask for the results of a query,
and it will perform whatever calculations are necessary to provide the
answer:
</P>
<PRE>
Xapian::MSet <A HREF="apidoc/html/classXapian_1_1Enquire.html">Xapian::Enquire::get_mset</A>(Xapian::doccount first,
                           Xapian::doccount maxitems,
                           const Xapian::RSet * omrset = 0,
                           const Xapian::MatchDecider * mdecider = 0) const
<!-- FIXME check parameters -->
</PRE>
<P>
When asking for the results, you must specify (in <CODE>first</CODE>) the
first item in the result set to return, where the numbering starts at zero
(so a value of
zero corresponds to the first item returned being that with the highest
score, and a value of 10 corresponds to the first 10 items being ignored,
and the returned items starting at the eleventh).
</P>
<P>
You must also specify (in <CODE>maxitems</CODE>) the maximum number of
items to return.  Unless there are not enough matching items, precisely
this number of items will be returned.
If <CODE>maxitems</CODE> is zero, no items will be returned, but the usual
statistics (such as the maximum possible weight which a document could be
assigned by the query) will be calculated.  (See &quot;The Xapian::MSet&quot;
below).
</P>

<H3>The Xapian::MSet</H3>
<P>
Query results are returned in an
<A HREF="apidoc/html/classXapian_1_1MSet.html"><CODE>Xapian::MSet</CODE></A> object.
The results can be accessed using a
<A HREF="apidoc/html/classXapian_1_1MSetIterator.html"><CODE>Xapian::MSetIterator</CODE></A>
which returns the matches in descending sorted order
of relevance (so the most relevant document is first in the list).
Each <CODE>Xapian::MSet</CODE> entry comprises a document id, and the weight
calculated for that document.
</P>
<P>
An <CODE>Xapian::MSet</CODE> also contains various information about the search
result:
<TABLE>
<TR><TD VALIGN="top">
<CODE>firstitem</CODE>
</TD><TD>
The index of the first item in the result which was put into the MSet.
(Corresponding to <CODE>first</CODE> in
<CODE>Xapian::Enquire::get_mset()</CODE>)
</TD></TR><TR><TD VALIGN="top">
<CODE>max_attained</CODE>
</TD><TD VALIGN="top">
The greatest weight which is attained in the full results of the search.
</TD></TR><TR><TD VALIGN="top">
<CODE>max_possible</CODE>
</TD><TD VALIGN="top">
The maximum possible weight in the MSet.
</TD></TR><TR><TD VALIGN="top">
<CODE>docs_considered</CODE>
</TD><TD VALIGN="top">
The number of documents matching the query considered for the MSet.
This provides a lower bound on the number of documents in the database
which have a weight greater than zero.  Note that this value may change
if the search is recalculated with different values for <CODE>first</CODE> or
<CODE>max_items<CODE>.
</TD><TR>
</TABLE>
</P>
<P>
See the <A HREF="apidoc/html/classXapian_1_1MSet.html">automatically extracted documentation</A>
for more details of these fields.
</P>
<P>
The <CODE>Xapian::MSet</CODE> also provides methods for converting the score
calculated for a given document into a percentage value, suitable for
displaying to a user.  This may be done using the
<A HREF="apidoc/html/classXapian_1_1MSet.html"><CODE>convert_to_percent()</CODE></A>
methods:
<PRE>
     int Xapian::MSet::convert_to_percent(const Xapian::MSetIterator &amp; item) const
     int Xapian::MSet::convert_to_percent(Xapian::weight wt) const
</PRE>
These methods return a value in the range 0 to 100, which will be
0 if and only if the item did not match the query at all.
</P>

<H3>Accessing a document</H3>
<P>
A document in the database is accessed via a
<A HREF="apidoc/html/classXapian_1_1Document.html"><CODE>Xapian::Document</CODE></A>
object.
This can be obtained by calling
<A HREF="apidoc/html/classXapian_1_1Database.html"><CODE>Xapian::Database::get_document()</CODE></A>.
The returned <CODE>Xapian::Document</CODE> is a reference counted handle so
copying is cheap.
</P>

<P>
Each document can have the following types of information associated with it:
</P>

<ul>
<li> document data - this is an arbitrary block of data accessed using
<A HREF="apidoc/html/classXapian_1_1Document.html"><CODE>Xapian::Document::get_data()</CODE></A>.
The contents of the document data can be whatever you want and in whatever
format.  Often it contains fields such as a URL or other external UID, a
document title, and an excerpt from the document text.  If you wish to
interoperate with Omega, it should contain name=value pairs, one per line
(recent versions of Omega also support one field value per line, and
can assign names to line numbers in the query template).

<li> terms and positional information - terms index the document (like index
entries in the back of a book); positional information records the word
offset into the document of each occurrence of a particular term.  This is
used to implement phrase searching and the NEAR operator.

<li> document values - these are arbitrary pieces of data which are stored
so they can be accessed rapidly during the match process (to allow sorting
collapsing of duplicates, etc).  Each value is stored in a numbered slot
so you can have several for each document.  There's currently no length limit,
but you should keep them short for efficiency.
</ul>

<P>
There's some overlap in what you can do with terms and with values.  A
simple boolean operator (e.g. document language) is definitely better
done using a term and OP_FILTER.
</P>

<P>
Using a value allows you to do things you can't do with terms, such as
"sort by price", or "show only the best match for each website".  You
can also perform filtering with a value which is more sophisticated
than can easily be achieved with terms, for example: find matches
with a price between $100 and $900.  Omega uses boolean terms to perform
date range filtering, but this might actually be better done using a
value (the code in Omega was written before values were added to
Xapian).
</P>

<H2>Specifying a relevance set</H2>
<P>
Xapian supports the idea of relevance feedback: that is, of allowing the user
to mark documents as being relevant to the search, and using this information
to modify the search.  This is supported by means of relevance sets, which
are simply sets of document ids which are marked as relevant.  These
are held in <A HREF="apidoc/html/classXapian_1_1RSet.html"><CODE>Xapian::RSet</CODE></A> objects,
one of which may optionally be supplied to Xapian in the
<CODE>omrset</CODE> parameter when calling
<CODE>Xapian::Enquire::get_mset()</CODE>.
</P>

<H3>Match options</H3>

<P>
There are various additional options which may be specified when
performing the query.  These are specified by calling
<A HREF="apidoc/html/classXapian_1_1Enquire.html">various methods
of the <CODE>Xapian::Enquire</CODE> object</A>.
The options are as follows.
</P>
<TABLE>
<TR><TD VALIGN="top">
<B>collapse key</B>
</TD><TD VALIGN="top">
Each document in a database may have a set of numbered keys.   The
contents of each key is a string of arbitrary length.
The <CODE>set_collapse_key</A>(Xapian::valueno collapse_key)</CODE>
method specifies a key number upon which to remove duplicates.
Only the most recently set duplicate removal key is active at any time, and the
default is to perform no duplicate removal.
</TD></TR><TR><TD VALIGN="top">
<B>percentage cutoff</B>
</TD><TD VALIGN="top">
It may occasionally be desirable to exclude any documents which have a
weight less than a given percentage value.  This may be done using
<CODE>set_cutoff(Xapian::percent percent_cutoff)</A></CODE>.
</TD></TR><TR><TD VALIGN="top">
<B>sort direction</B>
</TD><TD VALIGN="top">
Some weighting functions may frequently result in several documents being
returned with the same weight.  In this case, by default, the documents
will be returned in ascending document id order.  This can be changed
by using
<CODE>set_docid_order()</A></CODE>
to set the sort direction.  <CODE>set_sort_forward(Xapian::Enquire::DESCENDING)</CODE> may be
useful, for example, when it would be best to return the newest documents,
and new documents are being added to the end of the database (which is
what happens by default).
</TD></TR>
</TABLE>

<H3>Match decision functors</H3>
<P>
Sometimes it may be useful to return only documents matching criteria
which can't be easily represented by queries.  This can be done using
a match decision functor.  To set such a condition, derive a class
from <CODE>Xapian::MatchDecider</CODE> and override the function operator,
<CODE>operator()(const Xapian::Document &amp;doc)</CODE>.  The operator can make
a decision based on the document values via <CODE>Xapian::Document::get_value(Xapian::valueno)</CODE>.
</p>
<p>
The functor will also have access to the document data stored in the
database (via <CODE>Xapian::Document::get_data()</CODE>), but beware that for
most database backends, this is an expensive operation and is likely to slow
down the search considerably.
</P>

<H2>Expand - Suggesting new terms for the query</H2>
<P>
Xapian also supports the idea of calculating terms to add to the
query, based on the relevant documents supplied.  A set of such
terms, together with their weights, may be returned by:
<PRE>
Xapian::ESet Xapian::Enquire::<A HREF="apidoc/html/classXapian_1_1Enquire.html">get_eset</A>(Xapian::termcount maxitems,
                           const Xapian::RSet &amp; omrset,
			   bool exclude_query_terms = true,
			   bool use_exact_termfreq = false,
			   double k = 1.0,
			   const Xapian::ExpandDecider * edecider = 0) const;
Xapian::ESet Xapian::Enquire::<A HREF="apidoc/html/classXapian_1_1Enquire.html">get_eset</A>(Xapian::termcount maxitems,
                           const Xapian::RSet &amp; omrset,
                           const Xapian::ExpandDecider * edecider) const
</PRE>
</P>
<P>
As for <CODE>get_mset</CODE>, up to <CODE>maxitems</CODE> expand terms
will be returned, with fewer being returned if and only if no more terms
could be found.
</P>
<P>
The expand terms are returned in sorted weight order in an
<A HREF="apidoc/html/classXapian_1_1ESet.html"><CODE>Xapian::ESet</CODE></A> item.
</P>

<H3>exclude_query_terms</H3>
<P>
By default terms which are already in the query will never be returned by
<CODE>get_eset()</CODE>.  If <CODE>exclude_query_terms</CODE> is
<CODE>false</CODE>) then query terms may be returned.
</P>

<H3>use_exact_termfreq</H3>
<P>
By default, Xapian uses an approximation to the term frequency when
<CODE>get_eset()</CODE> is called when searching over multiple databases.
This approximation improves performance, and usually still returns good
results.  If you're willing to pay the performance penalty, you can
get Xapian to calculate the exact term frequencies by passing <CODE>true</CODE>
for <CODE>use_exact_termfreq</CODE>.
</P>

<H3>Expand decision functors</H3>
<P>
It is often useful to allow only certain classes of term to be returned
in the expand set.  For example, there may be special terms in the
database with various prefixes, which should be removed from the expand
set.  This is accomplished by providing a decision functor.  To do this,
derive a class from <CODE>Xapian::ExpandDecider</CODE> and override the
function operator, <CODE>operator()(const string &amp;)</CODE>.
The functor is called with each term before it is added to the set,
and it may accept (by returning <CODE>true</CODE>) or reject (by returning
<CODE>false</CODE>) the term as appropriate.
</P>

<H2>Thread safety</H2>
<P>
There's no pthread specific code in Xapian.  If you want to use the same
object concurrently from different threads, it's up to you to police access
(with a mutex or in some other way) to ensure only one method is being
executed at once.  The reason for this is to avoid adding the overhead
of locking and unlocking mutexes when they aren't required.  It also makes
the Xapian code easier to maintain, and simplifies building it.

<P>
For most applications, this is unlikely to be an issue - generally the
calls to Xapian are likely to be from a single thread.  And if they aren't,
you can just create an entirely separate Xapian::Database object in each thread
- this is no different to accessing the same database from two different
processes.
</P>

<H2>Examples</H2>
<P>
Extensively documented examples of simple usage of the Xapian API for
creating databases and then for searching through them are given in the
<A HREF="quickstart.html">QuickStart</A> tutorial.
</P>
<P>
Further examples of usage of Xapian are available in the examples subdirectory
of xapian-core.
</P>

<!-- FOOTER $Author: olly $ $Date: 2009-12-22 04:31:45 +0000 (Tue, 22 Dec 2009) $ $Id: overview.html 13776 2009-12-22 04:31:45Z olly $ -->
</BODY>
</HTML>