8

I am using the Google Custom Search API to search for images. My implementation is using Java, and this is how I build my search string:

URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?"
                + "v=1.0&q=barack%20obama&userip=INSERT-USER-IP");

How would I modify the URL to limit search results, for example, to: 2014-08-15 and 2014-09-31?

Amr
  • 2,420
  • 2
  • 17
  • 26

2 Answers2

6

You can specify a date range using the sort parameter. For your example, you would add this to your query string: sort=date:r:20140815:20140931.

This is documented at https://developers.google.com/custom-search/docs/structured_data#page_dates

Also if you use Google's Java API you can use the Query class and its setSort() method rather than building the URL by hand.

2

I think the better way is to put this into query itself. Query parameter contains 'after' flag which can be used like:

https://customsearch.googleapis.com/customsearch/v1?
key=<api_key>&
cx=<search_engine_id>&
q="<your_search_word> after:<YYYY-MM-DD>"
Kebechet
  • 1,461
  • 15
  • 31
  • 1
    Interesting. You can also use `before:` One thing to note is that the `before:` parameter **is not inclusive**, while the `sort=date:r` **is inclusive**. – Ramon Dias Nov 11 '22 at 14:55
  • I tested both method and the result is almost the same. I could not find this solution on the docs, so I think the sort range is preferable. – Ramon Dias Nov 11 '22 at 15:07