File: api_queryopt.cc

package info (click to toggle)
xapian-core 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 25,008 kB
  • sloc: cpp: 136,717; ansic: 11,798; sh: 5,416; perl: 1,024; javascript: 551; makefile: 460; tcl: 299; python: 40
file content (150 lines) | stat: -rw-r--r-- 4,533 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
/** @file
 * @brief Tests of the query optimiser.
 */
/* Copyright (C) 2009 Lemur Consulting Ltd
 * Copyright (C) 2019,2024 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, see
 * <https://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "api_queryopt.h"

#include <xapian.h>

#include "testsuite.h"
#include "testutils.h"

#include "apitest.h"
#include <vector>

using namespace std;

class MyDontUsePostingSource : public Xapian::PostingSource {
  public:
    MyDontUsePostingSource() : Xapian::PostingSource() {}
    PostingSource* clone() const override {
	return new MyDontUsePostingSource();
    }

    void reset(const Xapian::Database&, Xapian::doccount) override { }

    double get_weight() const override {
	FAIL_TEST("MyDontUsePostingSource::get_weight() called");
    }

    // These bounds could be better, but that's not important here.
    Xapian::doccount get_termfreq_min() const override {
	FAIL_TEST("MyDontUsePostingSource::get_termfreq_min() called");
    }
    Xapian::doccount get_termfreq_est() const override {
	FAIL_TEST("MyDontUsePostingSource::get_termfreq_est() called");
    }
    Xapian::doccount get_termfreq_max() const override {
	FAIL_TEST("MyDontUsePostingSource::get_termfreq_max() called");
    }

    void next(double) override {
	FAIL_TEST("MyDontUsePostingSource::next() called");
    }

    void skip_to(Xapian::docid, double) override {
	FAIL_TEST("MyDontUsePostingSource::skip_to() called");
    }

    bool at_end() const override {
	FAIL_TEST("MyDontUsePostingSource::at_end() called");
    }

    Xapian::docid get_docid() const override {
	FAIL_TEST("MyDontUsePostingSource::get_docid() called");
    }

    string get_description() const override {
	return "MyDontUsePostingSource";
    }
};

/// Test that ANDMAYBE branches are ignored if their weight factor is 0.
DEFINE_TESTCASE(boolandmaybe1, backend && !remote) {
    Xapian::Enquire enquire(get_database("etext"));

    MyDontUsePostingSource src;
    Xapian::Query query(Xapian::Query::OP_AND_MAYBE,
			Xapian::Query::MatchAll,
			Xapian::Query(&src));

    enquire.set_query(0.0 * query);
    enquire.get_mset(0, 10);

    enquire.set_weighting_scheme(Xapian::BoolWeight());
    enquire.set_query(query);
    enquire.get_mset(0, 10);
}

/** Test that ANDMAYBE branches return same documents and lists of matching
 *  terms when their weight factor is 0.
 */
DEFINE_TESTCASE(boolandmaybe2, backend) {
    Xapian::Query query(Xapian::Query::OP_AND_MAYBE,
			Xapian::Query("last"), Xapian::Query("day"));
    Xapian::MSet mset1, mset2, mset3;
    Xapian::Database db(get_database("etext"));
    Xapian::Enquire enquire1(db), enquire2(db), enquire3(db);

    enquire1.set_query(0 * query);
    mset1 = enquire1.get_mset(0, 1000);

    enquire2.set_query(query);
    enquire2.set_weighting_scheme(Xapian::BoolWeight());
    mset2 = enquire2.get_mset(0, 1000);

    enquire3.set_query(query);
    mset3 = enquire3.get_mset(0, 1000);

    TEST_EQUAL(mset1.size(), mset2.size());
    TEST_EQUAL(mset1.size(), mset3.size());

    vector<Xapian::docid> docids;
    for (Xapian::doccount i = 0; i != mset3.size(); ++i) {
	docids.push_back(*mset3[i]);
    }
    sort(docids.begin(), docids.end());

    for (Xapian::doccount i = 0; i != mset1.size(); ++i) {
	TEST_EQUAL(*mset1[i], *mset2[i]);
	TEST_EQUAL(*mset1[i], docids[i]);
    }

    for (Xapian::docid did : docids) {
	Xapian::TermIterator ti1(enquire1.get_matching_terms_begin(did));
	Xapian::TermIterator ti2(enquire2.get_matching_terms_begin(did));
	Xapian::TermIterator ti3(enquire3.get_matching_terms_begin(did));

	while (ti1 != enquire1.get_matching_terms_end(did)) {
	    TEST(ti2 != enquire2.get_matching_terms_end(did));
	    TEST(ti3 != enquire3.get_matching_terms_end(did));
	    TEST_EQUAL(*ti1, *ti2);
	    TEST_EQUAL(*ti2, *ti3);

	    ++ti1;
	    ++ti2;
	    ++ti3;
	}
	TEST(ti2 == enquire2.get_matching_terms_end(did));
	TEST(ti3 == enquire3.get_matching_terms_end(did));
    }
}