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
|
/* Copyright (C) 2006-2022 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// Handle translation from rcl's SearchData structures to Xapian Queries
#include "autoconfig.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iostream>
using namespace std;
#include "xapian.h"
#include "cstr.h"
#include "rcldb.h"
#include "rcldb_p.h"
#include "searchdata.h"
#include "log.h"
#include "smallut.h"
#include "textsplit.h"
#include "unacpp.h"
#include "utf8iter.h"
#include "stoplist.h"
#include "rclconfig.h"
#include "termproc.h"
#include "synfamily.h"
#include "stemdb.h"
#include "expansiondbs.h"
#include "base64.h"
#include "daterange.h"
namespace Rcl {
SearchData::~SearchData()
{
LOGDEB0("SearchData::~SearchData\n");
for (auto& clausep : m_query)
delete clausep;
}
// This is called by the GUI simple search if the option is set: add
// (OR) phrase to a query (if it is simple enough) so that results
// where the search terms are close and in order will come up on top.
// We remove very common terms from the query to avoid performance issues.
bool SearchData::maybeAddAutoPhrase(Rcl::Db& db, double freqThreshold)
{
LOGDEB0("SearchData::maybeAddAutoPhrase()\n");
simplify();
if (m_query.empty()) {
LOGDEB2("SearchData::maybeAddAutoPhrase: empty query\n");
return false;
}
string field;
auto clp0 = dynamic_cast<SearchDataClauseSimple*>(*m_query.begin());
if (clp0)
field = clp0->getfield();
vector<string> words;
// Walk the clause list. If this is not an AND list, we find any
// non simple clause or different field names, bail out.
for (auto& clausep : m_query) {
SClType tp = clausep->m_tp;
if (tp != SCLT_AND) {
LOGDEB2("SearchData::maybeAddAutoPhrase: wrong tp " << tp << "\n");
return false;
}
auto clp = dynamic_cast<SearchDataClauseSimple*>(clausep);
if (clp == nullptr) {
LOGDEB2("SearchData::maybeAddAutoPhrase: other than clauseSimple in query.\n");
return false;
}
if (clp->getfield().compare(field)) {
LOGDEB2("SearchData::maybeAddAutoPhrase: diff. fields\n");
return false;
}
// If there are wildcards or quotes in there, bail out
if (clp->gettext().find_first_of("\"*[?") != string::npos) {
LOGDEB2("SearchData::maybeAddAutoPhrase: wildcards\n");
return false;
}
// Do a simple word-split here, not the full-blown
// textsplit. Spans of stopwords should not be trimmed later
// in this function, they will be properly split when the
// phrase gets processed by toNativeQuery() later on.
vector<string> wl;
stringToStrings(clp->gettext(), wl);
words.insert(words.end(), wl.begin(), wl.end());
}
// Trim the word list by eliminating very frequent terms
// (increasing the slack as we do it):
int slack = 0;
int doccnt = db.docCnt();
if (!doccnt)
doccnt = 1;
string swords;
for (const auto& word : words) {
double freq = double(db.termDocCnt(word)) / doccnt;
if (freq < freqThreshold) {
if (!swords.empty())
swords.append(1, ' ');
swords += word;
} else {
LOGDEB0("SearchData::Autophrase: [" << word << "] too frequent (" <<
100 * freq << " %" << ")\n");
slack++;
}
}
// We can't make a phrase with a single word :)
int nwords = TextSplit::countWords(swords);
if (nwords <= 1) {
LOGDEB2("SearchData::maybeAddAutoPhrase: ended with 1 word\n");
return false;
}
// Increase the slack: we want to be a little more laxist than for
// an actual user-entered phrase
slack += 1 + nwords / 3;
m_autophrase = make_shared<SearchDataClauseDist>(SCLT_PHRASE, swords, slack, field);
m_autophrase->setParent(this);
return true;
}
// Add clause to current list. OR lists cant have EXCL clauses.
bool SearchData::addClause(SearchDataClause* cl)
{
if (m_tp == SCLT_OR && cl->getexclude()) {
LOGERR("SearchData::addClause: cant add EXCL to OR list\n");
m_reason = "No Negative (AND_NOT) clauses allowed in OR queries";
return false;
}
cl->setParent(this);
m_haveWildCards = m_haveWildCards || cl->m_haveWildCards;
m_query.push_back(cl);
return true;
}
// Am I a file name only search ? This is to turn off term highlighting.
// There can't be a subclause in a filename search: no possible need to recurse
bool SearchData::fileNameOnly()
{
for (const auto& clausep : m_query) {
if (!clausep->isFileName())
return false;
}
return true;
}
// The query language creates a lot of subqueries. See if we can merge them.
void SearchData::simplify()
{
LOGDEB0("SearchData::simplify()\n");
//std::cerr << "SIMPLIFY BEFORE: "; dump(std::cerr);
for (unsigned int i = 0; i < m_query.size(); i++) {
if (m_query[i]->m_tp != SCLT_SUB)
continue;
auto clsubp = dynamic_cast<SearchDataClauseSub*>(m_query[i]);
if (nullptr == clsubp) {
// ??
continue;
}
if (clsubp->getSub()->m_tp != m_tp) {
LOGDEB0("Not simplifying because sub has differing m_tp\n");
continue;
}
clsubp->getSub()->simplify();
// If this subquery has special attributes, it's not a
// candidate for collapsing, except if it has no clauses, because
// then, we just pick the attributes.
if (!clsubp->getSub()->m_filetypes.empty() ||
!clsubp->getSub()->m_nfiletypes.empty() ||
clsubp->getSub()->m_haveDates ||
#ifdef EXT4_BIRTH_TIME
clsubp->getSub()->m_haveBrDates ||
#endif
clsubp->getSub()->m_maxSize != -1 ||
clsubp->getSub()->m_minSize != -1 ||
clsubp->getSub()->m_subspec != SUBDOC_ANY ||
clsubp->getSub()->m_haveWildCards) {
if (!clsubp->getSub()->m_query.empty()) {
LOGDEB0("Not simplifying because sub has special attributes and non-empty query\n");
continue;
}
m_filetypes.insert(m_filetypes.end(),
clsubp->getSub()->m_filetypes.begin(),
clsubp->getSub()->m_filetypes.end());
m_nfiletypes.insert(m_nfiletypes.end(),
clsubp->getSub()->m_nfiletypes.begin(),
clsubp->getSub()->m_nfiletypes.end());
if (clsubp->getSub()->m_haveDates && !m_haveDates) {
m_dates = clsubp->getSub()->m_dates;
m_haveDates = 1;
}
#ifdef EXT4_BIRTH_TIME
if (clsubp->getSub()->m_haveBrDates && !m_haveBrDates) {
m_brdates = clsubp->getSub()->m_brdates;
m_haveBrDates = 1;
}
#endif
if (m_maxSize == -1)
m_maxSize = clsubp->getSub()->m_maxSize;
if (m_minSize == -1)
m_minSize = clsubp->getSub()->m_minSize;
if (m_subspec == SUBDOC_ANY)
m_subspec = clsubp->getSub()->m_subspec;
m_haveWildCards = m_haveWildCards || clsubp->getSub()->m_haveWildCards;
// And then let the clauses processing go on, there are
// none anyway, we will just delete the subquery.
}
// Delete the clause_sub, and insert the queries from its searchdata in its place
m_query.erase(m_query.begin() + i);
for (unsigned int j = 0; j < clsubp->getSub()->m_query.size(); j++) {
m_query.insert(m_query.begin() + i + j, clsubp->getSub()->m_query[j]->clone());
m_query[i+j]->setParent(this);
}
i += int(clsubp->getSub()->m_query.size()) - 1;
}
//std::cerr << "SIMPLIFY AFTER: "; dump(std::cerr);
}
// Extract terms and groups for highlighting
void SearchData::getTerms(HighlightData &hld) const
{
for (const auto& clausep : m_query) {
if (!(clausep->getModifiers() & SearchDataClause::SDCM_NOTERMS) && !clausep->getexclude()) {
clausep->getTerms(hld);
}
}
sort(hld.spellexpands.begin(), hld.spellexpands.end());
hld.spellexpands.erase(
unique(hld.spellexpands.begin(), hld.spellexpands.end()), hld.spellexpands.end());
return;
}
bool sdataWalk(SearchData* top, SdataWalker& walker)
{
if (!walker.sdata(top, true)) {
return false;
}
for (auto& clausep : top->m_query) {
if (!walker.clause(clausep))
return false;
auto clp = dynamic_cast<SearchDataClauseSub*>(clausep);
if (clp) {
if (!sdataWalk(clp->getSub().get(), walker))
return false;
}
}
if (!walker.sdata(top, false)) {
return false;
}
return true;
}
} // Namespace Rcl
|