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

File 0001-Add-set_max_wildcard_expansion-method-to-the-queryp2.patch, 4.1 KB (added by Adam Sjøgren, 16 years ago)

Patch to add set_max_wildcard_expansion() to the QueryParser

  • search-xapian/XS/QueryParser.xs

    diff --git a/search-xapian/XS/QueryParser.xs b/search-xapian/XS/QueryParser.xs
    index 3bec548..fbc9ca9 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 = 7)
    5460    string q
  • search-xapian/Xapian/QueryParser.pm

    diff --git a/search-xapian/Xapian/QueryParser.pm b/search-xapian/Xapian/QueryParser.pm
    index f48a12e..3778be3 100644
    a b prefix The term prefix to map this to  
    150150
    151151Returns a string describing this object.
    152152
     153=item set_max_wildcard_expansion <max>
     154
     155If the queryparser expands a wildcard to more than max terms, an
     156exception will be thrown.
     157
     158=cut
     159
    153160=back
    154161
    155162=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 b03a295..1c79b27 100644
    a b class XAPIAN_VISIBILITY_DEFAULT QueryParser {  
    355355    /// Specify the database being searched.
    356356    void set_database(const Database &db);
    357357
     358    /// Specify the maximum expansion of a wildcard term.
     359    void set_max_wildcard_expansion(Xapian::termcount);
     360
    358361    /** Parse a query.
    359362     *
    360363     *  @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 9d9f2df..684733d 100644
    a b QueryParser::set_database(const Database &db) {  
    104104    internal->db = db;
    105105}
    106106
     107void
     108QueryParser::set_max_wildcard_expansion(Xapian::termcount max)
     109{
     110    internal->max_wildcard_expansion = max;
     111}
     112
    107113Query
    108114QueryParser::parse_query(const string &query_string, unsigned flags,
    109115                         const string &default_prefix)
  • xapian-core/queryparser/queryparser.lemony

    diff --git a/xapian-core/queryparser/queryparser.lemony b/xapian-core/queryparser/queryparser.lemony
    index b6dc261..328f63f 100644
    a b class State {  
    226226    Database get_database() const {
    227227        return qpi->db;
    228228    }
     229
     230    Xapian::termcount get_max_wildcard_expansion() {
     231        return qpi->max_wildcard_expansion;
     232    }
    229233};
    230234
    231235string
    Term::as_wildcarded_query(State * state_) const  
    335339    Database db = state_->get_database();
    336340    vector<Query> subqs;
    337341    list<string>::const_iterator piter;
     342    long expansion_count = 0;
     343    Xapian::termcount max = state_->get_max_wildcard_expansion();
    338344    for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
    339345        string root = *piter;
    340346        root += name;
    Term::as_wildcarded_query(State * state_) const  
    342348        while (t != db.allterms_end(root)) {
    343349            subqs.push_back(Query(*t, 1, pos));
    344350            ++t;
     351            if (max != 0 && ++expansion_count > max) {
     352                throw Xapian::QueryParserError("Wildcard "+unstemmed+"* expands too much");
     353            }
    345354        }
    346355    }
    347356    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 88cbb8f..439f583 100644
    a b class QueryParser::Internal : public Xapian::Internal::RefCntBase {  
    7474
    7575    string corrected_query;
    7676
     77    Xapian::termcount max_wildcard_expansion;
     78
    7779    void add_prefix(const string &field, const string &prefix, bool filter);
    7880
    7981    std::string parse_term(Utf8Iterator &it, const Utf8Iterator &end,
    class QueryParser::Internal : public Xapian::Internal::RefCntBase {  
    8183
    8284  public:
    8385    Internal() : stem_action(STEM_NONE), stopper(NULL),
    84         default_op(Query::OP_OR), errmsg(NULL) { }
     86        default_op(Query::OP_OR), errmsg(NULL) { max_wildcard_expansion=0; }
    8587    Query parse_query(const string & query_string, unsigned int flags, const string & default_prefix);
    8688};
    8789