From: David Bremner <david@tethera.net>
To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org
Subject: [PATCH 2/2] lib: Add regexp expansion for for tags and paths
Date: Wed, 29 Mar 2017 21:46:04 -0300 [thread overview]
Message-ID: <20170330004604.1504-3-david@tethera.net> (raw)
In-Reply-To: <20170330004604.1504-1-david@tethera.net>
From a ui perspective this looks similar to what was already provided
for from, subject, and mid, but the implimentation is quite
different. It uses the database's list of terms to construct a term
based query equivalent to the passed regular expression.
---
lib/database.cc | 12 ++++++++----
lib/regexp-fields.cc | 31 +++++++++++++++++++++++++------
2 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/lib/database.cc b/lib/database.cc
index 49b3849c..5b13f541 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -259,12 +259,15 @@ prefix_t prefix_table[] = {
{ "file-direntry", "XFDIRENTRY", NOTMUCH_FIELD_NO_FLAGS },
{ "directory-direntry", "XDDIRENTRY", NOTMUCH_FIELD_NO_FLAGS },
{ "thread", "G", NOTMUCH_FIELD_EXTERNAL },
- { "tag", "K", NOTMUCH_FIELD_EXTERNAL },
- { "is", "K", NOTMUCH_FIELD_EXTERNAL },
+ { "tag", "K", NOTMUCH_FIELD_EXTERNAL |
+ NOTMUCH_FIELD_PROCESSOR },
+ { "is", "K", NOTMUCH_FIELD_EXTERNAL |
+ NOTMUCH_FIELD_PROCESSOR },
{ "id", "Q", NOTMUCH_FIELD_EXTERNAL },
{ "mid", "Q", NOTMUCH_FIELD_EXTERNAL |
NOTMUCH_FIELD_PROCESSOR },
- { "path", "P", NOTMUCH_FIELD_EXTERNAL },
+ { "path", "P", NOTMUCH_FIELD_EXTERNAL|
+ NOTMUCH_FIELD_PROCESSOR },
{ "property", "XPROPERTY", NOTMUCH_FIELD_EXTERNAL },
/*
* Unconditionally add ':' to reduce potential ambiguity with
@@ -272,7 +275,8 @@ prefix_t prefix_table[] = {
* letters. See Xapian document termprefixes.html for related
* discussion.
*/
- { "folder", "XFOLDER:", NOTMUCH_FIELD_EXTERNAL },
+ { "folder", "XFOLDER:", NOTMUCH_FIELD_EXTERNAL |
+ NOTMUCH_FIELD_PROCESSOR },
#if HAVE_XAPIAN_FIELD_PROCESSOR
{ "date", NULL, NOTMUCH_FIELD_EXTERNAL |
NOTMUCH_FIELD_PROCESSOR },
diff --git a/lib/regexp-fields.cc b/lib/regexp-fields.cc
index 7ae55e70..1598c17f 100644
--- a/lib/regexp-fields.cc
+++ b/lib/regexp-fields.cc
@@ -138,7 +138,7 @@ static inline Xapian::valueno _find_slot (std::string prefix)
else if (prefix == "mid")
return NOTMUCH_VALUE_MESSAGE_ID;
else
- throw Xapian::QueryParserError ("unsupported regexp field '" + prefix + "'");
+ return Xapian::BAD_VALUENO;
}
RegexpFieldProcessor::RegexpFieldProcessor (std::string prefix,
@@ -156,15 +156,34 @@ RegexpFieldProcessor::RegexpFieldProcessor (std::string prefix,
Xapian::Query
RegexpFieldProcessor::operator() (const std::string & str)
{
- if (str.size () == 0)
- return Xapian::Query(Xapian::Query::OP_AND_NOT,
+ if (str.empty ()) {
+ if (options & NOTMUCH_FIELD_PROBABILISTIC) {
+ return Xapian::Query(Xapian::Query::OP_AND_NOT,
Xapian::Query::MatchAll,
Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix));
+ } else {
+ return Xapian::Query (term_prefix);
+ }
+ }
if (str.at (0) == '/') {
- if (str.at (str.size () - 1) == '/'){
- RegexpPostingSource *postings = new RegexpPostingSource (slot, str.substr(1,str.size () - 2));
- return Xapian::Query (postings->release ());
+ if (str.length() > 1 && str.at (str.size () - 1) == '/'){
+ std::string regexp_str = str.substr(1,str.size () - 2);
+ if (slot != Xapian::BAD_VALUENO) {
+ RegexpPostingSource *postings = new RegexpPostingSource (slot, regexp_str);
+ return Xapian::Query (postings->release ());
+ } else {
+ std::vector<std::string> terms;
+ regex_t regexp;
+
+ compile_regex(regexp, regexp_str.c_str ());
+ for (Xapian::TermIterator it = notmuch->xapian_db->allterms_begin (term_prefix);
+ it != notmuch->xapian_db->allterms_end (); ++it) {
+ if (regexec (®exp, (*it).c_str (), 0, NULL, 0) == 0)
+ terms.push_back(*it);
+ }
+ return Xapian::Query (Xapian::Query::OP_OR, terms.begin(), terms.end());
+ }
} else {
throw Xapian::QueryParserError ("unmatched regex delimiter in '" + str + "'");
}
--
2.11.0
next prev parent reply other threads:[~2017-03-30 0:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-24 12:14 regexp search for more things David Bremner
2017-03-24 12:14 ` [PATCH 1/2] lib: Add regexp searching for mid: prefix David Bremner
2017-03-24 12:14 ` [PATCH 2/2] lib: Add regexp expansion for for tags and paths David Bremner
2017-03-30 0:46 ` v2 regexp search for mid/folder/path David Bremner
2017-03-30 0:46 ` [PATCH 1/2] lib: Add regexp searching for mid: prefix David Bremner
2017-03-30 0:46 ` David Bremner [this message]
2022-05-27 15:29 ` searching to doesn't support regexes ? erik colson
2022-05-27 17:37 ` David Bremner
2022-05-27 19:35 ` erik colson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170330004604.1504-3-david@tethera.net \
--to=david@tethera.net \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).