Kentico CMS 6.0 Developer's Guide

Performing indexing and search actions

Performing indexing and search actions

Previous topic Next topic Mail us feedback on this topic!  

Performing indexing and search actions

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a task that rebuilds a smart search index.

 

private bool RebuildIndex()
{

  // Get the search index
  SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("MyNewIndex");

 
    if (index != null)
    {

      // Create rebuild task
      SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Rebuild, index.IndexType, null, index.IndexName);

 
        return true;
    }
 
    return false;
}

 

The following example performs a search through the content of an index and retrieves the results in a dataset.

 

private bool SearchText()
{

  // Get the search index
  SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("MyNewIndex");

 
    int numberOfResults = 0;
 
    if (index != null)
    {
        // Set the properties
        string searchText = "home";
        string path = "/%";
        string classNames = "";
        string cultureCode = "EN-US";
        string defaultCulture = CultureHelper.DefaultCulture.IetfLanguageTag;
        Lucene.Net.Search.Sort sort = SearchHelper.GetSort("##SCORE##");
        bool combineWithDefaultCulture = false;
        bool checkPermissions = false;
        bool searchInAttachments = false;
        string searchIndexes = index.IndexName;
        int displayResults = 100;
        int startingPosition = 0;
        int numberOfProcessedResults = 100;
        UserInfo userInfo = CMSContext.CurrentUser;
        string attachmentWhere = "";
        string attachmentOrderBy = "";

 
      // Get search results

      DataSet ds = SearchHelper.Search(searchText, sort, path, classNames, cultureCode, defaultCulture, combineWithDefaultCulture, checkPermissions, searchInAttachments, searchIndexes, displayResults, startingPosition, numberOfProcessedResults, userInfo, out numberOfResults, attachmentWhere, attachmentOrderBy);

 
        // If found at least one item
        if (numberOfResults > 0)
        {
            return true;
        }
    }
 
    return false;
}

 

The following example creates a task that updates the content of a search index.

 

private bool UpdateIndex()
{

  // Tree provider
  TreeProvider provider = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);
 
  // Get document of specified site, aliaspath and culture
  TreeNode node = provider.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSiteName, "/", "en-us");
 
  // If node exists
  if ((node != null) && (node.PublishedVersionExists) && (SearchIndexInfoProvider.SearchEnabled))

    {
        // Edit and save document node
        node.NodeDocType += " changed";
        node.Update();

 
      // Create update task
      SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Update, PredefinedObjectType.DOCUMENT, SearchHelper.ID_FIELD, node.GetSearchID());

 
        return true;
    }
 
    return false;
}