Search

The search module integrates the Lucene.Net search engine into the CMS and provides a simplified set of calls for performing searches.

Configuring the module

The search module has a couple of settings:

  • ApplyToType: set this to a function of Type to bool which returns true if the content type will be searched.
  • DontRebuild: if set to true, the module will not rebuild the index when it starts up.  It's recommended to rebuild as this will fix any loss of sync between edits to content and the search index.

Configuring content types for search

Content types are configured for search by attaching the IndexAttribute to the properties of the type which should be indexed. The IndexAttribute tells the search module to index this property for search by converting it to a string. The mode parameter has the following values:

  • Textual: index the field separately as written text
  • NonTextual: index the field separately as a value which is not written text
  • Agglomerate: append the text of the field to a single agglomerated text field for the content item
  • TextualAndAgglomerated: append to the agglomerated field and index separately as well

Ensure the content type returns true when you input it to the ApplyToType function so it is included in search.

Running a search

There are two modes of running a search: an example of the more general and powerful one is shown below.

  • SearchManager.Instance.Canonicalise() reduces a search string to the form to which the search text has been reduced.  This process will do things like remove plurals, accents, reduce to lower case etc.
  • SearchSpec is a class which describes what kind of search is needed.  The most important field is GeneralSearch which is the canonicalised search string.
  • The AddGeneralSearchField lets you include another field against those against which the GeneralSearch string is matched.  You can also set a number which is a 'boost' by which the score of that matches (if there is one) is multiplied, making matches to that field more important.  Here for instance, a match to the Title receives a boost of 4 as a match to a title is more important than a match to body text.
  • Because this example is running in a context with multiple versions, before running the search the current version is set to the public version via VersionManager.Instance.PushState().  Because the search module only indexes the public version(s) of content items, this ensures that the search results comes back with these versions. See how to override the current version for more information on this.
  • The returned search results are a List<SearchResultItem>.  SearchResultItem simply includes the id of the content item (.VersionedId) and the search score (.Score).
var canonicalisedSearch = SearchManager.Instance.Canonicalise(search);
var spec = new SearchSpec { GeneralSearch = canonicalisedSearch };
spec.AddGeneralSearchField("Title", GbcSearchController.GeneralSearchTitleScore);
spec.AddGeneralSearchField("SearchIngredients", GbcSearchController.GeneralSearchSearchIngredientScore);
spec.AddGeneralSearchField("CurrentSponsor", GbcSearchController.GeneralSponsorScore);
 
List<SearchResultItem> searchResults;
VersionManager.Instance.PushState(VersioningMode.Public);
try
{
	searchResults = SearchManager.Instance.Search(spec, 0, 1000, out total);
}
finally
{
	VersionManager.Instance.PopState();
}

Running a simple search

ClientSearch.Instance.Search does a straightforward search on a search string, with a default boost of 4 for any title fields.  It returns a ClientSearchResult object, with a .Total property for the total count of results, and a .Results() method to get the results as a list of summaries of the result items in preference order, best result first.

There is also an HTTP webservice at /lynicon/search/clientsearch?search=<search string> which will return simple HTML search results.