Ticket #350: Add-set_max_wildcard_expansion-method-to-the-queryparser-updated.patch

File Add-set_max_wildcard_expansion-method-to-the-queryparser-updated.patch, 4.0 KB (added by Olly Betts, 13 years ago)

xapian-core patch, updated for trunk and tweaked

  • docs/queryparser.html

     
    117117to enable it, and tell the QueryParser which database to expand wildcards from
    118118using the <code>QueryParser::set_database(<i>database</i>)</code> method.
    119119
     120<P>You can limit the number of terms a wildcard will expand to by
     121calling <code>Xapian::QueryParser::set_max_wildcard_expansion()</code>. If a
     122wildcard expands to more terms than that number, an exception will be
     123thrown. The exception may be thrown by the QueryParser, or later when
     124Enquire handles the query. The default is not to limit the expansion.
     125
    120126<H3>Partially entered query matching</H3>
    121127
    122128<P>
  • include/xapian/queryparser.h

     
    446446     */
    447447    void set_database(const Database &db);
    448448
     449    /** Specify the maximum expansion of a wildcard term.
     450     *
     451     *  Note: you must also set FLAG_WILDCARD for wildcard expansion to happen.
     452     *
     453     *  @param limit    The maximum number of terms each wildcard in the query
     454     *                  can expand to, or 0 for no limit.
     455     */
     456    void set_max_wildcard_expansion(Xapian::termcount limit);
     457
    449458    /** Parse a query.
    450459     *
    451460     *  @param query_string  A free-text query as entered by a user
  • queryparser/queryparser.lemony

     
    2626#include "queryparser_internal.h"
    2727#include <xapian/error.h>
    2828#include <xapian/unicode.h>
     29#include "str.h"
    2930#include "stringutils.h"
    3031
    3132// Include the list of token values lemon generates.
     
    257258    void stoplist_resize(size_t s) {
    258259        qpi->stoplist.resize(s);
    259260    }
     261
     262    Xapian::termcount get_max_wildcard_expansion() const {
     263        return qpi->max_wildcard_expansion;
     264    }
    260265};
    261266
    262267string
     
    370375
    371376    const list<string> & prefixes = prefix_info->prefixes;
    372377    list<string>::const_iterator piter;
     378    Xapian::termcount expansion_count = 0;
     379    Xapian::termcount max = state_->get_max_wildcard_expansion();
    373380    for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
    374381        string root = *piter;
    375382        root += name;
    376383        TermIterator t = db.allterms_begin(root);
    377384        while (t != db.allterms_end(root)) {
     385            if (max != 0 && ++expansion_count > max) {
     386                string msg("Wilcard ");
     387                msg += unstemmed;
     388                msg += "* expands to more than ";
     389                msg += str(max);
     390                msg += " terms";
     391                throw Xapian::QueryParserError(msg);
     392            }
    378393            subqs.push_back(Query(*t, 1, pos));
    379394            ++t;
    380395        }
  • queryparser/queryparser_internal.h

     
    7676
    7777    string corrected_query;
    7878
     79    Xapian::termcount max_wildcard_expansion;
     80
    7981    void add_prefix(const string &field, const string &prefix,
    8082                    filter_type type);
    8183
     
    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
  • queryparser/queryparser.cc

     
    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)