-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathsearchqueryparser.cpp
360 lines (320 loc) · 14.1 KB
/
searchqueryparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "library/searchqueryparser.h"
#include <QRegularExpression>
#include <memory>
#include "library/searchquery.h"
#include "track/keyutils.h"
constexpr char kNegatePrefix[] = "-";
constexpr char kFuzzyPrefix[] = "~";
// see https://stackoverflow.com/questions/1310473/regex-matching-spaces-but-not-in-strings
#define STRING_QUOTE_SUFFIX "(?=[^\"]*(\"[^\"]*\"[^\"]*)*$)"
const QRegularExpression kSplitIntoWordsRegexp = QRegularExpression(
QStringLiteral(" " STRING_QUOTE_SUFFIX));
const QRegularExpression kSplitOnOrOperatorRegexp = QRegularExpression(
QStringLiteral("\\|" STRING_QUOTE_SUFFIX));
SearchQueryParser::SearchQueryParser(TrackCollection* pTrackCollection, QStringList searchColumns)
: m_pTrackCollection(pTrackCollection),
m_searchCrates(false) {
setSearchColumns(std::move(searchColumns));
m_textFilters << "artist"
<< "album_artist"
<< "album"
<< "title"
<< "genre"
<< "composer"
<< "grouping"
<< "comment"
<< "location"
<< "crate";
m_numericFilters << "track"
<< "bpm"
<< "played"
<< "rating"
<< "bitrate";
m_specialFilters << "year"
<< "key"
<< "duration"
<< "added"
<< "dateadded"
<< "datetime_added"
<< "date_added";
m_fieldToSqlColumns["artist"] << "artist" << "album_artist";
m_fieldToSqlColumns["album_artist"] << "album_artist";
m_fieldToSqlColumns["album"] << "album";
m_fieldToSqlColumns["title"] << "title";
m_fieldToSqlColumns["genre"] << "genre";
m_fieldToSqlColumns["composer"] << "composer";
m_fieldToSqlColumns["grouping"] << "grouping";
m_fieldToSqlColumns["comment"] << "comment";
m_fieldToSqlColumns["year"] << "year";
m_fieldToSqlColumns["track"] << "tracknumber";
m_fieldToSqlColumns["bpm"] << "bpm";
m_fieldToSqlColumns["bitrate"] << "bitrate";
m_fieldToSqlColumns["duration"] << "duration";
m_fieldToSqlColumns["key"] << "key";
m_fieldToSqlColumns["key_id"] << "key_id";
m_fieldToSqlColumns["played"] << "timesplayed";
m_fieldToSqlColumns["lastplayed"] << "last_played_at";
m_fieldToSqlColumns["rating"] << "rating";
m_fieldToSqlColumns["location"] << "location";
m_fieldToSqlColumns["datetime_added"] << "datetime_added";
m_allFilters.append(m_textFilters);
m_allFilters.append(m_numericFilters);
m_allFilters.append(m_specialFilters);
m_fuzzyMatcher = QRegularExpression(QString("^~(%1)$").arg(m_allFilters.join("|")));
m_textFilterMatcher = QRegularExpression(QString("^-?(%1):(.*)$").arg(m_textFilters.join("|")));
m_numericFilterMatcher = QRegularExpression(
QString("^-?(%1):(.*)$").arg(m_numericFilters.join("|")));
m_specialFilterMatcher = QRegularExpression(
QString("^[~-]?(%1):(.*)$").arg(m_specialFilters.join("|")));
}
SearchQueryParser::~SearchQueryParser() {
}
void SearchQueryParser::setSearchColumns(QStringList searchColumns) {
m_queryColumns = std::move(searchColumns);
// we need to create a filtered columns list that are handled differently
for (int i = 0; i < m_queryColumns.size(); ++i) {
if (m_queryColumns[i] == "crate") {
m_searchCrates = true;
m_queryColumns.removeAt(i);
break;
}
}
}
QString SearchQueryParser::getTextArgument(QString argument,
QStringList* tokens) const {
// If the argument is empty, assume the user placed a space after an
// advanced search command. Consume another token and treat that as the
// argument.
argument = argument.trimmed();
if (argument.length() == 0) {
if (tokens->length() > 0) {
argument = tokens->takeFirst();
}
}
// Deal with quoted arguments. If this token started with a quote, then
// search for the closing quote.
if (argument.startsWith("\"")) {
argument = argument.mid(1);
int quote_index = argument.indexOf("\"");
while (quote_index == -1 && tokens->length() > 0) {
argument += " " + tokens->takeFirst();
quote_index = argument.indexOf("\"");
}
if (quote_index == -1) {
// No ending quote found. Since we think they are going to close the
// quote eventually, treat the entire token list as the argument for
// now.
return argument;
}
// Stuff the rest of the argument after the quote back into tokens.
QString remaining = argument.mid(quote_index+1).trimmed();
if (remaining.size() != 0) {
tokens->push_front(remaining);
}
if (quote_index == 0) {
// We have found an explicit empty string ""
// return it as "" to distinguish it from an unfinished empty string
argument = kMissingFieldSearchTerm;
} else {
// Slice off the quote and everything after.
argument = argument.left(quote_index);
}
}
return argument;
}
void SearchQueryParser::parseTokens(QStringList tokens,
AndNode* pQuery) const {
while (tokens.size() > 0) {
QString token = tokens.takeFirst().trimmed();
if (token.length() == 0) {
continue;
}
bool negate = token.startsWith(kNegatePrefix);
std::unique_ptr<QueryNode> pNode;
const QRegularExpressionMatch fuzzyMatch = m_fuzzyMatcher.match(token);
const QRegularExpressionMatch textFilterMatch = m_textFilterMatcher.match(token);
const QRegularExpressionMatch numericFilterMatch = m_numericFilterMatcher.match(token);
const QRegularExpressionMatch specialFilterMatch = m_specialFilterMatcher.match(token);
if (fuzzyMatch.hasMatch()) {
// TODO(XXX): implement this feature.
} else if (textFilterMatch.hasMatch()) {
QString field = textFilterMatch.captured(1);
QString argument = getTextArgument(
textFilterMatch.captured(2), &tokens);
if (argument == kMissingFieldSearchTerm) {
qDebug() << "argument explicit empty";
if (field == "crate") {
pNode = std::make_unique<NoCrateFilterNode>(
&m_pTrackCollection->crates());
qDebug() << pNode->toSql();
} else {
pNode = std::make_unique<NullOrEmptyTextFilterNode>(
m_pTrackCollection->database(), m_fieldToSqlColumns[field]);
qDebug() << pNode->toSql();
}
} else if (!argument.isEmpty()) {
if (field == "crate") {
pNode = std::make_unique<CrateFilterNode>(
&m_pTrackCollection->crates(), argument);
} else {
pNode = std::make_unique<TextFilterNode>(
m_pTrackCollection->database(),
m_fieldToSqlColumns[field], argument);
}
}
} else if (numericFilterMatch.hasMatch()) {
QString field = numericFilterMatch.captured(1);
QString argument = getTextArgument(
numericFilterMatch.captured(2), &tokens)
.trimmed();
if (!argument.isEmpty()) {
if (argument == kMissingFieldSearchTerm) {
pNode = std::make_unique<NullNumericFilterNode>(
m_fieldToSqlColumns[field]);
} else {
pNode = std::make_unique<NumericFilterNode>(
m_fieldToSqlColumns[field], argument);
}
}
} else if (specialFilterMatch.hasMatch()) {
bool fuzzy = token.startsWith(kFuzzyPrefix);
QString field = specialFilterMatch.captured(1);
QString argument = getTextArgument(
specialFilterMatch.captured(2), &tokens)
.trimmed();
if (!argument.isEmpty()) {
if (field == "key") {
mixxx::track::io::key::ChromaticKey key =
KeyUtils::guessKeyFromText(argument);
if (key == mixxx::track::io::key::INVALID) {
if (argument == kMissingFieldSearchTerm) {
pNode = std::make_unique<NullOrEmptyTextFilterNode>(
m_pTrackCollection->database(), m_fieldToSqlColumns[field]);
} else {
pNode = std::make_unique<TextFilterNode>(
m_pTrackCollection->database(), m_fieldToSqlColumns[field], argument);
}
} else {
pNode = std::make_unique<KeyFilterNode>(key, fuzzy);
}
} else if (field == "duration") {
pNode = std::make_unique<DurationFilterNode>(
m_fieldToSqlColumns[field], argument);
} else if (field == "year") {
pNode = std::make_unique<YearFilterNode>(
m_fieldToSqlColumns[field], argument);
} else if (field == "date_added" ||
field == "datetime_added" ||
field == "added" ||
field == "dateadded") {
field = "datetime_added";
pNode = std::make_unique<TextFilterNode>(
m_pTrackCollection->database(), m_fieldToSqlColumns[field], argument);
}
}
} else {
// If no advanced search feature matched, treat it as a search term.
if (negate) {
token = token.mid(1);
}
// Don't trigger on a lone minus sign.
if (!token.isEmpty()) {
QString argument = getTextArgument(token, &tokens);
// For untagged strings we search the track fields as well
// as the crate names the track is in. This allows the user
// to use crates like tags
if (m_searchCrates) {
auto gNode = std::make_unique<OrNode>();
gNode->addNode(std::make_unique<CrateFilterNode>(
&m_pTrackCollection->crates(), argument));
gNode->addNode(std::make_unique<TextFilterNode>(
m_pTrackCollection->database(), m_queryColumns, argument));
pNode = std::move(gNode);
} else {
pNode = std::make_unique<TextFilterNode>(
m_pTrackCollection->database(), m_queryColumns, argument);
}
}
}
if (pNode) {
if (negate) {
pNode = std::make_unique<NotNode>(std::move(pNode));
}
pQuery->addNode(std::move(pNode));
}
}
}
std::unique_ptr<AndNode> SearchQueryParser::parseAndNode(const QString& query) const {
auto pQuery = std::make_unique<AndNode>();
QStringList tokens = query.split(" ");
parseTokens(std::move(tokens), pQuery.get());
return pQuery;
}
std::unique_ptr<OrNode> SearchQueryParser::parseOrNode(const QString& query) const {
auto pQuery = std::make_unique<OrNode>();
QStringList rawAndNodes = query.split(kSplitOnOrOperatorRegexp,
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
Qt::SkipEmptyParts);
#else
QString::SkipEmptyParts);
#endif
for (const QString& rawAndNode : rawAndNodes) {
if (!rawAndNode.isEmpty()) {
pQuery->addNode(parseAndNode(rawAndNode));
}
}
return pQuery;
}
std::unique_ptr<QueryNode> SearchQueryParser::parseQuery(
const QString& query,
const QString& extraFilter) const {
auto pQuery(std::make_unique<AndNode>());
if (!extraFilter.isEmpty()) {
pQuery->addNode(std::make_unique<SqlNode>(extraFilter));
}
if (!query.isEmpty()) {
pQuery->addNode(parseOrNode(query));
}
return pQuery;
}
QStringList SearchQueryParser::splitQueryIntoWords(const QString& query) {
QStringList queryWordList = query.split(kSplitIntoWordsRegexp,
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
Qt::SkipEmptyParts);
#else
QString::SkipEmptyParts);
#endif
return queryWordList;
}
bool SearchQueryParser::queryIsLessSpecific(const QString& original, const QString& changed) {
// separate search query into tokens
QStringList oldWordList = SearchQueryParser::splitQueryIntoWords(original);
QStringList newWordList = SearchQueryParser::splitQueryIntoWords(changed);
// we sort the lists for length so the comperator will pop the longest match first
std::sort(oldWordList.begin(), oldWordList.end(), [=](const QString& v1, const QString& v2) {
return v1.length() > v2.length();
});
std::sort(newWordList.begin(), newWordList.end(), [=](const QString& v1, const QString& v2) {
return v1.length() > v2.length();
});
for (int i = 0; i < oldWordList.length(); i++) {
const QString& oldWord = oldWordList.at(i);
for (int j = 0; j < newWordList.length(); j++) {
const QString& newWord = newWordList.at(j);
// Note(ronso0) Look for missing '~' in newWord (fuzzy matching)?
if ((oldWord.startsWith("-") && oldWord.startsWith(newWord)) ||
(!newWord.contains(":") && oldWord.contains(newWord)) ||
(newWord.contains(":") && oldWord.startsWith(newWord))) {
// we found a match and can remove the search term list
newWordList.removeAt(j);
break;
}
}
}
// if the new search query list contains no more terms, we have a reduced
// search term
if (newWordList.empty()) {
return true;
}
return false;
}