3

目前,我们的组织正在使用 Google 自定义搜索引擎来提供自动建议,并且我们在 CSE 中配置了大约 3 个细化标签。以前,我们使用 WebSearch 和 SearchControl,WebSearch 有一个 setSiteRestriction 方法,它允许我们专门选择一个细化标签:- http://code.google.com/apis/websearch/docs/reference.html#_class_GwebSearch

之前的代码示例:

var searchControl = new google.search.SearchControl();

var webSearch = new google.search.WebSearch();

//Refinement allows us to tell Google which specific sites to search
var refinement="Support";    
//filter custom search and currently there are 3 refinements
(some other variables declaration here including 'product')
switch(product)

{

    case "10000":
        refinement = "Support1";
        break;

    case "10200":
        refinement = "Support1";
        break;

    case "10001":
        refinement = "Support2";
        break;

    default:
        break;
}

/*this is the code to fill in the custom search. The refinement was set above - either "Support", "Support1", or "Support2".*/
webSearch.setSiteRestriction('cseId', refinement);
......  

但是,目前我们正在迁移到 CustomSearchControl 工具来替换已弃用的 WebSearch,但显然我找不到任何方法来根据 switch case 语句的值专门选择细化标签。这里需要立即帮助,如果有相关文档可以指点我,将不胜感激。谢谢!:)

4

2 回答 2

3

使用 customSearchControl 时,可以在选项中设置细化标签。这会

a) 不要将搜索中的其他优化限制为您可能添加的关键字(“更多:”+优化),并且

b) 还突出显示细化选项卡以告诉用户您代表他们做了什么。

var customSearchOptions =
    { 'defaultToRefinement' : 'refinement_label_name' };

  var customSearchControl =
    new google.search.CustomSearchControl('YOUR_CSE_ID', customSearchOptions);

自定义搜索元素JavaScript API 参考中提到了 defaultToRefinement 参数。

之前,这里介绍过这个答案。

于 2012-03-14T16:38:15.147 回答
1

得到了答案。将以下行附加到代码中:

var customSearchControl = new google.search.CustomSearchControl(cseId);
customSearchControl.setSearchStartingCallback(this, function(control, searcher, query) 
{
      searcher.setQueryAddition('more:' + refinement);
});
于 2011-05-28T13:49:34.317 回答