]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
mdssvc: fix filtering by share path prefix
authorRalph Boehme <slow@samba.org>
Thu, 4 Sep 2025 15:41:44 +0000 (17:41 +0200)
committerVolker Lendecke <vl@samba.org>
Fri, 10 Oct 2025 10:40:30 +0000 (10:40 +0000)
To correctly filter by share path, use a filter with a prefix match.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15927

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
python/samba/tests/blackbox/mdsearch.py
python/samba/tests/dcerpc/mdssvc.py
source3/rpc_server/mdssvc/es_parser.y
source3/rpc_server/mdssvc/es_parser_test.c
source3/rpc_server/mdssvc/mdssvc_es.c
source3/rpc_server/mdssvc/test_mdsparser_es.c

index 8d67090e18212fb056fbf8bcca1a5c18abbed628..7709ae6b032f9e6ecfb0644785da773ed6140bfc 100644 (file)
@@ -102,8 +102,21 @@ class MdfindBlackboxTests(BlackboxTestCase):
         json_in = r'''{
           "from": 0, "size": 50, "_source": ["path.real"],
           "query": {
-            "query_string": {
-              "query": "(samba*) AND path.real.fulltext:\"%BASEPATH%\""
+            "bool": {
+              "filter": [
+                {
+                  "prefix": {
+                    "path.real": "%BASEPATH%/"
+                  }
+                }
+              ],
+              "must": [
+                {
+                  "query_string": {
+                    "query": "samba*"
+                  }
+                }
+              ]
             }
           }
         }'''
index cd256548b91d7c04d379f2772d071813ad291e30..a6e69f15474c10b9b2e4eea0deb2a149817df34e 100644 (file)
@@ -133,8 +133,21 @@ class MdssvcTests(RpcInterfaceTestCase):
         exp_json_query = r'''{
           "from": 0, "size": 50, "_source": ["path.real"],
           "query": {
-            "query_string": {
-              "query": "(samba*) AND path.real.fulltext:\"%BASEPATH%\""
+            "bool": {
+              "filter": [
+                {
+                  "prefix": {
+                    "path.real": "%BASEPATH%/"
+                  }
+                }
+              ],
+              "must": [
+                {
+                  "query_string": {
+                    "query": "samba*"
+                  }
+                }
+              ]
             }
           }
         }'''
@@ -165,8 +178,21 @@ class MdssvcTests(RpcInterfaceTestCase):
         exp_json_query = r'''{
           "from": 0, "size": 50, "_source": ["path.real"],
           "query": {
-            "query_string": {
-              "query": "(file.filename:x\\+x OR file.filename:x\\*x OR file.filename:x=x OR file.filename:x'x OR file.filename:x\\?x OR file.filename:x\\ x OR file.filename:x\\(x OR file.filename:x\\\"x OR file.filename:x\\\\x) AND path.real.fulltext:\"%BASEPATH%\""
+            "bool": {
+              "filter": [
+                {
+                  "prefix": {
+                    "path.real": "%BASEPATH%/"
+                  }
+                }
+              ],
+              "must": [
+                {
+                  "query_string": {
+                    "query": "file.filename:x\\+x OR file.filename:x\\*x OR file.filename:x=x OR file.filename:x'x OR file.filename:x\\?x OR file.filename:x\\ x OR file.filename:x\\(x OR file.filename:x\\\"x OR file.filename:x\\\\x"
+                  }
+                }
+              ]
             }
           }
         }'''
@@ -207,8 +233,21 @@ class MdssvcTests(RpcInterfaceTestCase):
         exp_json_query = r'''{
           "from": 0, "size": 50, "_source": ["path.real"],
           "query": {
-            "query_string": {
-              "query": "(*samba*) AND path.real.fulltext:\"%BASEPATH%\""
+            "bool": {
+              "filter": [
+                {
+                  "prefix": {
+                    "path.real": "%BASEPATH%/"
+                  }
+                }
+              ],
+              "must": [
+                {
+                  "query_string": {
+                    "query": "*samba*"
+                  }
+                }
+              ]
             }
           }
         }'''
index 7f3275d3d9292810c9d548c2caeec33c981354c1..62ca63d90f20912d441a6220035033eafc99aa55 100644 (file)
@@ -85,7 +85,6 @@
        int mdsyylwrap(void);
        bool map_spotlight_to_es_query(TALLOC_CTX *mem_ctx,
                                       json_t *mappings,
-                                      const char *path_scope,
                                       const char *query_string,
                                       char **_es_query);
 }
@@ -640,7 +639,6 @@ int mdsyylwrap(void)
  **/
 bool map_spotlight_to_es_query(TALLOC_CTX *mem_ctx,
                               json_t *mappings,
-                              const char *path_scope,
                               const char *query_string,
                               char **_es_query)
 {
@@ -691,13 +689,11 @@ bool map_spotlight_to_es_query(TALLOC_CTX *mem_ctx,
                return false;
        }
 
-       es_query = talloc_asprintf(mem_ctx,
-                                  "(%s) AND path.real.fulltext:\\\"%s\\\"",
-                                  s.result, path_scope);
-       TALLOC_FREE(s.frame);
+       es_query = talloc_strdup(mem_ctx, s.result);
        if (es_query == NULL) {
                return false;
        }
+       TALLOC_FREE(s.frame);
 
        *_es_query = es_query;
        return true;
index 7d88c67abffc95a0d732d6dd75c05a3d6fafacb1..dfce8601911e0a8b1056148319210312e75a553a 100644 (file)
@@ -41,7 +41,6 @@ int main(int argc, char **argv)
        char *default_path = NULL;
        const char *path = NULL;
        const char *query_string = NULL;
-       const char *path_scope = NULL;
        char *es_query = NULL;
        bool ok;
 
@@ -50,7 +49,6 @@ int main(int argc, char **argv)
                return 1;
        }
        query_string = argv[1];
-       path_scope = "/foo/bar";
 
        lp_load_global(get_dyn_CONFIGFILE());
 
@@ -86,7 +84,6 @@ int main(int argc, char **argv)
 
        ok = map_spotlight_to_es_query(mem_ctx,
                                           mappings,
-                                          path_scope,
                                           query_string,
                                           &es_query);
        printf("%s\n", ok ? es_query : "*mapping failed*");
index d51441092b42b00fce523fdf7c18399bcc455a8e..83550bcdc23507867d2bbff13ac8567edad37b43 100644 (file)
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
-#define MDSSVC_ELASTIC_QUERY_TEMPLATE  \
-       "{"                             \
-       "    \"from\": %zu,"            \
-       "    \"size\": %zu,"            \
-       "    \"_source\": [%s],"        \
-       "    \"query\": {"              \
-        "        \"query_string\": {"  \
-       "            \"query\": \"%s\"" \
-       "        }"                     \
-       "    }"                         \
-       "}"
+#define MDSSVC_ELASTIC_QUERY_TEMPLATE                  \
+       "{\n"                                           \
+       "    \"from\": %zu,\n"                          \
+       "    \"size\": %zu,\n"                          \
+       "    \"_source\": [%s],\n"                      \
+       "    \"query\": {\n"                            \
+       "        \"bool\": {\n"                         \
+       "            \"filter\": [ {\n"                 \
+       "                \"prefix\": {\n"               \
+       "                    \"path.real\": \"%s/\"\n"  \
+       "                }\n"                           \
+       "            } ],\n"                            \
+       "            \"must\": [ {\n"                   \
+        "                \"query_string\": {\n"                \
+       "                    \"query\": \"%s\"\n"       \
+       "                }\n"                           \
+       "            } ]\n"                             \
+       "        }\n"                                   \
+       "    }\n"                                       \
+       "}\n"
 
 #define MDSSVC_ELASTIC_SOURCES \
        "\"path.real\""
