2011-05-27 110 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

相關問題