Knowledgebase Home Page  >  SearchUnit
Search the Knowledge Base
Making all searches wildcard by default (C#, VB).
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=224&catId=54

Options

Print this page
Email this to a friend

By default, users can do wildcard searches using the * operator. 

 Eg.

“fore*” matches all word starting in “fore”

“*ing” matches all words ending in “ing”

“*end*” matches all words containing “end”

 If however you want all searches to use one of the wildcard types (in this article it will be the first type, above), then the following code can be used to achieve this.  It’s important to note that; wildcard searches are slower and that they can produce unexpected results, because they match a lot more words.

 VB.NET STEPS

 1. Add this class to the project/codebehind

 Class CSearchAgent

    Inherits Keyoti.SearchEngine.Search.SearchAgent

 

    Sub New(ByVal key As String, ByRef conf As Keyoti.SearchEngine.Configuration)

        MyBase.New(key, conf)

    End Sub

 

    Public Overrides Function CreateWholeExpressionGroup(ByVal queryString As String) As Keyoti.SearchEngine.Search.GroupElement

        Dim group As Keyoti.SearchEngine.Search.GroupElement = MyBase.CreateWholeExpressionGroup(queryString)

        ChangeWordElementsToWildcard(group.ChildElements)

        Return group

    End Function

 

    Sub ChangeWordElementsToWildcard(ByRef elements As ArrayList)

        Dim element As Keyoti.SearchEngine.Search.Element

 

 

        For i As Integer = 0 To elements.Count - 1

            element = elements(i)

            If TypeOf element Is Keyoti.SearchEngine.Search.GroupElement Then

                Dim group As Keyoti.SearchEngine.Search.GroupElement = CType(element, Keyoti.SearchEngine.Search.GroupElement)

                ChangeWordElementsToWildcard(group.ChildElements)

            ElseIf TypeOf element Is Keyoti.SearchEngine.Search.WordElement Then

                  'append * to end of word to get starts with wildcard

                elements(i) = New Keyoti.SearchEngine.Search.WildcardWordElement(element.Content & "*", Configuration)

            End If

 

        Next

    End Sub

End Class

 

2. Use it by setting SearchAgent in the Page_Load on the search page

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

         'Dim licensekey As String = "5651665D6359655E67586968605965374C38383D373D3F3A423C42414343414647474C6" '30 day lite key

        Dim licensekey As String = "59546960665C68616A5B6C6B635C53253D3B3C413A40423D453F45444646444C4A4B508" '30 day pro key

        SearchResult1.SearchAgent = New CSearchAgent(licensekey, SearchResult1.Configuration)

      End Sub

 

C# STEPS

 1. Add this class to the project/codebehind

 

class CSearchAgent : Keyoti.SearchEngine.Search.SearchAgent

{

     public CSearchAgent(string key, Keyoti.SearchEngine.Configuration conf) : base(key, conf)

    {

    }

     public override Keyoti.SearchEngine.Search.GroupElement CreateWholeExpressionGroup(string queryString)

    {

        Keyoti.SearchEngine.Search.GroupElement group = base.CreateWholeExpressionGroup(queryString);

        ChangeWordElementsToWildcard(group.ChildElements);

        return group;

    }

     void ChangeWordElementsToWildcard(System.Collections.ArrayList elements)

    {

        Keyoti.SearchEngine.Search.Element element;

        for (int i = 0; i< elements.Count ; i++)

        {

            element = elements[i] as Keyoti.SearchEngine.Search.Element;

            if (element is Keyoti.SearchEngine.Search.GroupElement)

            {

                Keyoti.SearchEngine.Search.GroupElement group = ((Keyoti.SearchEngine.Search.GroupElement)(element));

                ChangeWordElementsToWildcard(group.ChildElements);

            }

            else if (element is Keyoti.SearchEngine.Search.WordElement)

            {

                  //append * to end of word to get starts with wildcard

                elements[i] = new Keyoti.SearchEngine.Search.WildcardWordElement((element.Content + "*"), Configuration);

            }

        }

    }

}

 

2. Use it by setting SearchAgent in the Page_Load on the search page

    protected void Page_Load(object sender, EventArgs e)

    {

        //string licensekey = "5651665D6359655E67586968605965374C38383D373D3F3A423C42414343414647474C6";//30 day lite key

        string licensekey = "59546960665C68616A5B6C6B635C53253D3B3C413A40423D453F45444646444C4A4B508";//30 day pro key

        SearchResult1.SearchAgent = new CSearchAgent(licensekey, SearchResult1.Configuration);

    }

 


Related Questions:

Attachments:

No attachments were found.