+notmuch_thread_results_t *
+notmuch_query_search_threads (notmuch_query_t *query)
+{
+ notmuch_thread_results_t *thread_results;
+ notmuch_message_results_t *message_results;
+ notmuch_message_t *message;
+ const char *thread_id;
+ GHashTable *seen;
+
+ thread_results = talloc (query, notmuch_thread_results_t);
+ if (thread_results == NULL)
+ return NULL;
+
+ thread_results->notmuch = query->notmuch;
+ thread_results->thread_ids = g_ptr_array_new ();
+ thread_results->index = 0;
+
+ seen = g_hash_table_new_full (g_str_hash, g_str_equal,
+ free, NULL);
+
+ for (message_results = notmuch_query_search_messages (query);
+ notmuch_message_results_has_more (message_results);
+ notmuch_message_results_advance (message_results))
+ {
+ message = notmuch_message_results_get (message_results);
+ thread_id = notmuch_message_get_thread_id (message);
+
+ if (g_hash_table_lookup_extended (seen,
+ thread_id, NULL, NULL))
+ {
+ continue;
+ }
+
+ g_hash_table_insert (seen, xstrdup (thread_id), NULL);
+
+ g_ptr_array_add (thread_results->thread_ids,
+ talloc_strdup (thread_results, thread_id));
+ }
+
+ g_hash_table_unref (seen);
+
+ return thread_results;
+}
+