如何触发特定的Google自定义搜索引擎细化标签?

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语句的值特定选择一个细化标签。这里需要立即帮助,如果有相关文档可以指引给我将不胜感激。谢谢!:)

2个回答

3
当使用customSearchControl时,您可以在选项中设置细化标签。 这将a)不限制您搜索中的其他细化内容到您可能已添加的关键字('more:'+细化),并且b)还会突出显示细化选项卡,以告诉用户您代表他们做了什么。
var customSearchOptions =
    { 'defaultToRefinement' : 'refinement_label_name' };

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

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

之前,这个问题已经在这里介绍过了。


1

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

var customSearchControl = new google.search.CustomSearchControl(cseId);
customSearchControl.setSearchStartingCallback(this, function(control, searcher, query) 
{
      searcher.setQueryAddition('more:' + refinement);
});

这个几乎有效...你可以这样做或者只需执行customSearchControl.execute(yourQuery + ' more:' + refinement)。然而,它没有突出显示细化选项卡以告诉用户代表他们所做的操作。正在调查中,也许我会提交一个漏洞报告。 - Tim Bray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接