2011-05-27 72 views
3

目前,我们的组织正在使用Google自定义搜索引擎来提供自动建议,并且我们在CSE中配置了大约3个优化标签。以前,我们使用网络搜索和SearchControl,以及网页搜索有着setSiteRestriction方法,使我们能够明确选择优化标签: - http://code.google.com/apis/websearch/docs/reference.html#_class_GwebSearch如何触发特定的Google自定义搜索引擎优化标签?

前面的代码示例:

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语句的值专门选择优化标签。在这里需要立即的帮助,如果有相关的文档,你们可以指点我会非常感激。谢谢! :)

回答

1

得到答案。在代码中加入以下几行:

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

这几乎是* *工作......你可以做到这一点,或只是做 customSearchControl.execute(yourQuery +“更多:” +精) 但是,它并不突出细化标签告诉用户什么你代表他们做了。调查,也许我会提交一个错误。 – 2011-06-19 18:59:52

3

当使用customSearchControl时,可以在选项中设置优化标签。 这将

一)不限制搜索其他改进你 可能与(“更多:”添加关键字+细化),并

B)还强调 细化选项卡告诉用户你代表他们做了什么。

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

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

的defaultToRefinement参数在自定义搜索元素JavaScript API Reference提及。

此前,此答案被引入here

相关问题