Search code examples
javascriptseogoogle-custom-search

Limit date range in Google Custom Search Engine


I'm using Google Custom Search Engine (https://cse.google.com). I used the client side script which creates a search element inside my html pages. The code looks like this:

<script>
  (function() {

    var cx = 'xxxxxxxxxxxxxxxxxx:yyyyy';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:searchresults-only></gcse:searchresults-only>

It works fine, but I need to add an option for users to choose which date range they want to search for. For example 'past month' or 'past week' ...

I saw people are talking about a parameter called as_qdr which could be set to 'm' or 'w' for this purpose. The problem is, by using the default javascript code, I don't have access to specific API url to append as_qdr=m to. I couldn't find any good documentation about how to do this.

If you take a look at http://www.reuters.com/search/news?blob=test for example you'll see what I mean. There is an option to choose the date range for the result.

Is there anyone who has done this before? Thanks.


Solution

  • I ended up using their RESTful APIs. So I designed the search form myself and then query Google for the result.

    https://developers.google.com/custom-search/json-api/v1/overview