queryparser/queryparser.cc

Go to the documentation of this file.
00001 /* queryparser.cc: The non-lemon-generated parts of the QueryParser
00002  * class.
00003  *
00004  * Copyright (C) 2005,2006,2007,2008 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00019  * USA
00020  */
00021 
00022 #include <config.h>
00023 
00024 #include <xapian/queryparser.h>
00025 #include <xapian/termiterator.h>
00026 
00027 #include "omdebug.h"
00028 #include "queryparser_internal.h"
00029 #include "vectortermlist.h"
00030 
00031 #include <string.h>
00032 
00033 using namespace Xapian;
00034 
00035 using namespace std;
00036 
00037 // Default implementation in case the user hasn't implemented it.
00038 string
00039 Stopper::get_description() const
00040 {
00041     return "Xapian::Stopper subclass";
00042 }
00043 
00044 string
00045 SimpleStopper::get_description() const
00046 {
00047     string desc("Xapian::SimpleStopper(");
00048     set<string>::const_iterator i;
00049     for (i = stop_words.begin(); i != stop_words.end(); ++i) {
00050         if (i != stop_words.begin()) desc += ' ';
00051         desc += *i;
00052     }
00053     desc += ')';
00054     return desc;
00055 }
00056 
00057 ValueRangeProcessor::~ValueRangeProcessor() { }
00058 
00059 QueryParser::QueryParser(const QueryParser & o) : internal(o.internal) { }
00060 
00061 QueryParser &
00062 QueryParser::operator=(const QueryParser & o)
00063 {
00064     internal = o.internal;
00065     return *this;
00066 }
00067 
00068 QueryParser::QueryParser() : internal(new QueryParser::Internal) { }
00069 
00070 QueryParser::~QueryParser() { }
00071 
00072 void
00073 QueryParser::set_stemmer(const Xapian::Stem & stemmer)
00074 {
00075     internal->stemmer = stemmer;
00076 }
00077 
00078 void
00079 QueryParser::set_stemming_strategy(stem_strategy strategy)
00080 {
00081     internal->stem_action = strategy;
00082 }
00083 
00084 void
00085 QueryParser::set_stopper(const Stopper * stopper)
00086 {
00087     internal->stopper = stopper;
00088 }
00089 
00090 void
00091 QueryParser::set_default_op(Query::op default_op)
00092 {
00093     internal->default_op = default_op;
00094 }
00095 
00096 Query::op
00097 QueryParser::get_default_op() const
00098 {
00099     return internal->default_op;
00100 }
00101 
00102 void
00103 QueryParser::set_database(const Database &db) {
00104     internal->db = db;
00105 }
00106 
00107 Query
00108 QueryParser::parse_query(const string &query_string, unsigned flags,
00109                          const string &default_prefix)
00110 {
00111     internal->stoplist.clear();
00112     internal->unstem.clear();
00113     internal->errmsg = NULL;
00114 
00115     if (query_string.empty()) return Query();
00116 
00117     Query result = internal->parse_query(query_string, flags, default_prefix);
00118     if (internal->errmsg && strcmp(internal->errmsg, "parse error") == 0) {
00119         result = internal->parse_query(query_string, 0, default_prefix);
00120     }
00121 
00122     if (internal->errmsg) throw Xapian::QueryParserError(internal->errmsg);
00123     return result;
00124 }
00125 
00126 void
00127 QueryParser::add_prefix(const string &field, const string &prefix)
00128 {
00129     Assert(internal.get());
00130     internal->add_prefix(field, prefix, false);
00131 }
00132 
00133 void
00134 QueryParser::add_boolean_prefix(const string &field, const string &prefix)
00135 {
00136     Assert(internal.get());
00137     // Don't allow the empty prefix to be set as boolean as it doesn't
00138     // really make sense.
00139     if (field.empty())
00140         throw Xapian::UnimplementedError("Can't set the empty prefix to be a boolean filter");
00141     internal->add_prefix(field, prefix, true);
00142 }
00143 
00144 TermIterator
00145 QueryParser::stoplist_begin() const
00146 {
00147     list<string> & sl = internal->stoplist;
00148     return TermIterator(new VectorTermList(sl.begin(), sl.end()));
00149 }
00150 
00151 TermIterator
00152 QueryParser::unstem_begin(const string &term) const
00153 {
00154     pair<multimap<string, string>::iterator,
00155          multimap<string, string>::iterator> range;
00156     range = internal->unstem.equal_range(term);
00157     list<string> l;
00158     multimap<string, string>::iterator & i = range.first;
00159     while (i != range.second) {
00160         l.push_back(i->second);
00161         ++i;
00162     }
00163     return TermIterator(new VectorTermList(l.begin(), l.end()));
00164 }
00165 
00166 void
00167 QueryParser::add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc)
00168 {
00169     Assert(internal.get());
00170     internal->valrangeprocs.push_back(vrproc);
00171 }
00172 
00173 string
00174 QueryParser::get_corrected_query_string() const
00175 {
00176     return internal->corrected_query;
00177 }
00178 
00179 string
00180 QueryParser::get_description() const
00181 {
00182     // FIXME : describe better!
00183     return "Xapian::QueryParser()";
00184 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.