Performing a search

Performing a search — How to do a simple search using the user search language

The Program

This example demonstrates how to execute a simple search, defined in the user search language, printing out the results.

It is important to note that this program will just print out all results and then hang. To keep it a simple example we wont detect when we are done printing hits. This could be done by registering a callback for the "done" signal on the search and noting the hit count by calling xesam_g_search_get_num_found().

#include <glib.h>
#include <xesam-glib.h>

static void
print_hit (XesamGHit *hit)
{
	g_printf ("Hit id: %d:\n", xesam_g_hit_get_id (hit));
	g_printf ("  Title: %s\n", xesam_g_hit_get_string(hit, "xesam:title"));
	g_printf ("    Url: %s\n", xesam_g_hit_get_string(hit, "xesam:url"));
}

static void
hits_added_cb (XesamGSearch *search, XesamGHits *hits, gpointer userdata)
{
	XesamGHit	*hit;
	
	hit = NULL;
	while ((hit = xesam_g_hits_next (hits, hit))) {
		print_hit (hit);
		g_printf ("\n");
	}
}

int
main (int argc, char* argv[])
{
	GMainLoop		*mainloop;
	XesamGSearcher	*searcher;
	XesamGSession	*session;
	XesamGSearch	*search;
	int				i;

	/* This will be used to define which fields to retrieve from the searcher */
	gchar			*hit_fields[3] = {"xesam:title", "xesam:url", NULL};
	
	g_type_init ();
	
	mainloop = g_main_loop_new (NULL,FALSE);
	searcher = XESAM_G_SEARCHER(xesam_g_dbus_searcher_new_default ());
	session = xesam_g_session_new (searcher);
	
	/* Set the hit-fields session property, so that we retrieve both 
	 * title and Url of the hits */
	g_object_set (session, "hit-fields", hit_fields, NULL);
	
	/* Create a search for the words "hello world". We wont get any results
	 * before we call start() on it */
	search = xesam_g_session_new_search_from_text (session, "hello world");
	
	g_signal_connect (search, "hits-ready", G_CALLBACK(hits_added_cb), mainloop);
	
	/* Ok, now that the signals are set up we can start the search */
	xesam_g_search_start (search);
	
	g_main_loop_run (mainloop);
	
	/* In an app we would need to unref all objects here, but since the
	 * program terminates now, we don't have to */
	
	return 0;
}

Compilation

Save the code in a file called xesam-simple-search.c and compile it with

gcc $(pkg-config --libs --cflags xesam-glib) xesam-simple-search.c -o xesam-simple-search

Test Run

You can take the compiled program for a test run simply by running

./xesam-simple-search

An obvious improvement would be to make the program read the command line arguments and search for those instead of the static string "hello world".

When running the program you will see something like

$ ./xesam-simple-search
Search done
Hit id: 0:
  Title: 
    Url: file:///home/mikkel/Projects/hello.c

Hit id: 1:
  Title: Introduction to C
    Url: /home/mikkel/Documents/c_tutorial.pdf

Hit id: 2:
  Title: My Computer and Me, a Hackers Diary
    Url: /home/mikkel/Documents/dont_readme.html

 ... probably more like this ...