Ticket #350: 0001-Add-set_max_wildcard_expansion-method-to-the-queryp3.patch

File 0001-Add-set_max_wildcard_expansion-method-to-the-queryp3.patch, 5.0 KB (added by Adam Sjøgren, 14 years ago)

Patch updated as per richards comments

  • search-xapian/XS/QueryParser.xs

    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)  
    4949    CODE:
    5050        THIS->set_database(*database);
    5151
     52void
     53QueryParser::set_max_wildcard_expansion(max)
     54    termcount max
     55    CODE:
     56        THIS->set_max_wildcard_expansion(max);
     57
    5258Query *
    5359QueryParser::parse_query(q, flags = QueryParser::FLAG_DEFAULT)
    5460    string q
  • search-xapian/Xapian/QueryParser.pm

    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.  
    166166
    167167If there were no corrections, an empty string is returned.
    168168
     169=item set_max_wildcard_expansion <max>
     170
     171If the queryparser expands a wildcard to more than max terms, an
     172exception will be thrown.
     173
     174=cut
     175
    169176=back
    170177
    171178=head1 REFERENCE
  • xapian-core/include/xapian/queryparser.h

    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 {  
    446446     */
    447447    void set_database(const Database &db);
    448448
     449    /// Specify the maximum expansion of a wildcard term.
     450    void set_max_wildcard_expansion(Xapian::termcount);
     451
    449452    /** Parse a query.
    450453     *
    451454     *  @param query_string  A free-text query as entered by a user
  • xapian-core/queryparser/queryparser.cc

    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) {  
    103103    internal->db = db;
    104104}
    105105
     106void
     107QueryParser::set_max_wildcard_expansion(Xapian::termcount max)
     108{
     109    internal->max_wildcard_expansion = max;
     110}
     111
    106112Query
    107113QueryParser::parse_query(const string &query_string, unsigned flags,
    108114                         const string &default_prefix)
  • xapian-core/queryparser/queryparser.lemony

    diff --git a/xapian-core/queryparser/queryparser.lemony b/xapian-core/queryparser/queryparser.lemony
    index 06b3ef8..ca9d0be 100644
    a b  
    2727#include <xapian/error.h>
    2828#include <xapian/unicode.h>
    2929#include "stringutils.h"
     30#include "utils.h"
    3031
    3132// Include the list of token values lemon generates.
    3233#include "queryparser_token.h"
    class State {  
    251252    void stoplist_resize(size_t s) {
    252253        qpi->stoplist.resize(s);
    253254    }
     255
     256    Xapian::termcount get_max_wildcard_expansion() {
     257        return qpi->max_wildcard_expansion;
     258    }
    254259};
    255260
    256261string
    Term::as_wildcarded_query(State * state_) const  
    364369
    365370    const list<string> & prefixes = prefix_info->prefixes;
    366371    list<string>::const_iterator piter;
     372    Xapian::termcount expansion_count = 0;
     373    Xapian::termcount max = state_->get_max_wildcard_expansion();
    367374    for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
    368375        string root = *piter;
    369376        root += name;
    Term::as_wildcarded_query(State * state_) const  
    371378        while (t != db.allterms_end(root)) {
    372379            subqs.push_back(Query(*t, 1, pos));
    373380            ++t;
     381            if (max != 0 && ++expansion_count > max) {
     382                throw Xapian::QueryParserError("Wildcard "+unstemmed+"* expands too much (exceeding "+om_tostring(max)+" terms)");
     383            }
    374384        }
    375385    }
    376386    delete this;
  • xapian-core/queryparser/queryparser_internal.h

    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 {  
    7979    void add_prefix(const string &field, const string &prefix,
    8080                    filter_type type);
    8181
     82    Xapian::termcount max_wildcard_expansion;
     83
    8284    std::string parse_term(Utf8Iterator &it, const Utf8Iterator &end,
    8385                           bool &was_acronym);
    8486
    8587  public:
    8688    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); }
    8890    Query parse_query(const string & query_string, unsigned int flags, const string & default_prefix);
    8991};
    9092