Hi,
Im implemening search for my site using google site search API. We use a search button and paging control for our search page, and on search button event handler i create the url, exceute the request and get xml response as follows,
private string urlFormat = "https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}&alt=atom&start={3}";
int startIndex = 1;
try
{
string url = string.Format(this.urlFormat,
HttpUtility.UrlEncode("AIzaSyA4TJfARVSIT6R00f8Eavs 51BUcuv1K1Xc"),
HttpUtility.UrlEncode("009064404879215115542:vqh-zxduchq"),
HttpUtility.UrlEncode("privacy"),
HttpUtility.UrlEncode(startIndex.ToString())
);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.Accept = "application/json";
request.ContentType = "application/json; charset=utf-8";
// Execute the request and obtain the response stream
WebResponse response = request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
this.Output.Text = sr.ReadToEnd();
}
When user clicks on the any of the pagenumber links, we execute the same above code, changing the startindex as
startIndex = (PgNo * PageCount) – 9
ex: if user clicks the pagenumber 4, i calculate the start index as (4 * 10) – 9 = 31
when i execute the results first time(with start vlaue as 1, it shows total results as 80 and when i click on 5th page(start index 41), it shows only 2 results, concluding the actual total results are only 42.
Please help with these wierd totalresults..im attaching my response here..
<?xml version="1.0" encoding="UTF-8"?>
<feed gd:kind="customsearch#search" xmlns="http://www.w3.org/2005/Atom" xmlns:cse="http://schemas.google.com/cseapi/2010" xmlns:gd="http://schemas.google.com/g/2005" xmlnspensearch="http://a9.com/-/spec/opensearch/1.1/">
<title>Google Custom Search - privacy</title>
<id>tag:www.googleapis.com,2010-09-29:/customsearch/v1?q=privacy&cx=009064404879215115542:vqh-zxduchq&num=2&start=41&safe=off</id>
<author>
<name>Dev test - Google Custom Search</name>
</author>
<updated>2011-06-15T21:13:22.903Z</updated>
<opensearch:Url type="application/atom+xml" template="https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={sta rtIndex?}&hr={language?}&safe={cse:safe?}& amp;cx={cse:cx?}&cref={cse:cref?}&sort={cs e:sort?}&alt=atom"/>
<opensearch:Query role="request" title="Google Custom Search - privacy" totalResults="42" searchTerms="privacy" count="2" startIndex="41" inputEncoding="utf8" outputEncoding="utf8" cse:safe="off" cse:cx="009064404879215115542:vqh-zxduchq"/>
<opensearch:Query role="csereviousPage" title="Google Custom Search - privacy" totalResults="42" searchTerms="privacy" count="10" startIndex="31" inputEncoding="utf8" outputEncoding="utf8" cse:safe="off" cse:cx="009064404879215115542:vqh-zxduchq"/>