Google Product Forums

Re: Sorting dates in search results

omr Apr 18, 2012 10:39 PM
Posted in group: Google Custom Search

Categories: Creating and Editing :

(Note: I'm not a Google employee.)


If you're using standard Custom Search Element code, you can make the following simple change to enable rough sorting by approximately estimated page dates.

(Note: If you're using an implementation in which search box code is separate from results code, make this change in the results code.)

If you've recently obtained standard code from the CSE control panel, your code may contain a customSearchOptions assignment like this:

  var customSearchOptions = {};

There may be additional code on the same line, e.g.:

  var customSearchOptions = {};  var customSearchControl = new google.search.CustomSearchControl(
    ...

First let's place the second "var ..." statement on a separate line:

  var customSearchOptions = {};
  var customSearchControl = new google.search.CustomSearchControl( ...

Between those lines, add:

  customSearchOptions
    [google.search.Search.RESTRICT_EXTENDED_ARGS] =
      {'sort': 'date'};


Thus:


  var customSearchOptions = {};

  customSearchOptions
    [google.search.Search.RESTRICT_EXTENDED_ARGS] =
      {'sort': 'date'};


  var customSearchControl = new google.search.CustomSearchControl(
    ...


This quick example specifies sorting by estimated dates for all searches.  This example doesn't provide a button or menu to select sorting by date vs. relevance.

The sorting mode specified in this example uses approximately "estimated" page dates, which are not always accurate.  Consequently you may generally expect some inaccuracies in the sorting order.

With further study of documentation and further work, it is also possible to sort by publication dates that can be specified in "structured data" (such as meta-tags, PageMaps, etc.) which can be embedded in (or associated with) pages.

-- omr