问题
Currently, our organization is using Google Custom Search engine to provide auto suggestion, and we have about 3 refinement labels configured in our CSE. Previously, we're using WebSearch and SearchControl, and the WebSearch has a setSiteRestriction method which allows us to specifically select a refinement label: - http://code.google.com/apis/websearch/docs/reference.html#_class_GwebSearch
Previous code example:
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);
......
However, currently we're migrating to CustomSearchControl tool to replace the deprecated WebSearch, but apparently I couldn't find any way to specifically select a refinement label based on the value of switch case statement. Immediate help needed here, and if there's a relevant docs that you guys can point me to will be much appreciated. thanks! :)
回答1:
When using customSearchControl, you can set the refinement label in the options. This will
a) not restrict other refinements in your search to the keywords you may have added with ('more: ' + refinement), and
b) also highlight the refinement tab to tell the user what you did on their behalf.
var customSearchOptions =
{ 'defaultToRefinement' : 'refinement_label_name' };
var customSearchControl =
new google.search.CustomSearchControl('YOUR_CSE_ID', customSearchOptions);
The defaultToRefinement parameter is mentioned in the Custom Search Element JavaScript API Reference.
Previously, this answer was introduced here.
回答2:
Got the answer. Appended the following lines to the code:
var customSearchControl = new google.search.CustomSearchControl(cseId);
customSearchControl.setSearchStartingCallback(this, function(control, searcher, query)
{
searcher.setQueryAddition('more:' + refinement);
});
来源:https://stackoverflow.com/questions/6154841/how-to-trigger-specific-google-custom-search-engine-refinement-label