[PATCH] lib: enable regexp search for 'body' field in sexp queries

Subject: [PATCH] lib: enable regexp search for 'body' field in sexp queries

Date: Sat, 29 Mar 2025 13:39:17 -0300

To: David Bremner, Peter Münster, notmuch@notmuchmail.org

Cc:

From: David Bremner


This only matches against individual terms in the message body, but
may be useful e.g. in some queries simulating stemming for non-English
languages

Test suite is adjusted as follows: switch one expected error test to
use mimetype instead of body, add one test for regex search in the
body.
---
 doc/man7/notmuch-sexp-queries.rst | 2 +-
 lib/parse-sexp.cc                 | 2 +-
 test/T081-sexpr-search.sh         | 8 ++++++--
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/doc/man7/notmuch-sexp-queries.rst b/doc/man7/notmuch-sexp-queries.rst
index 858ff685..a77f4d54 100644
--- a/doc/man7/notmuch-sexp-queries.rst
+++ b/doc/man7/notmuch-sexp-queries.rst
@@ -141,7 +141,7 @@ the most recent database revision (see :option:`count --lastmod`).
   +------------+-----------+-----------+-----------+-----------+----------+
   | attachment |    and    |  phrase   |    yes    |    yes    |    no    |
   +------------+-----------+-----------+-----------+-----------+----------+
-  |    body    |    and    |  phrase   |    no     |    no     |    no    |
+  |    body    |    and    |  phrase   |    no     |    no     |   yes    |
   +------------+-----------+-----------+-----------+-----------+----------+
   |    date    |           |   range   |    no     |    no     |    no    |
   +------------+-----------+-----------+-----------+-----------+----------+
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index 930888e9..9cfd1af6 100644
--- a/lib/parse-sexp.cc
+++ b/lib/parse-sexp.cc
@@ -85,7 +85,7 @@ static _sexp_prefix_t prefixes[] =
     { "attachment",     Xapian::Query::OP_AND,          SEXP_INITIAL_MATCH_ALL,
       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
     { "body",           Xapian::Query::OP_AND,          SEXP_INITIAL_MATCH_ALL,
-      SEXP_FLAG_FIELD },
+      SEXP_FLAG_FIELD | SEXP_FLAG_REGEX },
     { "date",           Xapian::Query::OP_INVALID,      SEXP_INITIAL_MATCH_ALL,
       SEXP_FLAG_RANGE },
     { "from",           Xapian::Query::OP_AND,          SEXP_INITIAL_MATCH_ALL,
diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh
index 8800b545..b97227ec 100755
--- a/test/T081-sexpr-search.sh
+++ b/test/T081-sexpr-search.sh
@@ -669,15 +669,19 @@ EOF
 test_expect_equal_file EXPECTED OUTPUT
 
 test_begin_subtest "regex in illegal field"
-notmuch search --query=sexp '(body (regex foo))' >& OUTPUT
+notmuch search --query=sexp '(mimetype (regex foo))' >& OUTPUT
 cat <<EOF > EXPECTED
 notmuch search: Syntax error in query
-'regex' not supported in field 'body'
+'regex' not supported in field 'mimetype'
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
 notmuch search --output=messages from:cworth > cworth.msg-ids
 
+test_begin_subtest "regexp 'body' search"
+output=$(notmuch search --output=messages --query=sexp '(body (rx taing))')
+test_expect_equal $output 'id:1258500222-32066-1-git-send-email-ingmar@exherbo.org'
+
 test_begin_subtest "regexp 'from' search"
 notmuch search --output=messages --query=sexp '(from (rx cworth))' > OUTPUT
 test_expect_equal_file cworth.msg-ids OUTPUT
-- 
2.47.2

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: