[tracker-miners/wip/carlosg/search-cli-queries] cli: Specify graphs in "tracker3 search" queries




commit 4371c50f2d97affe29526009badf31bcd072c968
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Aug 6 23:56:43 2022 +0200

    cli: Specify graphs in "tracker3 search" queries
    
    Since the queries did not specify the graph, there is some path
    explosion due to the nie:dataSource being in multiple graphs,
    so all combinations are shown and this is seen as duplicates in
    the result lists.
    
    Specify the graphs in all "tracker3 search" queries, so these
    redundant object paths are trimmed, and we show unique results.
    This also results in a speed up of the queries.
    
    Closes: https://gitlab.gnome.org/GNOME/tracker-miners/-/issues/229

 src/tracker/tracker-search.c | 321 +++++++++++++++++++++++++++++--------------
 1 file changed, 219 insertions(+), 102 deletions(-)
---
diff --git a/src/tracker/tracker-search.c b/src/tracker/tracker-search.c
index c85f8c7d9..4e4ecf493 100644
--- a/src/tracker/tracker-search.c
+++ b/src/tracker/tracker-search.c
@@ -309,17 +309,22 @@ get_document_files (TrackerSparqlConnection *connection,
        else
                limit_str = g_strdup_printf (" ");
 
-       show_all_str = show_all ? "" : "?document nie:isStoredAs/nie:dataSource/tracker:available true .";
+       show_all_str = show_all ? "" : "?u nie:dataSource/tracker:available true .";
        fts = get_fts_string (search_terms, use_or_operator);
 
        if (fts) {
-               query = g_strdup_printf ("SELECT ?document nie:isStoredAs(?document) fts:snippet(?document, 
\"%s\", \"%s\") "
+               query = g_strdup_printf ("SELECT ?document ?u fts:snippet(?document, \"%s\", \"%s\") "
                                         "WHERE { "
-                                        "  ?document a nfo:Document ;"
-                                        "  fts:match \"%s\" ."
-                                        "  %s"
+                                        "  GRAPH tracker:Documents {"
+                                        "    ?document a nfo:Document ;"
+                                        "      nie:isStoredAs ?u ;"
+                                        "      fts:match \"%s\" ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?document)) "
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         disable_color ? "" : SNIPPET_BEGIN,
@@ -329,12 +334,17 @@ get_document_files (TrackerSparqlConnection *connection,
                                         search_offset,
                                         limit_str);
        } else {
-               query = g_strdup_printf ("SELECT ?document nie:isStoredAs(?document) "
+               query = g_strdup_printf ("SELECT ?document ?u "
                                         "WHERE { "
-                                        "  ?document a nfo:Document ."
-                                        "  %s"
+                                        "  GRAPH tracker:Documents {"
+                                        "    ?document a nfo:Document ;"
+                                        "      nie:isStoredAs ?u ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?document)) "
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         show_all_str,
@@ -370,17 +380,22 @@ get_video_files (TrackerSparqlConnection *connection,
        else
                limit_str = g_strdup_printf (" ");
 
-       show_all_str = show_all ? "" : "?video nie:isStoredAs/nie:dataSource/tracker:available true . ";
+       show_all_str = show_all ? "" : "?u nie:dataSource/tracker:available true . ";
        fts = get_fts_string (search_terms, use_or_operator);
 
        if (fts) {
-               query = g_strdup_printf ("SELECT ?video nie:isStoredAs(?video) fts:snippet(?video, \"%s\", 
\"%s\") "
+               query = g_strdup_printf ("SELECT ?video ?u fts:snippet(?video, \"%s\", \"%s\") "
                                         "WHERE { "
-                                        "  ?video a nfo:Video ;"
-                                        "  fts:match \"%s\" ."
-                                        "  %s"
+                                        "  GRAPH tracker:Video {"
+                                        "    ?video a nfo:Video ;"
+                                        "      nie:isStoredAs ?u ;"
+                                        "      fts:match \"%s\" ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?video)) "
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         disable_color ? "" : SNIPPET_BEGIN,
@@ -390,12 +405,17 @@ get_video_files (TrackerSparqlConnection *connection,
                                         search_offset,
                                         limit_str);
        } else {
-               query = g_strdup_printf ("SELECT ?video nie:isStoredAs(?video) "
+               query = g_strdup_printf ("SELECT ?video ?u "
                                         "WHERE { "
-                                        "  ?video a nfo:Video ."
-                                        "  %s"
+                                        "  GRAPH tracker:Video {"
+                                        "    ?video a nfo:Video ;"
+                                        "      nie:isStoredAs ?u ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?video)) "
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         show_all_str,
@@ -431,17 +451,22 @@ get_image_files (TrackerSparqlConnection *connection,
        else
                limit_str = g_strdup_printf (" ");
 
-       show_all_str = show_all ? "" : "?image nie:isStoredAs/nie:dataSource/tracker:available true . ";
+       show_all_str = show_all ? "" : "?u nie:dataSource/tracker:available true . ";
        fts = get_fts_string (search_terms, use_or_operator);
 
        if (fts) {
-               query = g_strdup_printf ("SELECT ?image nie:isStoredAs(?image) fts:snippet(?image, \"%s\", 
\"%s\") "
+               query = g_strdup_printf ("SELECT ?image ?u fts:snippet(?image, \"%s\", \"%s\") "
                                         "WHERE { "
-                                        "  ?image a nfo:Image ;"
-                                        "  fts:match \"%s\" ."
-                                        "  %s"
-                                        "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?image)) "
+                                        "  GRAPH tracker:Pictures {"
+                                        "    ?image a nfo:Image ;"
+                                        "      nie:isStoredAs ?u ;"
+                                        "      fts:match \"%s\" ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
+                                        "}"
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         disable_color ? "" : SNIPPET_BEGIN,
@@ -451,12 +476,17 @@ get_image_files (TrackerSparqlConnection *connection,
                                         search_offset,
                                         limit_str);
        } else {
-               query = g_strdup_printf ("SELECT ?image nie:isStoredAs(?image) "
+               query = g_strdup_printf ("SELECT ?image ?u "
                                         "WHERE { "
-                                        "  ?image a nfo:Image ."
-                                        "  %s"
-                                        "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?image)) "
+                                        "  GRAPH tracker:Pictures {"
+                                        "    ?image a nfo:Image ;"
+                                        "      nie:isStoredAs ?u ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
+                                        "}"
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         show_all_str,
@@ -492,17 +522,22 @@ get_music_files (TrackerSparqlConnection *connection,
        else
                limit_str = g_strdup_printf (" ");
 
-       show_all_str = show_all ? "" : "?song nie:isStoredAs/nie:dataSource/tracker:available true . ";
+       show_all_str = show_all ? "" : "?u nie:dataSource/tracker:available true . ";
        fts = get_fts_string (search_terms, use_or_operator);
 
        if (fts) {
-               query = g_strdup_printf ("SELECT ?song nie:isStoredAs(?song) fts:snippet(?song, \"%s\", 
\"%s\")"
+               query = g_strdup_printf ("SELECT ?song ?u fts:snippet(?song, \"%s\", \"%s\")"
                                         "WHERE { "
-                                        "  ?song a nmm:MusicPiece ;"
-                                        "  fts:match \"%s\" ."
-                                        "  %s"
+                                        "  GRAPH tracker:Audio {"
+                                        "    ?song a nmm:MusicPiece ;"
+                                        "      nie:isStoredAs ?u ;"
+                                        "      fts:match \"%s\" ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?song)) "
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         disable_color ? "" : SNIPPET_BEGIN,
@@ -512,12 +547,17 @@ get_music_files (TrackerSparqlConnection *connection,
                                         search_offset,
                                         limit_str);
        } else {
-               query = g_strdup_printf ("SELECT ?song nie:isStoredAs(?song) "
+               query = g_strdup_printf ("SELECT ?song ?u "
                                         "WHERE { "
-                                        "  ?song a nmm:MusicPiece ."
-                                        "  %s"
+                                        "  GRAPH tracker:Audio {"
+                                        "    ?song a nmm:MusicPiece ;"
+                                        "      nie:isStoredAs ?u ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?song)) "
+                                        "ORDER BY ASC(?u) "
                                         "OFFSET %d "
                                         "%s",
                                         show_all_str,
@@ -557,9 +597,11 @@ get_music_artists (TrackerSparqlConnection *connection,
        if (fts) {
                query = g_strdup_printf ("SELECT ?artist ?title "
                                         "WHERE {"
-                                        "  ?artist a nmm:Artist ;"
-                                        "  nmm:artistName ?title ;"
-                                        "  fts:match \"%s\" . "
+                                        "  GRAPH tracker:Audio {"
+                                        "    ?artist a nmm:Artist ;"
+                                        "    nmm:artistName ?title ;"
+                                        "    fts:match \"%s\" . "
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(?title) "
                                         "OFFSET %d "
@@ -570,8 +612,10 @@ get_music_artists (TrackerSparqlConnection *connection,
        } else {
                query = g_strdup_printf ("SELECT ?artist ?title "
                                         "WHERE {"
-                                        "  ?artist a nmm:Artist ;"
-                                        "  nmm:artistName ?title . "
+                                        "  GRAPH tracker:Audio {"
+                                        "    ?artist a nmm:Artist ;"
+                                        "    nmm:artistName ?title . "
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(?title) "
                                         "OFFSET %d "
@@ -651,8 +695,10 @@ get_music_albums (TrackerSparqlConnection *connection,
        if (fts) {
                query = g_strdup_printf ("SELECT ?album nie:title(?album) "
                                         "WHERE {"
-                                        "  ?album a nmm:MusicAlbum ;"
-                                        "  fts:match \"%s\" ."
+                                        "  GRAPH tracker:Audio {"
+                                        "    ?album a nmm:MusicAlbum ;"
+                                        "    fts:match \"%s\" ."
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(nie:title(?album)) "
                                         "OFFSET %d "
@@ -663,7 +709,9 @@ get_music_albums (TrackerSparqlConnection *connection,
        } else {
                query = g_strdup_printf ("SELECT ?album nie:title(?album) "
                                         "WHERE {"
-                                        "  ?album a nmm:MusicAlbum ."
+                                        "  GRAPH tracker:Audio {"
+                                        "    ?album a nmm:MusicAlbum ."
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(nie:title(?album)) "
                                         "OFFSET %d "
@@ -828,8 +876,10 @@ get_software (TrackerSparqlConnection *connection,
        if (fts) {
                query = g_strdup_printf ("SELECT ?soft nie:title(?soft) fts:snippet(?soft, \"%s\", \"%s\") "
                                         "WHERE {"
-                                        "  ?soft a nfo:Software ;"
-                                        "  fts:match \"%s\" . "
+                                        "  GRAPH tracker:Software {"
+                                        "    ?soft a nfo:Software ;"
+                                        "    fts:match \"%s\" . "
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(nie:title(?soft)) "
                                         "OFFSET %d "
@@ -915,8 +965,10 @@ get_software_categories (TrackerSparqlConnection *connection,
        if (fts) {
                query = g_strdup_printf ("SELECT ?cat nie:title(?cat) "
                                         "WHERE {"
-                                        "  ?cat a nfo:SoftwareCategory ;"
-                                        "  fts:match \"%s\" . "
+                                        "  GRAPH tracker:Software {"
+                                        "    ?cat a nfo:SoftwareCategory ;"
+                                        "    fts:match \"%s\" . "
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(nie:title(?cat)) "
                                         "OFFSET %d "
@@ -927,7 +979,9 @@ get_software_categories (TrackerSparqlConnection *connection,
        } else {
                query = g_strdup_printf ("SELECT ?cat nie:title(?cat) "
                                         "WHERE {"
-                                        "  ?cat a nfo:SoftwareCategory ."
+                                        "  GRAPH tracker:Software {"
+                                        "    ?cat a nfo:SoftwareCategory ."
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(nie:title(?cat)) "
                                         "OFFSET %d "
@@ -997,16 +1051,20 @@ get_files (TrackerSparqlConnection *connection,
        else
                limit_str = g_strdup_printf (" ");
 
-       show_all_str = show_all ? "" : "?u nie:isStoredAs/nie:dataSource/tracker:available true . ";
+       show_all_str = show_all ? "" : "?url nie:dataSource/tracker:available true . ";
        fts = get_fts_string (search_terms, use_or_operator);
 
        if (fts) {
-               query = g_strdup_printf ("SELECT ?u ?url "
+               query = g_strdup_printf ("SELECT DISTINCT ?u ?url "
                                         "WHERE { "
-                                        "  ?u a nie:InformationElement ;"
-                                        "  nie:isStoredAs ?url ;"
-                                        "  fts:match \"%s\" ."
-                                        "  %s"
+                                        "  GRAPH ?g {"
+                                        "    ?u a nie:InformationElement ;"
+                                        "      nie:isStoredAs ?url ;"
+                                        "      fts:match \"%s\" ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(?urn) "
                                         "OFFSET %d "
@@ -1016,11 +1074,15 @@ get_files (TrackerSparqlConnection *connection,
                                         search_offset,
                                         limit_str);
        } else {
-               query = g_strdup_printf ("SELECT ?u ?url "
+               query = g_strdup_printf ("SELECT DISTINCT ?u ?url "
                                         "WHERE { "
-                                        "  ?u a nie:InformationElement ;"
-                                        "     nie:isStoredAs ?url ."
-                                        "  %s"
+                                        "  GRAPH ?g {"
+                                        "    ?u a nie:InformationElement ;"
+                                        "      nie:isStoredAs ?url ."
+                                        "  }"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    %s"
+                                        "  }"
                                         "} "
                                         "ORDER BY ASC(?urn) "
                                         "OFFSET %d "
@@ -1058,18 +1120,20 @@ get_folders (TrackerSparqlConnection *connection,
        else
                limit_str = g_strdup_printf (" ");
 
-       show_all_str = show_all ? "" : "?u nie:isStoredAs/nie:dataSource/tracker:available true . ";
+       show_all_str = show_all ? "" : "?url nie:dataSource/tracker:available true . ";
        fts = get_fts_string (search_terms, use_or_operator);
 
        if (fts) {
-               query = g_strdup_printf ("SELECT ?u nie:isStoredAs(?u) "
+               query = g_strdup_printf ("SELECT ?u ?url "
                                         "WHERE { "
-                                        "  ?u a nfo:Folder ;"
-                                        "  nie:isStoredAs ?url ."
-                                        "  ?url fts:match \"%s\" ."
-                                        "  %s"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    ?u a nfo:Folder ;"
+                                        "       nie:isStoredAs ?url ."
+                                        "    ?url fts:match \"%s\" ."
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?u)) "
+                                        "ORDER BY ASC(?url) "
                                         "OFFSET %d "
                                         "%s",
                                         fts,
@@ -1077,12 +1141,15 @@ get_folders (TrackerSparqlConnection *connection,
                                         search_offset,
                                         limit_str);
        } else {
-               query = g_strdup_printf ("SELECT ?u nie:isStoredAs(?u) "
+               query = g_strdup_printf ("SELECT ?u ?url "
                                         "WHERE { "
-                                        "  ?u a nfo:Folder ."
-                                        "  %s"
+                                        "  GRAPH tracker:FileSystem {"
+                                        "    ?u a nfo:Folder ;"
+                                        "       nie:isStoredAs ?url ."
+                                        "    %s"
+                                        "  }"
                                         "} "
-                                        "ORDER BY ASC(nie:isStoredAs(?u)) "
+                                        "ORDER BY ASC(?url) "
                                         "OFFSET %d "
                                         "%s",
                                         show_all_str,
@@ -1111,57 +1178,107 @@ get_all_by_search (TrackerSparqlConnection *connection,
        TrackerSparqlCursor *cursor;
        gchar *fts;
        gchar *query;
-       const gchar *show_all_str;
-       gchar *limit_str;
 
        fts = get_fts_string (search_words, use_or_operator);
        if (!fts) {
                return FALSE;
        }
 
-       if (search_limit != -1)
-               limit_str = g_strdup_printf ("LIMIT %d", search_limit);
-       else
-               limit_str = g_strdup_printf (" ");
-
-       show_all_str = show_all ? "" : "?s nie:isStoredAs/nie:dataSource/tracker:available | 
nie:dataSource/tracker:available true . ";
+       if (search_limit < 0)
+               search_limit = G_MAXINT;
 
        if (details) {
-               query = g_strdup_printf ("SELECT tracker:coalesce (nie:isStoredAs (?s), ?s) nie:mimeType (?s) 
?type fts:snippet(?s, \"%s\", \"%s\") "
+               query = g_strdup_printf ("SELECT ?uri ?mimetype ?type ?snippet "
                                         "WHERE {"
-                                        "  ?s fts:match \"%s\" ;"
-                                        "  rdf:type ?type ."
-                                        "  %s"
+                                        "  {"
+                                        "    SELECT (?s AS ?uri) ?mimetype ?type (fts:snippet(?s, \"%s\", 
\"%s\") AS ?snippet) {"
+                                        "      GRAPH tracker:FileSystem {"
+                                        "        ?s a nfo:FileDataObject ;"
+                                        "           fts:match \"%s\" ;"
+                                        "           rdf:type ?type ;"
+                                        "           nie:dataSource ?ds ."
+                                        "         ?ie nie:isStoredAs ?s;"
+                                        "           nie:mimeType ?mimetype ."
+                                        "        OPTIONAL { ?ds tracker:available ?available } ."
+                                        "        FILTER (IF (%s, true, ?available)) ."
+                                        "      }"
+                                        "    }"
+                                        "  } UNION {"
+                                        "    SELECT ?uri ?mimetype ?type (fts:snippet(?s, \"%s\", \"%s\") AS 
?snippet) {"
+                                        "      GRAPH ?g {"
+                                        "        ?s a nie:InformationElement ;"
+                                        "           fts:match \"%s\" ;"
+                                        "           rdf:type ?type ;"
+                                        "           nie:mimeType ?mimetype ;"
+                                        "           nie:isStoredAs ?uri ."
+                                        "      }"
+                                        "      GRAPH tracker:FileSystem {"
+                                        "        ?uri nie:dataSource ?ds ."
+                                        "        OPTIONAL { ?ds tracker:available ?available } ."
+                                        "        FILTER (IF (%s, true, ?available)) ."
+                                        "      }"
+                                        "    }"
+                                        "  }"
                                         "} "
-                                        "GROUP BY nie:isStoredAs(?s) "
-                                        "ORDER BY nie:isStoredAs(?s) "
-                                        "OFFSET %d"
-                                        "%s",
+                                        "GROUP BY ?uri "
+                                        "ORDER BY ?uri "
+                                        "OFFSET %d "
+                                        "LIMIT %d",
                                         disable_color ? "" : SNIPPET_BEGIN,
                                         disable_color ? "" : SNIPPET_END,
                                         fts,
-                                        show_all_str,
+                                        show_all ? "true" : "false",
+                                        disable_color ? "" : SNIPPET_BEGIN,
+                                        disable_color ? "" : SNIPPET_END,
+                                        fts,
+                                        show_all ? "true" : "false",
                                         search_offset,
-                                        limit_str);
+                                        search_limit);
        } else {
-               query = g_strdup_printf ("SELECT tracker:coalesce (nie:isStoredAs (?s), ?s) fts:snippet(?s, 
\"%s\", \"%s\") "
+               query = g_strdup_printf ("SELECT ?uri SAMPLE(?snippet) "
                                         "WHERE {"
-                                        "  ?s fts:match \"%s\" ."
-                                        "  %s"
+                                        "  {"
+                                        "    SELECT (?s AS ?uri) (fts:snippet(?s, \"%s\", \"%s\") AS 
?snippet) {"
+                                        "      GRAPH tracker:FileSystem {"
+                                        "        ?s a nfo:FileDataObject ;"
+                                        "           fts:match \"%s\" ;"
+                                        "           nie:dataSource ?ds ."
+                                        "        OPTIONAL { ?ds tracker:available ?available } ."
+                                        "        FILTER (IF (%s, true, ?available))"
+                                        "      }"
+                                        "    }"
+                                        "  } UNION {"
+                                        "    SELECT ?uri (fts:snippet(?s, \"%s\", \"%s\") AS ?snippet) {"
+                                        "      GRAPH ?g {"
+                                        "        ?s a nie:InformationElement ;"
+                                        "           fts:match \"%s\" ;"
+                                        "           nie:isStoredAs ?uri ."
+                                        "      }"
+                                        "      GRAPH tracker:FileSystem {"
+                                        "        ?uri nie:dataSource ?ds ."
+                                        "        OPTIONAL { ?ds tracker:available ?available } ."
+                                        "        FILTER (IF (%s, true, ?available))"
+                                        "      }"
+                                        "    }"
+                                        "  }"
                                         "} "
-                                        "ORDER BY nie:isStoredAs(?s) "
+                                        "GROUP BY ?uri "
+                                        "ORDER BY ?uri "
                                         "OFFSET %d "
-                                        "%s",
+                                        "LIMIT %d",
                                         disable_color ? "" : SNIPPET_BEGIN,
                                         disable_color ? "" : SNIPPET_END,
                                         fts,
-                                        show_all_str,
+                                        show_all ? "true" : "false",
+                                        disable_color ? "" : SNIPPET_BEGIN,
+                                        disable_color ? "" : SNIPPET_END,
+                                        fts,
+                                        show_all ? "true" : "false",
                                         search_offset,
-                                        limit_str);
+                                        search_limit);
        }
 
        g_free (fts);
-       g_free (limit_str);
 
        cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
        g_free (query);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]