[PATCH 16/25] lib/parse-sexp: initial support for wildcard queries

Subject: [PATCH 16/25] lib/parse-sexp: initial support for wildcard queries

Date: Sat, 17 Jul 2021 23:40:12 -0300

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


Because of the implicit way body: queries are implemented, it is not
clear how to support (body :any) queries with current Xapian.
---
 lib/parse-sexp.cc         | 18 +++++++++++---
 test/T081-sexpr-search.sh | 50 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index 14420c0e..95ee7c99 100644
--- a/lib/parse-sexp.cc
+++ b/lib/parse-sexp.cc
@@ -49,7 +49,7 @@ static _sexp_op_t operations[] =
 static _sexp_field_t fields[] =
 {
     { "attachment",   Xapian::Query::OP_PHRASE,       SEXP_FLAG_WILDCARD },
-    { "body",         Xapian::Query::OP_PHRASE,       SEXP_FLAG_WILDCARD },
+    { "body",         Xapian::Query::OP_PHRASE,       SEXP_FLAG_NONE },
     { "date",         Xapian::Query::OP_INVALID,      SEXP_FLAG_NONE },
     { "from",         Xapian::Query::OP_PHRASE,       SEXP_FLAG_NONE },
     { "folder",       Xapian::Query::OP_OR,           SEXP_FLAG_NONE },
@@ -274,6 +274,7 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Qu
     for (const _sexp_field_t *field = fields; field && field->name; field++) {
 	if (strcasecmp (field->name, hd_sexp (sx)->val) == 0) {
 	    _sexp_flag_t flags;
+	    const char *term_prefix;
 	    const sexp_t *rest;
 
 	    notmuch_status_t status = _sexp_parse_keywords (notmuch, sx->list->val,
@@ -295,8 +296,19 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Qu
 		}
 	    }
 
-	    return _sexp_combine_field (_find_prefix (field->name), field->xapian_op, sx->list->next,
-					output);
+	    term_prefix = _find_prefix (field->name);
+
+	    if (flags & SEXP_FLAG_WILDCARD) {
+		if (rest) {
+		    _notmuch_database_log (notmuch, "extra term(s) after wildcard\n", sx->val);
+		    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+		}
+		output = Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix);
+		return NOTMUCH_STATUS_SUCCESS;
+	    } else {
+		return _sexp_combine_field (term_prefix, field->xapian_op, rest,
+					    output);
+	    }
 	}
     }
 
diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh
index f4454ac2..43781f44 100755
--- a/test/T081-sexpr-search.sh
+++ b/test/T081-sexpr-search.sh
@@ -290,4 +290,54 @@ test_expect_success 'notmuch search --query-syntax=sexp "(subject \":foo\")"'
 test_begin_subtest "unknown keyword after non-keyword"
 test_expect_success 'notmuch search --query-syntax=sexp "(subject foo :foo)"'
 
+test_begin_subtest "wildcard search for attachment"
+notmuch search tag:attachment > EXPECTED
+notmuch search --query-syntax=sexp '(attachment :*)' > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "wildcard search for attachment (extra terms)"
+notmuch search --query-syntax=sexp '(attachment :* trailing-garbage)' >& OUTPUT
+cat <<EOF > EXPECTED
+notmuch search: Syntax error in query
+extra term(s) after wildcard
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+add_message '[subject]="empty body"' '[body]="."'
+notmuch tag +nobody id:${gen_msg_id}
+
+add_message '[subject]="no tags"'
+notag_mid=${gen_msg_id}
+notmuch tag -unread -inbox id:${notag_mid}
+
+test_begin_subtest "wildcard search for 'is'"
+notmuch search not id:${notag_mid} > EXPECTED
+notmuch search --query-syntax=sexp '(is :any)' > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "negated wildcard search for 'is'"
+notmuch search id:${notag_mid} > EXPECTED
+notmuch search --query-syntax=sexp '(not (is :any))' > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "wildcard search for 'tag'"
+notmuch search not id:${notag_mid} > EXPECTED
+notmuch search --query-syntax=sexp '(tag :any)' > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "negated wildcard search for 'tag'"
+notmuch search id:${notag_mid} > EXPECTED
+notmuch search --query-syntax=sexp '(not (tag :any))' > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+add_message '[subject]="message with properties"'
+notmuch restore <<EOF
+#= ${gen_msg_id} foo=bar
+EOF
+
+test_begin_subtest "wildcard search for 'property'"
+notmuch search property:foo=bar > EXPECTED
+notmuch search --query-syntax=sexp '(property :any)' > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
 test_done
-- 
2.30.2
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: