Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google custom search sort date range criteria not working

In Google Custom search we pass date range criteria in sort parameter

http://www.example.com/search?cx=xxxxxxxxxxxxxxxxxxxxx:yyyyyy_yyyyy&q=google&sort=date:r:20160304:20160505. https://developers.google.com/custom-search/docs/structured_search#restrict_to_range 

But we are not getting expected result as per the date range criteria. After debugging the code the sort parameter is not getting passed to custom search URL even when we are passing it on to our search page url.

Google Custom search URL:

https://cse.google.com/cse?q=google&r=m&cx=xxxxxxxxxxxxxxxxxxxxx:yyyyyy_yyyyy&fexp=20606&client=google-coop&hl=en&type=0&oe=UTF-8&ie=UTF-8&format=p4&ad=p4&nocache=1081472127237880&num=0&output=uds_ads_only&source=gcsc&v=3&allwcallad=1&adext=as1%2Csr1%2Cctc1&bsl=10&u_his=50&u_tz=330&dt=1472127237882&u_w=1366&u_h=768&biw=1349&bih=233&psw=1349&psh=233&frm=0&uio=uv3st16sd13sv13lhsl1sr1cc1-&jsv=14100&rurl=http%3A%2F%2Fwww.example.com%2Fsearch%3Fcx%xxxxxxxxxxxxxxxxxxxx_yyyyy%26q%3Dgoogle%26sort%3Ddate%253Ar%253A20160304%253A20160505 

But when we try to access the direct API with sort date range parameter we are getting expected results.

https://www.googleapis.com/customsearch/v1?q=google&cx=xxxxxxxxxxxxxxxxxxxxx:yyyyyy_yyyyy&sort=date%3Ar%3A20160701%3A20160724&key=<APIKEY> 

We are using Google custom search V2 implementation.

Thanks, Anup

like image 407
Anup Khandelwal Avatar asked Sep 01 '16 06:09

Anup Khandelwal


People also ask

How do you sort Google searches by date?

To configure search results sorting:Click Search features from the left menu and open the Advanced tab. Click on Results sorting. By default, sorting by Relevance and Date will already be available. If that's all you need, just click the slider to make sure Results sorting is On.

Can you filter Google search results by date?

Users can now skip the Tools tab and narrow down results from the search bar. Google has announced a new search bar command that enables users to filter results for a desired date range. The “before:YYYY-MM-DD” and “after:YYYY-MM-DD” shortcuts yield results for before, after, and within the dates specified.


2 Answers

Try to escape all special characters which are passed as query parameters. For example

>  - &lt; <  - &gt; &  - &amp; '  - &#039; '' - &#034; 

These are encoded like this. You can escape it in two ways

  1. explicitly specifying escapeXml = "**false**" as the default value will be true.
  2. Add the url in JSP if you are using <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> and then escape by calling ${fn:escapeXml(myString)}.
like image 112
Anirudh Lakshmeesh Avatar answered Sep 22 '22 23:09

Anirudh Lakshmeesh


It seems like an issue with your url parameters encoding. It might be double encoded.

like image 38
JorgeObregon Avatar answered Sep 21 '22 23:09

JorgeObregon