xapian-core  1.4.27
api_qpbackend.cc
Go to the documentation of this file.
1 
4 /* Copyright (c) 2009,2015 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (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
19  * USA
20  */
21 
22 #include <config.h>
23 
24 #include "api_qpbackend.h"
25 
26 #include <xapian.h>
27 
28 #include "backendmanager.h"
29 #include "testsuite.h"
30 #include "testutils.h"
31 
32 #include "apitest.h"
33 
34 using namespace std;
35 
36 namespace {
37 struct test {
38  const char *query;
39  const char *expect;
40 };
41 }
42 
44 DEFINE_TESTCASE(qpsynonympartial1, synonyms) {
45  static const test test_queries[] = {
46  { "hello", "(WILDCARD SYNONYM hello OR hello@1)" },
47  { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
48  { "hello world", "(hello@1 OR (WILDCARD SYNONYM world OR world@2))" },
49  { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR (WILDCARD SYNONYM world OR world@2))" },
50  { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
51  { NULL, NULL }
52  };
53  static const test test_queries_auto[] = {
54  { "hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
55  { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
56  { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
57  { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
58  { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
59  { NULL, NULL }
60  };
61  static const test test_queries_partial_auto[] = {
62  { "hello", "(WILDCARD SYNONYM hello OR hello@1)" },
63  { "~hello", "(WILDCARD SYNONYM hello OR hello@1)" },
64  { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR (WILDCARD SYNONYM world OR world@2))" },
65  { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR (WILDCARD SYNONYM world OR world@2))" },
66  { "world ~hello", "(world@1 OR (WILDCARD SYNONYM hello OR hello@2))" },
67  { NULL, NULL }
68  };
69 
70  Xapian::Database db = get_database("qpsynonympartial1",
72  const string&) {
73  wdb.add_synonym("hello", "hi");
74  wdb.add_synonym("hello", "howdy");
75  });
76  Xapian::Enquire enquire(db);
78  Xapian::Stem stemmmer("english");
79  qp.set_database(db);
80  qp.add_prefix("foo", "XFOO");
81 
82  const test *p;
83  for (p = test_queries; p->query; ++p) {
84  string expect, parsed;
85  if (p->expect)
86  expect = p->expect;
87  else
88  expect = "parse error";
89  try {
90  unsigned f = qp.FLAG_SYNONYM | qp.FLAG_PARTIAL | qp.FLAG_DEFAULT;
91  Xapian::Query qobj = qp.parse_query(p->query, f);
92  parsed = qobj.get_description();
93  expect = string("Query(") + expect + ')';
94  } catch (const Xapian::QueryParserError &e) {
95  parsed = e.get_msg();
96  } catch (const Xapian::Error &e) {
97  parsed = e.get_description();
98  } catch (...) {
99  parsed = "Unknown exception!";
100  }
101  tout << "Query: " << p->query << '\n';
102  TEST_STRINGS_EQUAL(parsed, expect);
103  }
104 
105  for (p = test_queries_auto; p->query; ++p) {
106  string expect, parsed;
107  if (p->expect)
108  expect = p->expect;
109  else
110  expect = "parse error";
111  try {
112  unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_DEFAULT;
113  Xapian::Query qobj = qp.parse_query(p->query, f);
114  parsed = qobj.get_description();
115  expect = string("Query(") + expect + ')';
116  } catch (const Xapian::QueryParserError &e) {
117  parsed = e.get_msg();
118  } catch (const Xapian::Error &e) {
119  parsed = e.get_description();
120  } catch (...) {
121  parsed = "Unknown exception!";
122  }
123  tout << "Query: " << p->query << '\n';
124  TEST_STRINGS_EQUAL(parsed, expect);
125  }
126 
127  for (p = test_queries_partial_auto; p->query; ++p) {
128  string expect, parsed;
129  if (p->expect)
130  expect = p->expect;
131  else
132  expect = "parse error";
133  try {
134  unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_PARTIAL;
135  f |= qp.FLAG_DEFAULT;
136  Xapian::Query qobj = qp.parse_query(p->query, f);
137  parsed = qobj.get_description();
138  expect = string("Query(") + expect + ')';
139  } catch (const Xapian::QueryParserError &e) {
140  parsed = e.get_msg();
141  } catch (const Xapian::Error &e) {
142  parsed = e.get_description();
143  } catch (...) {
144  parsed = "Unknown exception!";
145  }
146  tout << "Query: " << p->query << '\n';
147  TEST_STRINGS_EQUAL(parsed, expect);
148  }
149 }
This class is used to access a database, or a group of databases.
Definition: database.h:68
Class representing a stemming algorithm.
Definition: stem.h:62
const std::string & get_msg() const
Message giving details of the error, intended for human consumption.
Definition: error.h:122
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:778
a generic test suite engine
STL namespace.
Indicates a query string can&#39;t be parsed.
Definition: error.h:887
DEFINE_TESTCASE(qpsynonympartial1, synonyms)
Regression test for bug#407 fixed in 1.0.17 and 1.1.3.
Enable partial matching.
Definition: queryparser.h:837
test functionality of the Xapian API
Base class for backend handling in test harness.
This class provides read/write access to a database.
Definition: database.h:789
std::ostringstream tout
The debug printing stream.
Definition: testsuite.cc:104
Public interfaces for the Xapian library.
Query parse_query(const std::string &query_string, unsigned flags=FLAG_DEFAULT, const std::string &default_prefix=std::string())
Parse a query.
Definition: queryparser.cc:162
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
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:63
Xapian::Database get_database(const string &dbname)
Definition: apitest.cc:48
Enable automatic use of synonyms for single terms.
Definition: queryparser.h:864
void set_database(const Database &db)
Specify the database being searched.
Definition: queryparser.cc:142
std::string get_description() const
Return a string describing this object.
Definition: query.cc:232
This class provides an interface to the information retrieval system for the purpose of searching...
Definition: enquire.h:152
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:43
Xapian-specific test helper functions and macros.
#define TEST_STRINGS_EQUAL(a, b)
Test for equality of two strings.
Definition: testsuite.h:287
void add_synonym(const std::string &term, const std::string &synonym) const
Add a synonym for a term.
Definition: omdatabase.cc:1028
Class representing a query.
Definition: query.h:46
void add_prefix(const std::string &field, const std::string &prefix)
Add a free-text field term prefix.
Definition: queryparser.cc:184
Enable synonym operator &#39;~&#39;.
Definition: queryparser.h:858