File: api_percentages.cc

package info (click to toggle)
xapian-core 1.4.3-2~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 21,224 kB
  • sloc: cpp: 113,806; ansic: 8,723; sh: 4,433; perl: 836; makefile: 567; tcl: 317; python: 40
file content (359 lines) | stat: -rw-r--r-- 11,497 bytes parent folder | download | duplicates (2)
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
/** @file api_percentages.cc
 * @brief Tests of percentage calculations.
 */
/* Copyright (C) 2008,2009 Lemur Consulting Ltd
 * Copyright (C) 2008,2009,2010,2011,2012,2014 Olly Betts
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <config.h>

#include "api_percentages.h"

#include <xapian.h>

#include "apitest.h"
#include "backendmanager_local.h"
#include "str.h"
#include "testutils.h"

#include <cfloat>

using namespace std;

// Test that percentages reported are the same regardless of which part of the
// mset is returned, for sort-by-value search.  Regression test for bug#216 in
// 1.0.10 and earlier with returned percentages.
DEFINE_TESTCASE(consistency3, backend) {
    Xapian::Database db(get_database("apitest_sortconsist"));
    Xapian::Enquire enquire(db);
    enquire.set_query(Xapian::Query("foo"));
    enquire.set_sort_by_value(1, 0);
    Xapian::doccount lots = 3;
    Xapian::MSet bigmset = enquire.get_mset(0, lots);
    TEST_EQUAL(bigmset.size(), lots);
    for (Xapian::doccount start = 0; start < lots; ++start) {
	tout << *bigmset[start] << ":" << bigmset[start].get_weight() << ":"
	     << bigmset[start].get_percent() << "%" << endl;
	for (Xapian::doccount size = 0; size < lots - start; ++size) {
	    Xapian::MSet mset = enquire.get_mset(start, size);
	    if (mset.size()) {
		TEST_EQUAL(start + mset.size(),
			   min(start + size, bigmset.size()));
	    } else if (size) {
		TEST(start >= bigmset.size());
	    }
	    for (Xapian::doccount i = 0; i < mset.size(); ++i) {
		TEST_EQUAL(*mset[i], *bigmset[start + i]);
		TEST_EQUAL_DOUBLE(mset[i].get_weight(),
				  bigmset[start + i].get_weight());
		TEST_EQUAL_DOUBLE(mset[i].get_percent(),
				  bigmset[start + i].get_percent());
	    }
	}
    }
    return true;
}

class MyPostingSource : public Xapian::PostingSource {
    vector<pair<Xapian::docid, double> > weights;
    vector<pair<Xapian::docid, double> >::const_iterator i;
    bool started;

    MyPostingSource(const vector<pair<Xapian::docid, double> > &weights_,
		    double max_wt)
	: weights(weights_), started(false)
    {
	set_maxweight(max_wt);
    }

  public:
    MyPostingSource() : started(false) { }

    PostingSource * clone() const
    {
	return new MyPostingSource(weights, get_maxweight());
    }

    void append_docweight(Xapian::docid did, double wt) {
	weights.push_back(make_pair(did, wt));
	if (wt > get_maxweight()) set_maxweight(wt);
    }

    void init(const Xapian::Database &) { started = false; }

    double get_weight() const { return i->second; }

    Xapian::doccount get_termfreq_min() const { return weights.size(); }
    Xapian::doccount get_termfreq_est() const { return weights.size(); }
    Xapian::doccount get_termfreq_max() const { return weights.size(); }

    void next(double /*wt*/) {
	if (!started) {
	    i = weights.begin();
	    started = true;
	} else {
	    ++i;
	}
    }

    bool at_end() const {
	return (i == weights.end());
    }

    Xapian::docid get_docid() const { return i->first; }

    string get_description() const {
	return "MyPostingSource";
    }
};


