过滤搜索结果仅适用于PNG图像

时间:2013-03-13 03:03:02

标签: javascript image api google-custom-search

我从Google文档中复制了大部分代码,然后使用文档设置过滤器。

  • 我没有收到任何错误
  • 但是FILETYPE_PNG无效(the filetype never gets restricted)
我用过 https://developers.google.com/custom-search/docs/structured_search#filetype

有人知道代码有什么问题吗?

我也尝试过searcher.execute("Kobe Bryant"); - 但它仍然不仅限于PNG。

google.load('search', '1', {language: 'en', style: google.loader.themes.MINIMALIST});
google.setOnLoadCallback(function() {
  var customSearchOptions = {};
  var orderByOptions = {};
  orderByOptions['keys'] = [{label: 'Relevance', key: ''} , {label: 'Date', key: 'date'}];
  customSearchOptions['enableOrderBy'] = true;
  customSearchOptions['orderByOptions'] = orderByOptions;
  var imageSearchOptions = {};
  //imageSearchOptions['layout'] = LAYOUT_POPUP;  -- layout popup causing errors for some reason
  customSearchOptions['enableImageSearch'] = true;
  customSearchOptions['disableWebSearch'] = true;
  var customSearchControl =   new google.search.CustomSearchControl('Youaintfindingoutwhatthisis', customSearchOptions);
  customSearchControl.setResultSetSize(google.search.Search.SMALL_RESULTSET);

  var searcher = customSearchControl.getImageSearcher();
  searcher.setRestriction(
    customSearchControl.getImageSearcher.RESTRICT_FILETYPE,
    customSearchControl.getImageSearcher.FILETYPE_PNG
  );

  var options = new google.search.DrawOptions();
  options.setAutoComplete(true);
  customSearchControl.draw('cse', options);
}, true);

更新

  • 请参阅下面的答案

  • 仍然不知道LAYOUT_POPUP是什么 - 我在这里得到一个未定义的错误

1 个答案:

答案 0 :(得分:1)

好的,我明白了。

文档有点误导。

您需要以下代码才能使过滤工作:

customSearchControl.setSearchStartingCallback(
  this, function(control, searcher, query) {
      searcher.setQueryAddition("filetype:png OR filetype:PNG");
  }
);

将其放在js文件的末尾。希望这可以帮助其他人在文档中苦苦挣扎。

相关问题