Date range search using Google Custom Search API
Asked Answered
C

2

9

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?

Cohligan answered 4/9, 2015 at 21:37 Comment(0)
M
7

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.

Makkah answered 6/11, 2015 at 11:51 Comment(0)
E
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>"
Emptor answered 24/1, 2022 at 17:12 Comment(2)
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.Crosby
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.Crosby

© 2022 - 2024 — McMap. All rights reserved.