xapian-core  2.0.0
api_queryopt.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2009 Lemur Consulting Ltd
5  * Copyright (C) 2019,2024 Olly Betts
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 
24 #include "api_queryopt.h"
25 
26 #include <xapian.h>
27 
28 #include "testsuite.h"
29 #include "testutils.h"
30 
31 #include "apitest.h"
32 #include <vector>
33 
34 using namespace std;
35 
37  public:
38  MyDontUsePostingSource() : Xapian::PostingSource() {}
39  PostingSource* clone() const override {
40  return new MyDontUsePostingSource();
41  }
42 
43  void reset(const Xapian::Database&, Xapian::doccount) override { }
44 
45  double get_weight() const override {
46  FAIL_TEST("MyDontUsePostingSource::get_weight() called");
47  }
48 
49  // These bounds could be better, but that's not important here.
51  FAIL_TEST("MyDontUsePostingSource::get_termfreq_min() called");
52  }
54  FAIL_TEST("MyDontUsePostingSource::get_termfreq_est() called");
55  }
57  FAIL_TEST("MyDontUsePostingSource::get_termfreq_max() called");
58  }
59 
60  void next(double) override {
61  FAIL_TEST("MyDontUsePostingSource::next() called");
62  }
63 
64  void skip_to(Xapian::docid, double) override {
65  FAIL_TEST("MyDontUsePostingSource::skip_to() called");
66  }
67 
68  bool at_end() const override {
69  FAIL_TEST("MyDontUsePostingSource::at_end() called");
70  }
71 
72  Xapian::docid get_docid() const override {
73  FAIL_TEST("MyDontUsePostingSource::get_docid() called");
74  }
75 
76  string get_description() const override {
77  return "MyDontUsePostingSource";
78  }
79 };
80 
82 DEFINE_TESTCASE(boolandmaybe1, backend && !remote) {
83  Xapian::Enquire enquire(get_database("etext"));
84 
88  Xapian::Query(&src));
89 
90  enquire.set_query(0.0 * query);
91  enquire.get_mset(0, 10);
92 
94  enquire.set_query(query);
95  enquire.get_mset(0, 10);
96 }
97 
101 DEFINE_TESTCASE(boolandmaybe2, backend) {
103  Xapian::Query("last"), Xapian::Query("day"));
104  Xapian::MSet mset1, mset2, mset3;
105  Xapian::Database db(get_database("etext"));
106  Xapian::Enquire enquire1(db), enquire2(db), enquire3(db);
107 
108  enquire1.set_query(0 * query);
109  mset1 = enquire1.get_mset(0, 1000);
110 
111  enquire2.set_query(query);
112  enquire2.set_weighting_scheme(Xapian::BoolWeight());
113  mset2 = enquire2.get_mset(0, 1000);
114 
115  enquire3.set_query(query);
116  mset3 = enquire3.get_mset(0, 1000);
117 
118  TEST_EQUAL(mset1.size(), mset2.size());
119  TEST_EQUAL(mset1.size(), mset3.size());
120 
121  vector<Xapian::docid> docids;
122  for (Xapian::doccount i = 0; i != mset3.size(); ++i) {
123  docids.push_back(*mset3[i]);
124  }
125  sort(docids.begin(), docids.end());
126 
127  for (Xapian::doccount i = 0; i != mset1.size(); ++i) {
128  TEST_EQUAL(*mset1[i], *mset2[i]);
129  TEST_EQUAL(*mset1[i], docids[i]);
130  }
131 
132  for (Xapian::docid did : docids) {
133  Xapian::TermIterator ti1(enquire1.get_matching_terms_begin(did));
134  Xapian::TermIterator ti2(enquire2.get_matching_terms_begin(did));
136 
137  while (ti1 != enquire1.get_matching_terms_end(did)) {
138  TEST(ti2 != enquire2.get_matching_terms_end(did));
139  TEST(ti3 != enquire3.get_matching_terms_end(did));
140  TEST_EQUAL(*ti1, *ti2);
141  TEST_EQUAL(*ti2, *ti3);
142 
143  ++ti1;
144  ++ti2;
145  ++ti3;
146  }
147  TEST(ti2 == enquire2.get_matching_terms_end(did));
148  TEST(ti3 == enquire3.get_matching_terms_end(did));
149  }
150 }
static Xapian::Query query(Xapian::Query::op op, const string &t1=string(), const string &t2=string(), const string &t3=string(), const string &t4=string(), const string &t5=string(), const string &t6=string(), const string &t7=string(), const string &t8=string(), const string &t9=string(), const string &t10=string())
Definition: api_anydb.cc:62
DEFINE_TESTCASE(boolandmaybe1, backend &&!remote)
Test that ANDMAYBE branches are ignored if their weight factor is 0.
Definition: api_queryopt.cc:82
Xapian::Database get_database(const string &dbname)
Definition: apitest.cc:47
test functionality of the Xapian API
void skip_to(Xapian::docid, double) override
Advance to the specified docid.
Definition: api_queryopt.cc:64
Xapian::docid get_docid() const override
Return the current docid.
Definition: api_queryopt.cc:72
string get_description() const override
Return a string describing this object.
Definition: api_queryopt.cc:76
bool at_end() const override
Return true if the current position is past the last entry in this list.
Definition: api_queryopt.cc:68
Xapian::doccount get_termfreq_max() const override
An upper bound on the number of documents this object can return.
Definition: api_queryopt.cc:56
Xapian::doccount get_termfreq_est() const override
An estimate of the number of documents this object can return.
Definition: api_queryopt.cc:53
Xapian::doccount get_termfreq_min() const override
A lower bound on the number of documents this object can return.
Definition: api_queryopt.cc:50
PostingSource * clone() const override
Clone the posting source.
Definition: api_queryopt.cc:39
void next(double) override
Advance the current position to the next matching document.
Definition: api_queryopt.cc:60
void reset(const Xapian::Database &, Xapian::doccount) override
Set this PostingSource to the start of the list of postings.
Definition: api_queryopt.cc:43
double get_weight() const override
Return the weight contribution for the current document.
Definition: api_queryopt.cc:45
Class implementing a "boolean" weighting scheme.
Definition: weight.h:678
An indexed database of documents.
Definition: database.h:75
Querying session.
Definition: enquire.h:57
void set_weighting_scheme(const Weight &weight)
Set the weighting scheme to use.
Definition: enquire.cc:85
MSet get_mset(doccount first, doccount maxitems, doccount checkatleast=0, const RSet *rset=NULL, const MatchDecider *mdecider=NULL) const
Run the query.
Definition: enquire.cc:200
TermIterator get_matching_terms_begin(docid did) const
Iterate query terms matching a document.
Definition: enquire.cc:210
void set_query(const Query &query, termcount query_length=0)
Set the query.
Definition: enquire.cc:72
TermIterator get_matching_terms_end(docid) const noexcept
End iterator corresponding to get_matching_terms_begin().
Definition: enquire.h:435
Class representing a list of search results.
Definition: mset.h:46
Xapian::doccount size() const
Return number of items in this MSet object.
Definition: mset.cc:374
Base class which provides an "external" source of postings.
Definition: postingsource.h:47
Class representing a query.
Definition: query.h:45
@ OP_AND_MAYBE
Match the first subquery taking extra weight from other subqueries.
Definition: query.h:118
static const Xapian::Query MatchAll
A query matching all documents.
Definition: query.h:75
Class for iterating over a list of terms.
Definition: termiterator.h:41
void sort(_RandomAccessIterator first, _RandomAccessIterator last, _Compare comp)
Definition: heap.h:277
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
a generic test suite engine
#define FAIL_TEST(MSG)
Fail the current testcase with message MSG.
Definition: testsuite.h:65
#define TEST_EQUAL(a, b)
Test for equality of two things.
Definition: testsuite.h:276
#define TEST(a)
Test a condition, without an additional explanation for failure.
Definition: testsuite.h:273
Xapian-specific test helper functions and macros.
Public interfaces for the Xapian library.