Fix searchdata::simplify() ending up erasing the GUI query, which could need to be reused for further filtering changes

This commit is contained in:
Jean-Francois Dockes 2022-09-25 17:20:12 +02:00
parent 21fedc1fb4
commit 394264a165
2 changed files with 39 additions and 24 deletions

View File

@ -64,9 +64,8 @@ SearchData::~SearchData()
bool SearchData::maybeAddAutoPhrase(Rcl::Db& db, double freqThreshold)
{
LOGDEB0("SearchData::maybeAddAutoPhrase()\n");
// cerr << "BEFORE SIMPLIFY\n"; dump(cerr);
simplify();
// cerr << "AFTER SIMPLIFY\n"; dump(cerr);
if (m_query.empty()) {
LOGDEB2("SearchData::maybeAddAutoPhrase: empty query\n");
@ -176,6 +175,7 @@ bool SearchData::fileNameOnly()
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;
@ -201,8 +201,10 @@ void SearchData::simplify()
clsubp->getSub()->m_maxSize != -1 ||
clsubp->getSub()->m_minSize != -1 ||
clsubp->getSub()->m_haveWildCards) {
if (!clsubp->getSub()->m_query.empty())
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());
@ -221,21 +223,15 @@ void SearchData::simplify()
// 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);
m_query.insert(m_query.begin() + i, clsubp->getSub()->m_query.begin(),
clsubp->getSub()->m_query.end());
for (unsigned int j = i; j < i + clsubp->getSub()->m_query.size(); j++) {
m_query[j]->setParent(this);
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;
// We don't want the clauses to be deleted when the parent is, as we
// know own them.
clsubp->getSub()->m_query.clear();
delete clsubp;
}
//std::cerr << "SIMPLIFY AFTER: "; dump(std::cerr);
}
// Extract terms and groups for highlighting

View File

@ -100,7 +100,7 @@ public:
bool toNativeQuery(Rcl::Db &db, void *);
/** We become the owner of cl and will delete it */
bool addClause(SearchDataClause* cl);
bool addClause(SearchDataClause *cl);
/** If this is a simple query (one field only, no distance clauses),
* add phrase made of query terms to query, so that docs containing the
@ -246,9 +246,10 @@ public:
: m_tp(tp), m_parentSearch(0), m_haveWildCards(0),
m_modifiers(SDCM_NONE), m_weight(1.0), m_exclude(false),
m_rel(REL_CONTAINS) {}
virtual ~SearchDataClause() {}
virtual ~SearchDataClause() = default;
SearchDataClause(const SearchDataClause &) = default;
SearchDataClause& operator=(const SearchDataClause&) = default;
virtual SearchDataClause* clone() = 0;
virtual bool toNativeQuery(Rcl::Db &db, void *) = 0;
bool isFileName() const {return m_tp == SCLT_FILENAME ? true: false;}
@ -336,8 +337,10 @@ public:
m_haveWildCards =
(txt.find_first_of(cstr_minwilds) != std::string::npos);
}
virtual ~SearchDataClauseSimple() {}
virtual ~SearchDataClauseSimple() = default;
virtual SearchDataClauseSimple *clone() override{
return new SearchDataClauseSimple(*this);
}
/** Translate to Xapian query */
virtual bool toNativeQuery(Rcl::Db &, void *) override;
@ -394,7 +397,10 @@ public:
m_text = t1;
m_t2 = t2;
}
virtual ~SearchDataClauseRange() {}
virtual ~SearchDataClauseRange() = default;
virtual SearchDataClauseRange *clone() override {
return new SearchDataClauseRange(*this);
}
virtual void dump(std::ostream& o) const override;
virtual const std::string& gettext2() const {
@ -423,7 +429,10 @@ public:
addModifier(SDCM_FILTER);
}
virtual ~SearchDataClauseFilename() {}
virtual ~SearchDataClauseFilename() = default;
virtual SearchDataClauseFilename *clone() override {
return new SearchDataClauseFilename(*this);
}
virtual bool toNativeQuery(Rcl::Db &, void *) override;
virtual void dump(std::ostream& o) const override;
@ -458,8 +467,10 @@ public:
m_haveWildCards = false;
addModifier(SDCM_FILTER);
}
virtual ~SearchDataClausePath() {}
virtual ~SearchDataClausePath() = default;
virtual SearchDataClausePath *clone() override {
return new SearchDataClausePath(*this);
}
virtual bool toNativeQuery(Rcl::Db &, void *) override;
virtual void dump(std::ostream& o) const override;
@ -475,8 +486,10 @@ public:
const std::string& fld = std::string())
: SearchDataClauseSimple(tp, txt, fld), m_slack(slack) {}
virtual ~SearchDataClauseDist() {}
virtual ~SearchDataClauseDist() = default;
virtual SearchDataClauseDist *clone() override {
return new SearchDataClauseDist(*this);
}
virtual bool toNativeQuery(Rcl::Db &, void *) override;
virtual int getslack() const {
return m_slack;
@ -494,6 +507,12 @@ class SearchDataClauseSub : public SearchDataClause {
public:
SearchDataClauseSub(std::shared_ptr<SearchData> sub)
: SearchDataClause(SCLT_SUB), m_sub(sub) {}
virtual ~SearchDataClauseSub() = default;
virtual SearchDataClauseSub *clone() override {
return new SearchDataClauseSub(*this);
}
virtual bool toNativeQuery(Rcl::Db &db, void *p) override {
bool ret = m_sub->toNativeQuery(db, p);
if (!ret)