Thursday, April 16, 2009

Sort by Date for Google Site Search

Google Searches by Relevance by Default. One of the things that was on my TODO list was to have the articles search sorted by date, irrespective of the article relevance. So I had a radiobutton list which lets the users to choose if they want the results by relevance, or by date. There were not really lot of information about it on google blogs/forums, and nobody was sure whats the correct way to do it. After some clicks and head bashings, I found out the closest way to get to the results to sort by date is to have its own Annotation file and context file. So I logged into my google account and went to the custom search section, where I had customized my google site search. On the advanced Tab, I downloaded the Annotation file and the context file.

The Idea is that, when the user opts to sort by date, call these customized xml files to show results sorted out by date.

In the context file you will find the following tag

<Label name="_cse_bolrua_bzhw" mode="FILTER" />
<Label name="_cse_exclude_bolrua_bzhw" mode="ELIMINATE" />

Add the following Label to the xml file and save it in your website directory.

<Label name="recent4" mode="BOOST"/>

This tag does the trick.

Also Just before the </GoogleCustomizations> tag, add the following line:

<Include type="Annotations" href=""/>

This references the annotations file that you downloaded from the "Advanced Console"

There wont be any change in the annotations file, except that a copy will reside on the server. Make sure that when you change the site restrictions (Adding/Removing folders/files for site search), compare your downloaded version with the one on the Custom search.

Based upon the Radio Button selection, just select the annotation file:

if (rbSearchby.SelectedValue == "r") //Search by Relevance
Response.Redirect(GetRoot() + "content/googlesearch.aspx?cx=01054907335329%3Abolrua_bzhw&cof=FORID%3A11&ie=UTF-8&q=" + HttpUtility.UrlEncode(SearchBox_TextBox.Text) + "&sa=Search");
//Search by Date
Response.Redirect(GetRoot() + "content/googlesearch.aspx?" + HttpUtility.UrlEncode(SearchBox_TextBox.Text) + "&sa=Search");

No comments: