3

In normal Google search, I can restrict search results to a specific date range using Tools:

Search in Google site

But when I use the same restriction with Google custom search API, all results are coming instead of the specific date range.

Suppose I want search results published only on yesterday. Although I specify the date range in the parameters, all results are being shown. But it's supposed to return no results even if the news published earlier, not yesterday.

I have seen this answer:
Specifying a Date Range in Google Custom Search api

but it doesn't work.

Implementation of google custom search

I want a solution, if it is possible or not to restrict search result to a specific date range. Do I need to change anything in my API dashboard?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Mutasim Sadi
  • 121
  • 1
  • 4

2 Answers2

2

Just use Google's operators before: and after:.

0

After some testing, this is what the \v1\ version of GOOGLE CSE expects you to submit in the query. The structure for limiting results to a date range is:

result = service.cse().list({q:"feedbacklabs",sort:"date:r:20160101:20190101"}).execute()

In python, this is what I do:

# this is in a class function.
from googleapiclient.discovery import build
service = build("customsearch", "v1", developerKey=API_KEY)
# you need an API_KEY for billing. Also note that I've created a 
# custom search engine with the restriction on sites to be none, so 
# essentially searches all of the Internet - but the google docs don't 
# tell you that is an option. I feed it my params like this:

params = dict(
        q=query_string,
        cx=self.search_engine_id, # use google console to build this
        exactTerms=exactTerms, # results must have this word
        linkSite=linkSite, # results must contain a link to this site
        sort=sort) # date range
result = service.cse().list(**params).execute()
# documented here: https://developers.google.com/custom-search/docs/structured_search#restrict-to-range

Also note that these date restrictions are not very sensitive. It's fine if you want stuff from last week or last month, but if you compare one year ago to two years ago, you essentially get the same estimated number of total search results, rounded off to the nearest hundred.

Marc Maxmeister
  • 4,191
  • 4
  • 40
  • 54