xapian-core  1.4.21
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 
71  db.add_synonym("hello", "hi");
72  db.add_synonym("hello", "howdy");
73  db.commit();
74 
75  Xapian::Enquire enquire(db);
77  Xapian::Stem stemmmer("english");
78  qp.set_database(db);
79  qp.add_prefix("foo", "XFOO");
80 
81  const test *p;
82  for (p = test_queries; p->query; ++p) {
83  string expect, parsed;
84  if (p->expect)
85  expect = p->expect;
86  else
87  expect = "parse error";
88  try {
89  unsigned f = qp.FLAG_SYNONYM | qp.FLAG_PARTIAL | qp.FLAG_DEFAULT;
90  Xapian::Query qobj = qp.parse_query(p->query, f);
91  parsed = qobj.get_description();
92  expect = string("Query(") + expect + ')';
93  } catch (const Xapian::QueryParserError &e) {
94  parsed = e.get_msg();
95  } catch (const Xapian::Error &e) {
96  parsed = e.get_description();
97  } catch (...) {
98  parsed = "Unknown exception!";
99  }
100  tout << "Query: " << p->query << '\n';
101  TEST_STRINGS_EQUAL(parsed, expect);
102  }
103 
104  for (p = test_queries_auto; p->query; ++p) {
105  string expect, parsed;
106  if (p->expect)
107  expect = p->expect;
108  else
109  expect = "parse error";
110  try {
111  unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_DEFAULT;
112  Xapian::Query qobj = qp.parse_query(p->query, f);
113  parsed = qobj.get_description();
114  expect = string("Query(") + expect + ')';
115  } catch (const Xapian::QueryParserError &e) {
116  parsed = e.get_msg();
117  } catch (const Xapian::Error &e) {
118  parsed = e.get_description();
119  } catch (...) {
120  parsed = "Unknown exception!";
121  }
122  tout << "Query: " << p->query << '\n';
123  TEST_STRINGS_EQUAL(parsed, expect);
124  }
125 
126  for (p = test_queries_partial_auto; p->query; ++p) {
127  string expect, parsed;
128  if (p->expect)
129  expect = p->expect;
130  else
131  expect = "parse error";
132  try {
133  unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_PARTIAL;
134  f |= qp.FLAG_DEFAULT;
135  Xapian::Query qobj = qp.parse_query(p->query, f);
136  parsed = qobj.get_description();
137  expect = string("Query(") + expect + ')';
138  } catch (const Xapian::QueryParserError &e) {
139  parsed = e.get_msg();
140  } catch (const Xapian::Error &e) {
141  parsed = e.get_description();
142  } catch (...) {
143  parsed = "Unknown exception!";
144  }
145  tout << "Query: " << p->query << '\n';
146  TEST_STRINGS_EQUAL(parsed, expect);
147  }
148 }
Class representing a stemming algorithm.
Definition: stem.h:62
Xapian::WritableDatabase get_writable_database(const string &dbname)
Definition: apitest.cc:87
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:785
std::ostringstream tout
The debug printing stream.
Definition: testsuite.cc:103
Public interfaces for the Xapian library.
void commit()
Commit any pending modifications made to the database.
Definition: omdatabase.cc:857
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:161
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
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:141
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:183
Enable synonym operator &#39;~&#39;.
Definition: queryparser.h:858