Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • 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).
Code Block
languagec#
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.

...