/// Test for rounding errors in percentage weight calculations and cutoffs.
DEFINE_TESTCASE(pctcutoff4, backend && !remote && !multi) {
    // Find the number of DBL_EPSILONs to subtract which result in the
    // percentage of the second hit being 49% instead of 50%.
    int epsilons = 0;
    Xapian::Database db(get_database("apitest_simpledata"));
    Xapian::Enquire enquire(db);
    while (true) {
	MyPostingSource source;
	source.append_docweight(1, 100);
	source.append_docweight(2, 50 - epsilons * DBL_EPSILON);
	enquire.set_query(Xapian::Query(&source));
	Xapian::MSet mset = enquire.get_mset(0, 10);
	TEST_EQUAL(mset.size(), 2);
	if (mset[1].get_percent() != 50) break;
	++epsilons;
    }

    // Make a set of document weights including ones on either side of the
    // 49% / 50% boundary.
    MyPostingSource source;
    source.append_docweight(1, 100);
    source.append_docweight(2, 50);
    source.append_docweight(3, 50 - (epsilons - 1) * DBL_EPSILON);
    source.append_docweight(4, 50 - epsilons * DBL_EPSILON);
    source.append_docweight(5, 25);

    enquire.set_query(Xapian::Query(&source));
    Xapian::MSet mset1 = enquire.get_mset(0, 10);
    TEST_EQUAL(mset1.size(), 5);
    TEST_EQUAL(mset1[2].get_percent(), 50);
    TEST_EQUAL(mset1[3].get_percent(), 49);

    // Use various different percentage cutoffs, and check that the values
    // returned are as expected.
    int percent = 100;
    for (Xapian::MSetIterator i = mset1.begin(); i != mset1.end(); ++i) {
	int new_percent = mset1.convert_to_percent(i);
	tout << "mset1 item = " << i.get_percent() << "%\n";
	if (new_percent != percent) {
	    enquire.set_cutoff(percent);
	    Xapian::MSet mset2 = enquire.get_mset(0, 10);
	    tout << "cutoff = " << percent << "%, "
		    "mset size = " << mset2.size() << "\n";
	    TEST_EQUAL(mset2.size(), i.get_rank());
	    percent = new_percent;
	}
    }

    return true;
}

/// Check we throw for a percentage cutoff while sorting primarily by value.
DEFINE_TESTCASE(pctcutoff5, backend) {
    Xapian::Database db(get_database("apitest_simpledata"));
    Xapian::Enquire enquire(db);
    enquire.set_query(Xapian::Query("test"));
    enquire.set_cutoff(42);
    Xapian::MSet mset;

    enquire.set_sort_by_value(0, false);
    TEST_EXCEPTION(Xapian::UnimplementedError, mset = enquire.get_mset(0, 10));

    enquire.set_sort_by_value(0, true);
    TEST_EXCEPTION(Xapian::UnimplementedError, mset = enquire.get_mset(0, 10));

    enquire.set_sort_by_value_then_relevance(0, false);
    TEST_EXCEPTION(Xapian::UnimplementedError, mset = enquire.get_mset(0, 10));

    enquire.set_sort_by_value_then_relevance(0, true);
    TEST_EXCEPTION(Xapian::UnimplementedError, mset = enquire.get_mset(0, 10));

    return true;
}

// Regression test for bug fixed in 1.0.14.
DEFINE_TESTCASE(topercent3, remote) {
    BackendManagerLocal local_manager;
    local_manager.set_datadir(test_driver::get_srcdir() + "/testdata/");
    Xapian::Database db;
    db.add_database(get_database("apitest_simpledata"));
    db.add_database(local_manager.get_database("apitest_simpledata"));

    Xapian::Enquire enquire(db);
    enquire.set_sort_by_value(1, false);

    const char * terms[] = { "paragraph", "banana" };
    enquire.set_query(Xapian::Query(Xapian::Query::OP_OR, terms, terms + 2));

    Xapian::MSet mset = enquire.get_mset(0, 20);

    Xapian::MSetIterator i;
    for (i = mset.begin(); i != mset.end(); ++i) {
	// We should never achieve 100%.
	TEST_REL(i.get_percent(),<,100);
    }

    return true;
}

// Regression test for bug introduced temporarily by the "percent without
// termlist" patch.
DEFINE_TESTCASE(topercent4, backend) {
    Xapian::Enquire enquire(get_database("apitest_simpledata"));

    Xapian::Query query(Xapian::Query::OP_FILTER,
			Xapian::Query("paragraph"),
			Xapian::Query("queri"));
    query = Xapian::Query(Xapian::Query::OP_XOR,
			  query, Xapian::Query("rubbish"));

    enquire.set_query(query);
    Xapian::MSet mset = enquire.get_mset(0, 10);

    // We should get 50% not 33%.
    TEST(!mset.empty());
    TEST_EQUAL(mset[0].get_percent(), 50);

    return true;
}

