[tracker-miners/wip/carlosg/search-cli-queries] cli: Specify graphs in "tracker3 search" queries
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker-miners/wip/carlosg/search-cli-queries] cli: Specify graphs in "tracker3 search" queries
- Date: Sat, 6 Aug 2022 22:04:45 +0000 (UTC)
commit b800de80dbab70ef9b1b6f483e444ac7dfd4919c
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 | 291 ++++++++++++++++++++++++++++---------------
1 file changed, 194 insertions(+), 97 deletions(-)
---
diff --git a/src/tracker/tracker-search.c b/src/tracker/tracker-search.c
index c85f8c7d9..b3f1f9600 100644
--- a/src/tracker/tracker-search.c
+++ b/src/tracker/tracker-search.c
@@ -313,13 +313,16 @@ get_document_files (TrackerSparqlConnection *connection,
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\" ."
+ " %s"
+ " }"
"} "
- "ORDER BY ASC(nie:isStoredAs(?document)) "
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
disable_color ? "" : SNIPPET_BEGIN,
@@ -329,12 +332,15 @@ 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 ."
+ " %s"
+ " }"
"} "
- "ORDER BY ASC(nie:isStoredAs(?document)) "
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
show_all_str,
@@ -374,13 +380,16 @@ get_video_files (TrackerSparqlConnection *connection,
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\" ."
+ " %s"
+ " }"
"} "
- "ORDER BY ASC(nie:isStoredAs(?video)) "
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
disable_color ? "" : SNIPPET_BEGIN,
@@ -390,12 +399,15 @@ 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 ."
+ " %s"
+ " }"
"} "
- "ORDER BY ASC(nie:isStoredAs(?video)) "
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
show_all_str,
@@ -435,13 +447,16 @@ get_image_files (TrackerSparqlConnection *connection,
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\" ."
+ " %s"
+ " }"
+ "}"
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
disable_color ? "" : SNIPPET_BEGIN,
@@ -451,12 +466,15 @@ 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 ."
+ " %s"
+ " }"
+ "}"
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
show_all_str,
@@ -496,13 +514,16 @@ get_music_files (TrackerSparqlConnection *connection,
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\" ."
+ " %s"
+ " }"
"} "
- "ORDER BY ASC(nie:isStoredAs(?song)) "
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
disable_color ? "" : SNIPPET_BEGIN,
@@ -512,12 +533,15 @@ 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 ."
+ " %s"
+ " }"
"} "
- "ORDER BY ASC(nie:isStoredAs(?song)) "
+ "ORDER BY ASC(?u) "
"OFFSET %d "
"%s",
show_all_str,
@@ -557,9 +581,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 +596,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 +679,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 +693,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 +860,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 +949,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 +963,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 +1035,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 +1058,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 "
@@ -1062,14 +1108,16 @@ get_folders (TrackerSparqlConnection *connection,
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 +1125,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 +1162,103 @@ 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 ."
+ " ?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 ."
+ " ?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]