help.axcms.netAxinom Logo
Save Save Chapter Send Feedback

AxCMS.net - How to implement search in your project

This step by step guide shows you how to implement a search form using two different ways.

Below you find the steps necessary to write a simple search form, containing a textbox for a search term, a search-button and optionally a drop down list for element types. Naming conventions of Axinom are used.

  1. Create new template page or web control in your template project.
  2. Add a TextBox named _searchTextBox and a Button named _searchButton to the design.
  3. If you want the user to choose the element type where to search, add the DropDownList control named _typeDropDown and fill it with the element types to search in.
  4. Add OnClick event to the _searchButton, name it _searchButton_Click.
  5. Go to the code of this event. This event should start the search.
  6. For the search, use the Axinom.AECMS.Index.AxIndexSearch class. First, create the instance of this class:
    AxIndexSearch searcher = new AxIndexSearch(Configuration.ConnectionManager);
  7. Then, create the search parameters object using the string entered to _searchTextBox:
    AxSearchParams searchParams = new AxSearchParams((short)ElementTypes.Page, _searchTextBox.Text);
    In this example, the search will be done for pages.
    If you gave the user possibility to choose element type, use selected value of
    _typeDropDown as the first parameter.
  8. Now, run the search:
    ElementCollection results = searcher.DoSearch(searchParams);
  9. The DoSearch method returns ElementCollection as result. Since you know the type for which the search was done, you can easily load the additional information about the search result items and display them. For example, for Page element type:
AxPageAdapter pageAdapter = new AxPageAdapter();
foreach (IClassifiable result in results)
{
    AxPage page = pageAdapter.Load((int) element.ID);
    Response.Write(page.Name + "<br>");
}

The complete source of the event

private void _searchButton_Click(object sender, EventArgs e)
{
   AxIndexSearch searcher = new AxIndexSearch(Configuration.ConnectionManager);
   AxSearchParams searchParams = new AxSearchParams((short)ElementTypes.Page, _searchTextBox.Text);
   ElementCollection results = searcher.DoSearch(searchParams);
   if(results.Count==0)
   {
      Response.Write("Nothing found<br>");
   }
   else
   {
      AxPageAdapter pageAdapter = new AxPageAdapter();
      foreach (IClassifiable element in results)
      {
         AxPage page = pageAdapter.Load((int) element.ID);
         Response.Write(page.Name + "<br>");
      }
   }
}

Using additional search criteria

Somethimes you will want to use other search criteria additionaly to the search term (Category, Author, Date, etc.). For this case you have 2 strategies. The first one - to use DoSearch-method:

ElementCollection results = searcher.DoSearch(searchParams);

And then as the second step iterate through the collection, applying additional criteria.

The second strategy is, to create your own database query, which can use any filter you want, and then inject additional filter for the search term. AxIndexSearch-class offers for this purpose method QuerySearch.

Here is the source code sample doing this:

private void _searchButton_Click(object sender, EventArgs e)
{
   AxIndexSearch searcher = new AxIndexSearch(Configuration.ConnectionManager);
   AxSearchParams searchParams = new AxSearchParams((short)ElementTypes.Page, _searchTextBox.Text);

   DbSelectQuery query=new AxPageQuery();
   AxPageFilterParameters filter = new AxPageFilterParameters();
   filter.FilterMode = FilterMode.Expert;
   filter.Category = 123; // CategoryID you want to filter for
   AxPageAdapter pageAdapter = new AxPageAdapter();
   pageAdapter.ApplyFilterParameters(query, filter);
   searcher.QuerySearch(query,"T.AxID", searchParams);
   AxPageCollection results = new AxPageCollection(pageAdapter);
   results.Fill(query);
   if(results.Count==0)
   {
      Response.Write("Nothing found<br>");
   }
   else
   {
      foreach (AxPage page in results)
      {
         Response.Write(page.Name + "<br>");
      }
   }
}

Conceptually the method searcher.QuerySearch is similar to the method adapter.ApplyFilterParameters. Both of them inject some additional filter (SQL WHERE) into the given query.

Using this approach has following advantages:

  • You get your result with one database query
  • You get smaller result set
  • It is easier to code
  • Is not neccessary to load custom object for each entry in the result set.

Disadvantages of this approach:

  • More complex query
  • The index has to be in the same database with AxCMS.net

Search Syntax

The AxCMS.net search engine supports a powerful search syntax.  As a developer use AxSearchParams-class:

AxSearchParams
searchParams = new AxSearchParams((short) ElementTypes.Page, searchString);

In the searchString parameter an expression can be passed in the search syntax.

Developing a custom search form you can expose this syntax to your user, directly passing an input string to AxSearchParams constructor, or disable it by manually adding particular AxSearchParam objects to the AxSearchParams object.

AxCMS.net GUI exposes the search syntax at the following places:

  • Simple search for Pages and Documents (in the overview-pages)
  • Quick-search for Pages and Documents (at the menu line)
  • Test search (Admin/Index Search/Test)
  • Publish / Check-In Objects

See the Search Syntax under Reference for more details.