/// Test that a search with a non-existent term doesn't get 100%.
DEFINE_TESTCASE(topercent5, backend) {
    Xapian::Enquire enquire(get_database("apitest_simpledata"));
    Xapian::Query q(Xapian::Query::OP_OR,
		    Xapian::Query("paragraph"), Xapian::Query("xyzzy"));
    enquire.set_query(q);
    Xapian::MSet mset = enquire.get_mset(0, 10);
    TEST(!mset.empty());
    TEST(mset[0].get_percent() < 100);
    // It would be odd if the non-existent term was worth more, but in 1.0.x
    // the top hit got 4% in this testcase.  In 1.2.x it gets 50%, which is
    // better, but >50% would be more natural.
    TEST(mset[0].get_percent() >= 50);
    return true;
}

/// Test that OP_FILTER doesn't affect percentages.
//  Regression test for bug#590 fixed in 1.3.1 and 1.2.10.
DEFINE_TESTCASE(topercent6, backend) {
    Xapian::Enquire enquire(get_database("apitest_simpledata"));
    Xapian::Query q(Xapian::Query::OP_OR,
		    Xapian::Query("rubbish"), Xapian::Query("letter"));
    enquire.set_query(q);
    Xapian::MSet mset = enquire.get_mset(0, 10);
    TEST(!mset.empty());
    TEST(mset[0].get_percent() < 100);

    q = Xapian::Query(q.OP_FILTER, q, Xapian::Query("this"));
    enquire.set_query(q);
    Xapian::MSet mset2 = enquire.get_mset(0, 10);
    TEST(!mset2.empty());
    TEST_EQUAL(mset[0].get_percent(), mset2[0].get_percent());
    return true;
}

static void
make_topercent7_db(Xapian::WritableDatabase &db, const string &)
{
    for (int i = 1; i <= 6; ++i) {
	Xapian::Document d;
	d.set_data(str(i));
	d.add_term("boom", 2 + (i - 4)*(i - 2));
	if (i != 5)
	    d.add_boolean_term("XCAT122");
	db.add_document(d);
    }
    db.commit();
}

/// Test that a term with wdf always = 0 gets counted.
//  Regression test for bug introduced in 1.2.10 by the original fix for #590,
//  and fixed in 1.2.13 (and in trunk before 1.3.1 was released).
DEFINE_TESTCASE(topercent7, generated) {
    Xapian::Database db(get_database("topercent7", make_topercent7_db));

    Xapian::Query q;
    q = Xapian::Query(q.OP_OR, Xapian::Query("tomb"), Xapian::Query("boom"));
    q = Xapian::Query(q.OP_AND, q, Xapian::Query("XCAT122"));

    Xapian::Enquire enq(db);
    enq.set_query(q);
    Xapian::MSet m = enq.get_mset(0, 10);
    TEST(!m.empty());
    TEST_REL(m[0].get_percent(),>,60);
    return true;
}

class ZWeight : public Xapian::Weight {
  public:
    ZWeight() { }

    void init(double) { }

    Weight * clone() const {
	return new ZWeight();
    }

    double get_sumpart(Xapian::termcount,
		       Xapian::termcount,
		       Xapian::termcount) const {
	return 0.0;
    }

    double get_maxpart() const {
	return 0.0;
    }

    double get_sumextra(Xapian::termcount doclen,
			Xapian::termcount) const {
	return 1.0 / doclen;
    }

    double get_maxextra() const {
	return 1.0;
    }
};

/// Regression test for bug introduced in 1.3.1 and fixed in 1.3.2.
DEFINE_TESTCASE(checkzeromaxpartopt1, backend && !remote) {
    Xapian::Database db = get_database("apitest_simpledata");
    Xapian::Enquire enquire(db);
    // "this" indexes all documents, so will get replaced with MatchAll
    // internally.
    const char * terms[] = { "this", "spoken", "blank" };
    enquire.set_query(Xapian::Query(Xapian::Query::OP_OR, terms, terms + 3));
    ZWeight wt;
    enquire.set_weighting_scheme(wt);
    Xapian::MSet mset = enquire.get_mset(0, db.get_doccount());
    // No documents match all 3 terms, so the score shouldn't be 100%.
    TEST(mset[0].get_percent() != 100);
    // Make sure the percentage score isn't 0 or 1 though.
    TEST_REL(mset[0].get_percent(), >, 1);
    return true;
}