@@ -413,7 +422,6 @@ static bool mds_es_search(struct sl_query *slq)
        ok = map_spotlight_to_es_query(
                s,
                mds_es_ctx->mdssvc_es_ctx->mappings,
-               slq->path_scope,
                slq->query_string,
                &s->es_query);
        if (!ok) {
@@ -621,6 +629,7 @@ static struct tevent_req *mds_es_search_send(TALLOC_CTX *mem_ctx,
                                        s->from,
                                        s->size,
                                        MDSSVC_ELASTIC_SOURCES,
+                                       s->slq->path_scope,
                                        s->es_query);
        if (tevent_req_nomem(elastic_query, req)) {
                return tevent_req_post(req, ev);
index 8fc011220a444188a0d214c8f18f483a9519377b..a61f84ceaff91ffa6946c26368c4c089d4ede5bc 100644 (file)
 #include "lib/param/param.h"
 #include "rpc_server/mdssvc/es_parser.tab.h"
 
-#define PATH_QUERY_SUBEXPR \
-       " AND path.real.fulltext:\\\"/foo/bar\\\""
-
 static struct {
        const char *mds;
        const char *es;
 } map[] = {
        {
                "*==\"samba\"",
-               "(samba)" PATH_QUERY_SUBEXPR
+               "samba"
        }, {
                "kMDItemTextContent==\"samba\"",
-               "(content:samba)" PATH_QUERY_SUBEXPR
+               "content:samba"
        }, {
                "_kMDItemGroupId==\"11\"",
-               "(file.content_type:(application\\\\/pdf))" PATH_QUERY_SUBEXPR
+               "file.content_type:(application\\\\/pdf)"
        }, {
                "kMDItemContentType==\"1\"",
-               "(file.content_type:(message\\\\/rfc822))" PATH_QUERY_SUBEXPR
+               "file.content_type:(message\\\\/rfc822)"
        }, {
                "kMDItemContentType==\"public.content\"",
-               "(file.content_type:(message\\\\/rfc822 application\\\\/pdf application\\\\/vnd.oasis.opendocument.presentation image\\\\/* text\\\\/*))" PATH_QUERY_SUBEXPR
+               "file.content_type:(message\\\\/rfc822 application\\\\/pdf application\\\\/vnd.oasis.opendocument.presentation image\\\\/* text\\\\/*)"
        }, {
                "kMDItemContentTypeTree==\"1\"",
-               "(file.content_type:(message\\\\/rfc822))" PATH_QUERY_SUBEXPR
+               "file.content_type:(message\\\\/rfc822)"
        }, {
                "kMDItemFSContentChangeDate==$time.iso(2018-10-01T10:00:00Z)",
-               "(file.last_modified:2018\\\\-10\\\\-01T10\\\\:00\\\\:00Z)" PATH_QUERY_SUBEXPR
+               "file.last_modified:2018\\\\-10\\\\-01T10\\\\:00\\\\:00Z"
        }, {
                "kMDItemFSContentChangeDate==\"1\"",
-               "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
+               "file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z"
        }, {
                "kMDItemFSCreationDate==\"1\"",
-               "(file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
+               "file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z"
        }, {
                "kMDItemFSName==\"samba*\"",
-               "(file.filename:samba*)" PATH_QUERY_SUBEXPR
+               "file.filename:samba*"
        }, {
                "kMDItemFSOwnerGroupID==\"0\"",
-               "(attributes.owner:0)" PATH_QUERY_SUBEXPR
+               "attributes.owner:0"
        }, {
                "kMDItemFSOwnerUserID==\"0\"",
-               "(attributes.group:0)" PATH_QUERY_SUBEXPR
+               "attributes.group:0"
        }, {
                "kMDItemFSSize==\"1\"",
-               "(file.filesize:1)" PATH_QUERY_SUBEXPR
+               "file.filesize:1"
        }, {
                "kMDItemPath==\"/foo/bar\"",
-               "(path.real:\\\\/foo\\\\/bar)" PATH_QUERY_SUBEXPR
+               "path.real:\\\\/foo\\\\/bar"
        }, {
                "kMDItemAttributeChangeDate==\"1\"",
-               "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
+               "file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z"
        }, {
                "kMDItemAuthors==\"Chouka\"",
-               "(meta.author:Chouka)" PATH_QUERY_SUBEXPR
+               "meta.author:Chouka"
        }, {
                "kMDItemContentCreationDate==\"1\"",
-               "(file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
+               "file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z"
        }, {
                "kMDItemContentModificationDate==\"1\"",
-               "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR
+               "file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z"
        }, {
                "kMDItemCreator==\"Chouka\"",
-               "(meta.raw.creator:Chouka)" PATH_QUERY_SUBEXPR
+               "meta.raw.creator:Chouka"
        }, {
                "kMDItemDescription==\"Dog\"",
-               "(meta.raw.description:Dog)" PATH_QUERY_SUBEXPR
+               "meta.raw.description:Dog"
        }, {
                "kMDItemDisplayName==\"Samba\"",
-               "(file.filename:Samba)" PATH_QUERY_SUBEXPR
+               "file.filename:Samba"
        }, {
                "kMDItemDurationSeconds==\"1\"",
-               "(meta.raw.xmpDM\\\\:duration:1)" PATH_QUERY_SUBEXPR
+               "meta.raw.xmpDM\\\\:duration:1"
        }, {
                "kMDItemNumberOfPages==\"1\"",
-               "(meta.raw.xmpTPg\\\\:NPages:1)" PATH_QUERY_SUBEXPR
+               "meta.raw.xmpTPg\\\\:NPages:1"
        }, {
                "kMDItemTitle==\"Samba\"",
-               "(meta.title:Samba)" PATH_QUERY_SUBEXPR
+               "meta.title:Samba"
        }, {
                "kMDItemAlbum==\"Red Roses for Me\"",
-               "(meta.raw.xmpDM\\\\:album:Red\\\\ Roses\\\\ for\\\\ Me)" PATH_QUERY_SUBEXPR
+               "meta.raw.xmpDM\\\\:album:Red\\\\ Roses\\\\ for\\\\ Me"
        }, {
                "kMDItemBitsPerSample==\"1\"",
-               "(meta.raw.tiff\\\\:BitsPerSample:1)" PATH_QUERY_SUBEXPR
+               "meta.raw.tiff\\\\:BitsPerSample:1"
        }, {
                "kMDItemPixelHeight==\"1\"",
-               "(meta.raw.Image\\\\ Height:1)" PATH_QUERY_SUBEXPR
+               "meta.raw.Image\\\\ Height:1"
        }, {
                "kMDItemPixelWidth==\"1\"",
-               "(meta.raw.Image\\\\ Width:1)" PATH_QUERY_SUBEXPR
+               "meta.raw.Image\\\\ Width:1"
        }, {
                "kMDItemResolutionHeightDPI==\"72\"",
-               "(meta.raw.Y\\\\ Resolution:72)" PATH_QUERY_SUBEXPR
+               "meta.raw.Y\\\\ Resolution:72"
        }, {
                "kMDItemResolutionWidthDPI==\"72\"",
-               "(meta.raw.X\\\\ Resolution:72)" PATH_QUERY_SUBEXPR
+               "meta.raw.X\\\\ Resolution:72"
        },{
                "*!=\"samba\"",
-               "((NOT samba))" PATH_QUERY_SUBEXPR
+               "(NOT samba)"
        }, {
                "kMDItemFSSize!=\"1\"",
-               "((NOT file.filesize:1))" PATH_QUERY_SUBEXPR
+               "(NOT file.filesize:1)"
        }, {
                "kMDItemFSSize>\"1\"",
-               "(file.filesize:{1 TO *})" PATH_QUERY_SUBEXPR
+               "file.filesize:{1 TO *}"
        }, {
                "kMDItemFSSize<\"1\"",
-               "(file.filesize:{* TO 1})" PATH_QUERY_SUBEXPR
+               "file.filesize:{* TO 1}"
        }, {
                "kMDItemFSCreationDate!=\"1\"",
-               "((NOT file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z))" PATH_QUERY_SUBEXPR
+               "(NOT file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)"
        }, {
                "kMDItemFSCreationDate>\"1\"",
-               "(file.created:{2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO *})" PATH_QUERY_SUBEXPR
+               "file.created:{2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO *}"
        }, {
                "kMDItemFSCreationDate<\"1\"",
-               "(file.created:{* TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z})" PATH_QUERY_SUBEXPR
+               "file.created:{* TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z}"
        }, {
                "kMDItemFSName==\"Samba\"||kMDItemTextContent==\"Samba\"",
-               "(file.filename:Samba OR content:Samba)" PATH_QUERY_SUBEXPR
+               "file.filename:Samba OR content:Samba"
        }, {
                "kMDItemFSName==\"Samba\"&&kMDItemTextContent==\"Samba\"",
-               "((file.filename:Samba) AND (content:Samba))" PATH_QUERY_SUBEXPR
+               "(file.filename:Samba) AND (content:Samba)"
        }, {
                "InRange(kMDItemFSCreationDate,1,2)",
-               "(file.created:[2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:02Z])" PATH_QUERY_SUBEXPR
+               "file.created:[2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:02Z]"
        }, {
                "InRange(kMDItemFSSize,1,2)",
-               "(file.filesize:[1 TO 2])" PATH_QUERY_SUBEXPR
+               "file.filesize:[1 TO 2]"
        }
 };
 
@@ -167,38 +164,37 @@ static struct {
 } map_ignore_failures[] = {
        {
                "*==\"Samba\"||foo==\"bar\"",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "*==\"Samba\"&&foo==\"bar\"",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "*==\"Samba\"||kMDItemContentType==\"666\"",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "*==\"Samba\"&&kMDItemContentType==\"666\"",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "*==\"Samba\"||foo==\"bar\"||kMDItemContentType==\"666\"",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "*==\"Samba\"&&foo==\"bar\"&&kMDItemContentType==\"666\"",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "foo==\"bar\"||kMDItemContentType==\"666\"||*==\"Samba\"||x!=\"6\"",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "*==\"Samba\"||InRange(foo,1,2)",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }, {
                "*==\"Samba\"||foo==$time.iso(2018-10-01T10:00:00Z)",
-               "(Samba)" PATH_QUERY_SUBEXPR
+               "Samba"
        }
 };
 
 static void test_mdsparser_es(void **state)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       const char *path_scope = "/foo/bar";
        char *es_query = NULL;
        char *default_path = NULL;
        const char *path = NULL;
@@ -226,7 +222,6 @@ static void test_mdsparser_es(void **state)
                DBG_DEBUG("Mapping: %s\n", map[i].mds);
                ok = map_spotlight_to_es_query(frame,
                                               mappings,
-                                              path_scope,
                                               map[i].mds,
                                               &es_query);
                assert_true(ok);
@@ -245,7 +240,6 @@ static void test_mdsparser_es(void **state)
                DBG_DEBUG("Mapping: %s\n", map_ignore_failures[i].mds);
                ok = map_spotlight_to_es_query(frame,
                                               mappings,
-                                              path_scope,
                                               map_ignore_failures[i].mds,
                                               &es_query);
                assert_true(ok);