From d7dfd8d4bf56efff32e7f17eb99302bbbda4029f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adam=20Sj=C3=B8gren?= <adsj@novozymes.com>
Date: Mon, 29 Mar 2010 14:54:54 +0200
Subject: [PATCH] Add set_max_wildcard_expansion method to the queryparser.
---
search-xapian/XS/QueryParser.xs | 6 ++++++
search-xapian/Xapian/QueryParser.pm | 7 +++++++
xapian-core/include/xapian/queryparser.h | 3 +++
xapian-core/queryparser/queryparser.cc | 6 ++++++
xapian-core/queryparser/queryparser.lemony | 10 ++++++++++
xapian-core/queryparser/queryparser_internal.h | 4 +++-
6 files changed, 35 insertions(+), 1 deletions(-)
diff --git a/search-xapian/XS/QueryParser.xs b/search-xapian/XS/QueryParser.xs
index 60cf0a3..bc9daf2 100644
a
|
b
|
QueryParser::set_database(database)
|
49 | 49 | CODE: |
50 | 50 | THIS->set_database(*database); |
51 | 51 | |
| 52 | void |
| 53 | QueryParser::set_max_wildcard_expansion(max) |
| 54 | termcount max |
| 55 | CODE: |
| 56 | THIS->set_max_wildcard_expansion(max); |
| 57 | |
52 | 58 | Query * |
53 | 59 | QueryParser::parse_query(q, flags = QueryParser::FLAG_DEFAULT) |
54 | 60 | string q |
diff --git a/search-xapian/Xapian/QueryParser.pm b/search-xapian/Xapian/QueryParser.pm
index c159010..05e3774 100644
a
|
b
|
QueryParser::parse_query() was last called.
|
166 | 166 | |
167 | 167 | If there were no corrections, an empty string is returned. |
168 | 168 | |
| 169 | =item set_max_wildcard_expansion <max> |
| 170 | |
| 171 | If the queryparser expands a wildcard to more than max terms, an |
| 172 | exception will be thrown. |
| 173 | |
| 174 | =cut |
| 175 | |
169 | 176 | =back |
170 | 177 | |
171 | 178 | =head1 REFERENCE |
diff --git a/xapian-core/include/xapian/queryparser.h b/xapian-core/include/xapian/queryparser.h
index 15274fb..27bb4a4 100644
a
|
b
|
class XAPIAN_VISIBILITY_DEFAULT QueryParser {
|
446 | 446 | */ |
447 | 447 | void set_database(const Database &db); |
448 | 448 | |
| 449 | /// Specify the maximum expansion of a wildcard term. |
| 450 | void set_max_wildcard_expansion(Xapian::termcount); |
| 451 | |
449 | 452 | /** Parse a query. |
450 | 453 | * |
451 | 454 | * @param query_string A free-text query as entered by a user |
diff --git a/xapian-core/queryparser/queryparser.cc b/xapian-core/queryparser/queryparser.cc
index ee38248..75f602a 100644
a
|
b
|
QueryParser::set_database(const Database &db) {
|
103 | 103 | internal->db = db; |
104 | 104 | } |
105 | 105 | |
| 106 | void |
| 107 | QueryParser::set_max_wildcard_expansion(Xapian::termcount max) |
| 108 | { |
| 109 | internal->max_wildcard_expansion = max; |
| 110 | } |
| 111 | |
106 | 112 | Query |
107 | 113 | QueryParser::parse_query(const string &query_string, unsigned flags, |
108 | 114 | const string &default_prefix) |
diff --git a/xapian-core/queryparser/queryparser.lemony b/xapian-core/queryparser/queryparser.lemony
index 06b3ef8..ca9d0be 100644
a
|
b
|
|
27 | 27 | #include <xapian/error.h> |
28 | 28 | #include <xapian/unicode.h> |
29 | 29 | #include "stringutils.h" |
| 30 | #include "utils.h" |
30 | 31 | |
31 | 32 | // Include the list of token values lemon generates. |
32 | 33 | #include "queryparser_token.h" |
… |
… |
class State {
|
251 | 252 | void stoplist_resize(size_t s) { |
252 | 253 | qpi->stoplist.resize(s); |
253 | 254 | } |
| 255 | |
| 256 | Xapian::termcount get_max_wildcard_expansion() { |
| 257 | return qpi->max_wildcard_expansion; |
| 258 | } |
254 | 259 | }; |
255 | 260 | |
256 | 261 | string |
… |
… |
Term::as_wildcarded_query(State * state_) const
|
364 | 369 | |
365 | 370 | const list<string> & prefixes = prefix_info->prefixes; |
366 | 371 | list<string>::const_iterator piter; |
| 372 | Xapian::termcount expansion_count = 0; |
| 373 | Xapian::termcount max = state_->get_max_wildcard_expansion(); |
367 | 374 | for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) { |
368 | 375 | string root = *piter; |
369 | 376 | root += name; |
… |
… |
Term::as_wildcarded_query(State * state_) const
|
371 | 378 | while (t != db.allterms_end(root)) { |
372 | 379 | subqs.push_back(Query(*t, 1, pos)); |
373 | 380 | ++t; |
| 381 | if (max != 0 && ++expansion_count > max) { |
| 382 | throw Xapian::QueryParserError("Wildcard "+unstemmed+"* expands too much (exceeding "+om_tostring(max)+" terms)"); |
| 383 | } |
374 | 384 | } |
375 | 385 | } |
376 | 386 | delete this; |
diff --git a/xapian-core/queryparser/queryparser_internal.h b/xapian-core/queryparser/queryparser_internal.h
index b4d76d6..61eb5cc 100644
a
|
b
|
class QueryParser::Internal : public Xapian::Internal::RefCntBase {
|
79 | 79 | void add_prefix(const string &field, const string &prefix, |
80 | 80 | filter_type type); |
81 | 81 | |
| 82 | Xapian::termcount max_wildcard_expansion; |
| 83 | |
82 | 84 | std::string parse_term(Utf8Iterator &it, const Utf8Iterator &end, |
83 | 85 | bool &was_acronym); |
84 | 86 | |
85 | 87 | public: |
86 | 88 | Internal() : stem_action(STEM_NONE), stopper(NULL), |
87 | | default_op(Query::OP_OR), errmsg(NULL) { } |
| 89 | default_op(Query::OP_OR), errmsg(NULL) { max_wildcard_expansion(0); } |
88 | 90 | Query parse_query(const string & query_string, unsigned int flags, const string & default_prefix); |
89 | 91 | }; |
90 | 92 | |