[PATCH] Add set_max_wildcard_expansion method to the queryparser.

Adam Sjøgren asjo at koldfront.dk
Tue Sep 16 16:41:05 BST 2008


---
 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     |    9 +++++++++
 xapian-core/queryparser/queryparser_internal.h |    4 +++-
 6 files changed, 34 insertions(+), 1 deletions(-)

diff --git a/search-xapian/XS/QueryParser.xs b/search-xapian/XS/QueryParser.xs
index 3bec548..78edaaa 100644
--- a/search-xapian/XS/QueryParser.xs
+++ b/search-xapian/XS/QueryParser.xs
@@ -49,6 +49,12 @@ QueryParser::set_database(database)
     CODE:
 	THIS->set_database(*database);
 
+void
+QueryParser::set_max_wildcard_expansion(max)
+    long max
+    CODE:
+	THIS->set_max_wildcard_expansion(max);
+
 Query *
 QueryParser::parse_query(q, flags = 7)
     string q
diff --git a/search-xapian/Xapian/QueryParser.pm b/search-xapian/Xapian/QueryParser.pm
index f48a12e..3778be3 100644
--- a/search-xapian/Xapian/QueryParser.pm
+++ b/search-xapian/Xapian/QueryParser.pm
@@ -150,6 +150,13 @@ prefix	The term prefix to map this to
 
 Returns a string describing this object.
 
+=item set_max_wildcard_expansion <max>
+
+If the queryparser expands a wildcard to more than max terms, an
+exception will be thrown.
+
+=cut
+
 =back
 
 =head1 REFERENCE
diff --git a/xapian-core/include/xapian/queryparser.h b/xapian-core/include/xapian/queryparser.h
index b03a295..583cb45 100644
--- a/xapian-core/include/xapian/queryparser.h
+++ b/xapian-core/include/xapian/queryparser.h
@@ -355,6 +355,9 @@ class XAPIAN_VISIBILITY_DEFAULT QueryParser {
     /// Specify the database being searched.
     void set_database(const Database &db);
 
+    /// Specify the maximum expansion of a wildcard term.
+    void set_max_wildcard_expansion(long);
+
     /** Parse a query.
      *
      *  @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 9d9f2df..665d0b8 100644
--- a/xapian-core/queryparser/queryparser.cc
+++ b/xapian-core/queryparser/queryparser.cc
@@ -104,6 +104,12 @@ QueryParser::set_database(const Database &db) {
     internal->db = db;
 }
 
+void
+QueryParser::set_max_wildcard_expansion(long max)
+{
+    internal->max_wildcard_expansion = max;
+}
+
 Query
 QueryParser::parse_query(const string &query_string, unsigned flags,
 			 const string &default_prefix)
diff --git a/xapian-core/queryparser/queryparser.lemony b/xapian-core/queryparser/queryparser.lemony
index b6dc261..e723ebc 100644
--- a/xapian-core/queryparser/queryparser.lemony
+++ b/xapian-core/queryparser/queryparser.lemony
@@ -226,6 +226,10 @@ class State {
     Database get_database() const {
 	return qpi->db;
     }
+
+    long get_max_wildcard_expansion() {
+	return qpi->max_wildcard_expansion;
+    }
 };
 
 string
@@ -335,6 +339,8 @@ Term::as_wildcarded_query(State * state_) const
     Database db = state_->get_database();
     vector<Query> subqs;
     list<string>::const_iterator piter;
+    long expansion_count = 0;
+    long max = state_->get_max_wildcard_expansion();
     for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
 	string root = *piter;
 	root += name;
@@ -342,6 +348,9 @@ Term::as_wildcarded_query(State * state_) const
 	while (t != db.allterms_end(root)) {
 	    subqs.push_back(Query(*t, 1, pos));
 	    ++t;
+            if (max != 0 && ++expansion_count > max) {
+                throw Xapian::InvalidOperationError("Wildcard expands too much");
+            }
 	}
     }
     delete this;
diff --git a/xapian-core/queryparser/queryparser_internal.h b/xapian-core/queryparser/queryparser_internal.h
index 88cbb8f..87cfaf5 100644
--- a/xapian-core/queryparser/queryparser_internal.h
+++ b/xapian-core/queryparser/queryparser_internal.h
@@ -74,6 +74,8 @@ class QueryParser::Internal : public Xapian::Internal::RefCntBase {
 
     string corrected_query;
 
+    long max_wildcard_expansion;
+
     void add_prefix(const string &field, const string &prefix, bool filter);
 
     std::string parse_term(Utf8Iterator &it, const Utf8Iterator &end,
@@ -81,7 +83,7 @@ class QueryParser::Internal : public Xapian::Internal::RefCntBase {
 
   public:
     Internal() : stem_action(STEM_NONE), stopper(NULL),
-	default_op(Query::OP_OR), errmsg(NULL) { }
+	default_op(Query::OP_OR), errmsg(NULL) { max_wildcard_expansion=0; }
     Query parse_query(const string & query_string, unsigned int flags, const string & default_prefix);
 };
 
-- 
1.5.6.3


--=-=-=--




More information about the Xapian-discuss mailing list