xapian-core  1.4.27
api_collapse.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2009 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "api_collapse.h"
24 
25 #include <xapian.h>
26 
27 #include "apitest.h"
28 #include "testutils.h"
29 
30 using namespace std;
31 
33 DEFINE_TESTCASE(collapsekey5, backend) {
34  Xapian::Database db(get_database("apitest_simpledata"));
35  Xapian::Enquire enquire(db);
36  // "this" matches all documents.
37  enquire.set_query(Xapian::Query("this"));
38 
39  Xapian::MSet full_mset = enquire.get_mset(0, db.get_doccount());
40 
41  for (Xapian::valueno slot = 0; slot < 10; ++slot) {
42  map<string, Xapian::doccount> tally;
43  for (Xapian::docid did = 1; did <= db.get_doccount(); ++did) {
44  ++tally[db.get_document(did).get_value(slot)];
45  }
46 
47  for (Xapian::doccount cmax = db.get_doccount() + 1; cmax > 0; --cmax) {
48  tout << "Collapsing on slot " << slot << " max " << cmax << '\n';
49  enquire.set_collapse_key(slot, cmax);
50  Xapian::MSet mset = enquire.get_mset(0, full_mset.size());
51 
52  // Check the collapse MSet size is as expected.
53  Xapian::doccount expect_size = 0;
54  map<string, Xapian::doccount>::const_iterator i;
55  for (i = tally.begin(); i != tally.end(); ++i) {
56  if (i->first.empty() || i->second <= cmax) {
57  expect_size += i->second;
58  } else {
59  expect_size += cmax;
60  }
61  }
62  TEST_EQUAL(mset.size(), expect_size);
63 
64  // Check that the right number of documents with each collapse key
65  // value are left after collapsing.
66  map<string, Xapian::doccount> seen;
67  for (Xapian::MSetIterator j = mset.begin(); j != mset.end(); ++j) {
68  const string & key = j.get_collapse_key();
69  TEST(tally.find(key) != tally.end());
70  ++seen[key];
71  }
72  for (i = tally.begin(); i != tally.end(); ++i) {
73  if (i->first.empty() || i->second <= cmax) {
74  TEST_EQUAL(seen[i->first], i->second);
75  } else {
76  TEST_EQUAL(seen[i->first], cmax);
77  }
78  }
79  }
80  }
81 }
Xapian::doccount size() const
Return number of items in this MSet object.
Definition: omenquire.cc:318
Xapian::Document get_document(Xapian::docid did) const
Get a document from the database, given its document id.
Definition: omdatabase.cc:490
DEFINE_TESTCASE(collapsekey5, backend)
Simple test of collapsing with collapse_max > 1.
Definition: api_collapse.cc:33
#define TEST(a)
Test a condition, without an additional explanation for failure.
Definition: testsuite.h:275
This class is used to access a database, or a group of databases.
Definition: database.h:68
Class representing a list of search results.
Definition: mset.h:44
STL namespace.
MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems, Xapian::doccount checkatleast=0, const RSet *omrset=0, const MatchDecider *mdecider=0) const
Get (a portion of) the match set for the current query.
Definition: omenquire.cc:938
Xapian::doccount get_doccount() const
Get the number of documents in the database.
Definition: omdatabase.cc:267
test functionality of the Xapian API
std::ostringstream tout
The debug printing stream.
Definition: testsuite.cc:104
Iterator over a Xapian::MSet.
Definition: mset.h:368
Public interfaces for the Xapian library.
MSetIterator begin() const
Return iterator pointing to the first item in this MSet.
Definition: mset.h:624
MSetIterator end() const
Return iterator pointing to just after the last item in this MSet.
Definition: mset.h:629
void set_query(const Xapian::Query &query, Xapian::termcount qlen=0)
Set the query to run.
Definition: omenquire.cc:793
Xapian::Database get_database(const string &dbname)
Definition: apitest.cc:48
This class provides an interface to the information retrieval system for the purpose of searching...
Definition: enquire.h:152
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
Xapian-specific test helper functions and macros.
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
Class representing a query.
Definition: query.h:46
#define TEST_EQUAL(a, b)
Test for equality of two things.
Definition: testsuite.h:278
void set_collapse_key(Xapian::valueno collapse_key, Xapian::doccount collapse_max=1)
Set the collapse key to use for queries.
Definition: omenquire.cc:848
std::string get_value(Xapian::valueno slot) const
Get value by number.
Definition: omdocument.cc:64