Kentico CMS 7.0 Developer's Guide

Managing search indexes

Managing search indexes

Previous topic Next topic Mail us feedback on this topic!  

Managing search indexes

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 smart search index.

 

private void CreateSearchIndex()
{
    // Create new search index object
    SearchIndexInfo newIndex = new SearchIndexInfo();
 
    // Set the properties
    newIndex.IndexDisplayName = "My new index";
    newIndex.IndexName = "MyNewIndex";
    newIndex.IndexIsCommunityGroup = false;
    newIndex.IndexType = PredefinedObjectType.DOCUMENT;
    newIndex.IndexAnalyzerType = AnalyzerTypeEnum.StandardAnalyzer;
    newIndex.StopWordsFile = "";
        
    // Save the search index
    SearchIndexInfoProvider.SetSearchIndexInfo(newIndex);
}

 

The following example creates and configures the settings of a search index.

 

private bool CreateIndexSettings()
{

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

    if (index != null)
    {
        // Create new index settings
        SearchIndexSettingsInfo indexSettings = new SearchIndexSettingsInfo();
 
        // Set setting properties
        indexSettings.IncludeBlogs = true;
        indexSettings.IncludeForums = true;
        indexSettings.IncludeMessageCommunication = true;
        indexSettings.ClassNames = ""// for all document types
        indexSettings.Path = "/%";
        indexSettings.Type = SearchIndexSettingsInfo.TYPE_ALLOWED;
        indexSettings.ID = Guid.NewGuid();
 
        // Save index settings                     
        SearchIndexSettings settings = new SearchIndexSettings();
        settings.SetSearchIndexSettingsInfo(indexSettings);
        index.IndexSettings = settings;
 
        // Save to database
        SearchIndexInfoProvider.SetSearchIndexInfo(index);
 
        return true;
    }
 
    return false;
}

 

The following example gets and updates a search index.

 

private bool GetAndUpdateSearchIndex()
{

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

    if (updateIndex != null)
    {
        // Update the properties
        updateIndex.IndexDisplayName = updateIndex.IndexDisplayName.ToLower();
 
        // Save the changes
        SearchIndexInfoProvider.SetSearchIndexInfo(updateIndex);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates search indexes.

 

private bool GetAndBulkUpdateSearchIndexes()
{
    // Prepare the parameters
    string where = "IndexName LIKE N'MyNewIndex%'";
 
    // Get the data
    DataSet indexes = SearchIndexInfoProvider.GetSearchIndexes(where, null);
    if (!DataHelper.DataSourceIsEmpty(indexes))
    {
        // Loop through the individual items
        foreach (DataRow indexDr in indexes.Tables[0].Rows)
        {
            // Create object from DataRow
            SearchIndexInfo modifyIndex = new SearchIndexInfo(indexDr);

 
          // Update the properties
           modifyIndex.IndexDisplayName = modifyIndex.IndexDisplayName.ToUpper();

 
            // Save the changes
            SearchIndexInfoProvider.SetSearchIndexInfo(modifyIndex);
        }
 
        return true;
    }
 
    return false;
}

 

The following example deletes a search index.

 

private bool DeleteSearchIndex()
{

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

 
    // Delete the search index
    SearchIndexInfoProvider.DeleteSearchIndexInfo(deleteIndex);
 
    return (deleteIndex != null);
}