Show language: C# VB.NET Both

AutoComplete Customization

The AutoComplete feature of SearchBox can be customized through the use of a plug-in, or with Javascript on the client side.

Plug-in (Server Side)

AutoComplete Settings

For maximum efficiency plug-in DLLs are not loaded unless SearchBox.AutoCompleteQueryLoadPlugin is set "true". Set this property in the ASPX or codebehind for the page which uses SearchBox, in order to be able to modify the autocomplete suggestions.

Creating The Plug-in

A Plug-in project can quickly be generated from the 'Plug-ins' tab in the Index Management Tool, in the project you will find code which handles the AutoCompleteSuggestionsGenerated event. For more information on plug-ins, see the overview of the Central Event System in general.

A plug-in class will subscribe to the Action event and process ActionName.AutoCompleteSuggestionsGenerated. The associated ActionData.Data is of type ArrayList, and contains a list of SearchBoxAutoCompleteHelper.SuggestionResultPair objects. The original text entered by the user is accessible through 'sender' (see below)

C#
private void CentralEventDispatcher_Action(object sender, Keyoti.SearchEngine.Events.ActionEventArgs e)
{
    if(e.ActionData.Name== ActionName.AutoCompleteSuggestionsGenerated)
    {
		ArrayList suggestionPairs = e.ActionData.Data as ArrayList;
		Keyoti.SearchEngine.Web.SearchBoxAutoCompleteHelper helper = 
			sender as Keyoti.SearchEngine.Web.SearchBoxAutoCompleteHelper;
		string textEnteredByUser = helper.Query;
		string[] wordsInText = helper.Segments;
		suggestionPairs.Add(new 
			Keyoti.SearchEngine.Web.SearchBoxAutoCompleteHelper.SuggestionResultPair("testQuery", 99));
    } 
}
VB.NET
Private Sub CentralEventDispatcher_Action(ByVal sender As Object,_
					ByVal e As Keyoti.SearchEngine.Events.ActionEventArgs)        
	If (e.ActionData.Name = ActionName.AutoCompleteSuggestionsGenerated) Then
		 Dim suggestionPairs As ArrayList = CType(e.ActionData.Data,ArrayList)
		 Dim helper As Keyoti.SearchEngine.Web.SearchBoxAutoCompleteHelper = _
			CType(sender,Keyoti.SearchEngine.Web.SearchBoxAutoCompleteHelper)
		 Dim textEnteredByUser As String = helper.Query
		 Dim wordsInText() As String = helper.Segments
		 suggestionPairs.Add(New _
			Keyoti.SearchEngine.Web.SearchBoxAutoCompleteHelper.SuggestionResultPair("testQuery", 99))
	 End If
End Sub

In this example the suggestionPairs list is modified with a new suggestion 'testQuery' added to the end. The list can be cleared, or items removed too. Use 'textEnteredByUser' or 'wordsInText' to determine what the user has typed.

Javascript (Client Side)

On the page with the SearchBox, add a function called sew_OnAutoCompleteSuggestionsGenerated, this will be automatically called by the autocomplete engine when suggestions are generated, allowing for modification of the suggestion list or items in the list.

Javascript
function sew_OnAutoCompleteSuggestionsGenerated(query, suggestions) {

	//how to iterate the generated suggestions
	for (var i = 0; i < suggestions.length; i++) {
		var suggestionText = suggestions[i][0];
		//# of results, will be -1 if AutoCompleteQueryShowNumberOfResults==false
		var suggestionNumberOfResults = suggestions[i][1]; 

		//modify the text?
		suggestions[i][0] = suggestions[i][0] + "*"; //turn each suggestion into a wildcard
	}
	//add a bonus suggestion, with '10' predicted results
	suggestions[suggestions.length]=["bonus", 10];
}