8#include "xapianqueryparser.h"
10#include "akonadi_search_xapian_debug.h"
12#include <QTextBoundaryFinder>
16XapianQueryParser::XapianQueryParser() =
default;
18void XapianQueryParser::setDatabase(Xapian::Database *db)
32 return count > rhs.count;
36Xapian::Query makeQuery(
const QString &
string,
int position, Xapian::Database *db)
41 return Xapian::Query(stdString, 1, position);
45 static const int MaxTerms = 100;
49 const std::string stdString(
string.toStdString());
50 Xapian::TermIterator it = db->allterms_begin(stdString);
51 Xapian::TermIterator
end = db->allterms_end(stdString);
52 for (; it !=
end; ++it) {
55 term.count = db->get_collection_freq(term.t);
57 if (topTerms.
size() < MaxTerms) {
59 std::push_heap(topTerms.
begin(), topTerms.
end());
63 std::push_heap(topTerms.
begin(), topTerms.
end());
65 std::pop_heap(topTerms.
begin(), topTerms.
end());
73 for (
const Term &term : std::as_const(topTerms)) {
74 queries << Xapian::Query(term.t, 1, position);
78 return Xapian::Query(
string.toStdString(), 1, position);
80 Xapian::Query finalQ(Xapian::Query::OP_SYNONYM, queries.
begin(), queries.
end());
84bool containsSpace(
const QString &
string)
86 for (
const QChar &ch :
string) {
96Xapian::Query XapianQueryParser::parseQuery(
const QString &text,
const QString &prefix)
122 bool inDoubleQuotes =
false;
123 bool inSingleQuotes =
false;
124 bool inPhrase =
false;
127 for (; bf.position() != -1; bf.toNextBoundary()) {
131 int pos = bf.position();
135 if (inDoubleQuotes) {
136 queries << Xapian::Query(Xapian::Query::OP_PHRASE, phraseQueries.
begin(), phraseQueries.
end());
137 phraseQueries.
clear();
138 inDoubleQuotes =
false;
140 inDoubleQuotes =
true;
143 if (inSingleQuotes) {
144 queries << Xapian::Query(Xapian::Query::OP_PHRASE, phraseQueries.
begin(), phraseQueries.
end());
145 phraseQueries.
clear();
146 inSingleQuotes =
false;
148 inSingleQuotes =
true;
150 }
else if (!containsSpace(delim)) {
151 if (!inPhrase && !queries.
isEmpty()) {
152 phraseQueries << queries.
takeLast();
155 }
else if (inPhrase && !phraseQueries.
isEmpty()) {
156 queries << Xapian::Query(Xapian::Query::OP_PHRASE, phraseQueries.
begin(), phraseQueries.
end());
157 phraseQueries.
clear();
162 start = bf.position();
175 for (
const QChar &ch : denormalized) {
176 auto cat = ch.category();
185 const QString term = prefix + t;
188 if (inDoubleQuotes || inSingleQuotes || inPhrase) {
191 phraseQueries << Xapian::Query(str, 1, position);
194 queries << makeQuery(term, position, m_db);
196 queries << Xapian::Query(term.
toStdString(), 1, position);
204 queries << Xapian::Query(Xapian::Query::OP_PHRASE, phraseQueries.
begin(), phraseQueries.
end());
205 phraseQueries.
clear();
208 if (!phraseQueries.
isEmpty()) {
209 queries << phraseQueries;
210 phraseQueries.
clear();
213 if (queries.
size() == 1) {
214 return queries.
first();
216 return {Xapian::Query::OP_AND, queries.
begin(), queries.
end()};
221 m_autoExpand = autoexpand;
226 const std::string stdString((prefix + word).toUtf8().constData());
227 Xapian::TermIterator it = m_db->allterms_begin(stdString);
228 Xapian::TermIterator end = m_db->allterms_end(stdString);
231 for (; it != end; ++it) {
232 queries << Xapian::Query(*it);
236 return Xapian::Query(stdString);
238 Xapian::Query finalQ(Xapian::Query::OP_SYNONYM, queries.
begin(), queries.
end());
Xapian::Query expandWord(const QString &word, const QString &prefix=QString())
Expands word to every possible option which it can be expanded to.
void setAutoExapand(bool autoexpand)
Set if each word in the string should be treated as a partial word and should be expanded to every po...
Q_SCRIPTABLE Q_NOREPLY void start()
Akonadi search infrastructure.
const QList< QKeySequence > & end()
bool operator<(const PosRange< Trait > &l, const PosRange< Trait > &r)
const char * constData() const const
qsizetype length() const const
qsizetype size() const const
bool isEmpty() const const
void push_back(parameter_type value)
void reserve(qsizetype size)
qsizetype size() const const
QString & append(QChar ch)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const const
QString toLower() const const
std::string toStdString() const const
QByteArray toUtf8() const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const