Smart search


List of examples:

Search indexes

Creating a search index



// Creates a new search index object
SearchIndexInfo newIndex = new SearchIndexInfo();

/* Sets the search index properties
 * Choose one of the following values for the 'IndexProvider' property:
 * Azure Cognitive Search: SearchIndexInfo.AZURE_SEARCH_PROVIDER
 * Locally stored search index: SearchIndexInfo.LUCENE_SEARCH_PROVIDER
*/
newIndex.IndexProvider = SearchIndexInfo.LUCENE_SEARCH_PROVIDER;

newIndex.IndexDisplayName = "New index";
newIndex.IndexName = "NewIndex";
newIndex.IndexAnalyzerType = SearchAnalyzerTypeEnum.StandardAnalyzer;
newIndex.StopWordsFile = "";

/* Sets the index type to Pages
 * The possible IndexType values are:
 * Pages: TreeNode.OBJECT_TYPE
 * Pages crawler: SearchHelper.DOCUMENTS_CRAWLER_INDEX
 * Custom tables: CustomTableInfo.OBJECT_TYPE_CUSTOMTABLE
 * Users: UserInfo.OBJECT_TYPE
 * On-line forms: SearchHelper.ONLINEFORMINDEX
 * General: SearchHelper.GENERALINDEX
 * Custom: SearchHelper.CUSTOM_SEARCH_INDEX
*/
newIndex.IndexType = TreeNode.OBJECT_TYPE;

// Saves the search index to the database
SearchIndexInfoProvider.SetSearchIndexInfo(newIndex);

> Back to list of examples

Updating a search index



// Gets the search index
SearchIndexInfo updateIndex = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");
if (updateIndex != null)
{
    // Updates the index properties
    updateIndex.IndexDisplayName = updateIndex.IndexDisplayName.ToLowerCSafe();

    // Saves the changes to the database
    SearchIndexInfoProvider.SetSearchIndexInfo(updateIndex);
}

> Back to list of examples

Updating multiple search indexes



// Gets all smart search indexes whose code name starts with 'New'
var indexes = SearchIndexInfoProvider.GetSearchIndexes().WhereStartsWith("IndexName", "New");

// Loops through individual search indexes
foreach (SearchIndexInfo index in indexes)
{
    // Updates the index properties
    index.IndexDisplayName = index.IndexDisplayName.ToUpper();

    // Saves the modified index to the database
    SearchIndexInfoProvider.SetSearchIndexInfo(index);
}

> Back to list of examples

Configuring the Indexed content settings for search indexes



// Gets the search index
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");

if (index != null)
{
    // Creates new index settings
    SearchIndexSettingsInfo indexSettings = new SearchIndexSettingsInfo();

    // Configures the indexed content properties (for a Page index in this case)
    indexSettings.ClassNames = ""; // Allows indexing for all page types
    indexSettings.Path = "/%";
    indexSettings.Type = SearchIndexSettingsInfo.TYPE_ALLOWED;

    // Saves the index settings to the database and assigns them to the search index
    SearchIndexSettings settings = new SearchIndexSettings();
    settings.SetSearchIndexSettingsInfo(indexSettings);
    index.IndexSettings = settings;

    // Saves the search index to the database
    SearchIndexInfoProvider.SetSearchIndexInfo(index);
}

> Back to list of examples

Assigning a search index to a site



// Gets the search index
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");
if (index != null)
{
    // Assigns the index to the current site
    SearchIndexSiteInfo.Provider.Add(index.IndexID, SiteContext.CurrentSiteID);
}

> Back to list of examples

Removing a search index from a site



// Gets the search index
SearchIndexInfo removeIndex = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");
if (removeIndex != null)
{
    // Gets the relationship between the index and the current site
    SearchIndexSiteInfo indexSite = SearchIndexSiteInfo.Provider.Get(removeIndex.IndexID, SiteContext.CurrentSiteID);

    // Removes the index from the site
    SearchIndexSiteInfo.Provider.Delete(indexSite);
}

> Back to list of examples

Assigning a culture to a page search index



// Gets the page search index and culture
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");
CultureInfo culture = CultureInfo.Provider.Get("en-us");

if ((index != null) && (culture != null))
{
    // Assigns the culture to the index
    SearchIndexCultureInfo.Provider.Add(index.IndexID, culture.CultureID);
}

> Back to list of examples

Removing a culture from a page search index



// Gets the page search index and culture
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");
CultureInfo culture = CultureInfo.Provider.Get("en-us");

if ((index != null) && (culture != null))
{
    // Gets the relationship between the index and the culture
    SearchIndexCultureInfo indexCulture = SearchIndexCultureInfo.Provider.Get(index.IndexID, culture.CultureID);

    // Removes the culture from the index
    SearchIndexCultureInfo.Provider.Delete(indexCulture);
}

> Back to list of examples

Deleting a search index



// Gets the search index
SearchIndexInfo deleteIndex = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");

if (deleteIndex != null)
{
    // Deletes the search index
    SearchIndexInfoProvider.DeleteSearchIndexInfo(deleteIndex);
}

> Back to list of examples

Search actions

Rebuilding a search index



// Gets the search index
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");

if (index != null)
{
    // Creates a rebuild task for the index.
    // The rebuild task will be processed as part of the next request handled by the application,
    // or by a scheduled task if the application is configured to handle search tasks using the scheduler.
    SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Rebuild, null, null, index.IndexName, index.IndexID);
}

> Back to list of examples

Searching indexes of the Pages type



// Prepares variables required to perform the search operation
IEnumerable<string> searchIndexes = new List<string> { "ArticleIndex" };
int pageNumber = 1;
int pageSize = 10;
UserInfo searchUser = MembershipContext.AuthenticatedUser;
string cultureCode = "en-us";
/* Indicates whether the search service uses site default language version of pages as a replacement
for pages that are not translated into the language specified by 'cultureCode' */
bool combineWithDefaultCulture = true;


// Performs basic search using default search settings
// Prepares a 'SearchParameters' object to search through indexes of the 'Pages' type
SearchParameters searchParametersDefault = SearchParameters.PrepareForPages("search query", searchIndexes, pageNumber, pageSize, searchUser, cultureCode, combineWithDefaultCulture);
// Searches the specified indexes
SearchResult searchResultDefault = SearchHelper.Search(searchParametersDefault);


// Performs search using modified search settings
string searchText = "title:\"5\" Away\"";
// 'FullSearch' ensures no special characters in the query are escaped
// This makes, for example, exact field searches such as the one in 'searchText' possible
SearchOptionsEnum searchOptions = SearchOptionsEnum.FullSearch;

// Prepares a 'SearchParameters' object to search through indexes of the 'Pages' type
SearchParameters searchParametersAdvanced = SearchParameters.PrepareForPages(new SearchPattern(searchText, searchOptions), searchIndexes, pageNumber, pageSize, searchUser, cultureCode, combineWithDefaultCulture);
// Searches the specified indexes
SearchResult searchResultAdvanced = SearchHelper.Search(searchParametersAdvanced);

> Back to list of examples

Searching through text



// Gets the search index
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("NewIndex");

if (index != null)
{
    // Prepares the search parameters
    SearchParameters parameters = new SearchParameters()
    {
        SearchFor = "home",
        SearchSort = "##SCORE##",
        Path = "/%",
        CurrentCulture = "EN-US",
        DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
        CombineWithDefaultCulture = false,
        CheckPermissions = false,
        SearchInAttachments = false,
        User = (UserInfo)MembershipContext.AuthenticatedUser,
        SearchIndexes = index.IndexName,
        StartingPosition = 0,
        DisplayResults = 100,
        NumberOfProcessedResults = 100,
        NumberOfResults = 0,
        AttachmentWhere = String.Empty,
        AttachmentOrderBy = String.Empty,
        ClassNames = ""
        /* The 'SearchParameters.ClassNames' property only limits the attachment search,
        not the results of the basic SearchHelper.Search method. You can limit class names (page types)
        by adding a Lucene search condition to the text of the 'SearchFor' query. */
    };

    // Performs the search and returns the matching results as a SearchResult object
    SearchResult results = SearchHelper.Search(parameters);

    // Processes the search result items from the SearchResult
    foreach (SearchResultItem resultItem in results.Items)
    {
        /* Access the values of search results via the properties of the SearchResultItem class,
        for example Title, Content, Score, etc. */
    }
}

> Back to list of examples

Updating search indexes



// Gets a page from the database
TreeNode node = new DocumentQuery<TreeNode>()
                    .Path("/Page", PathTypeEnum.Single)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

// Checks that the page exists and has search allowed
if ((node != null) && DocumentHelper.IsSearchTaskCreationAllowed(node))
{
    // Edits and saves the page
    node.DocumentName += " changed";
    node.Update();

    // Creates a smart search update task for the page (for all search indexes that cover the given page)
    SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Update, TreeNode.OBJECT_TYPE, SearchFieldsConstants.ID, node.GetSearchID(), node.DocumentID);
}

> Back to list of examples

Page search indexes

Fully setting up a new page search index



// Gets the 'en-US' culture for the index
CultureInfo culture = CultureInfo.Provider.Get("en-US");

// Creates a new page search index
SearchIndexInfo pageIndex = new SearchIndexInfo()
{
    IndexDisplayName = "Page index",
    IndexName = "PageIndex",

    IndexProvider = SearchIndexInfo.LUCENE_SEARCH_PROVIDER,
    IndexType = TreeNode.OBJECT_TYPE,

    IndexAnalyzerType = SearchAnalyzerTypeEnum.StandardAnalyzer,
    StopWordsFile = ""
};

// Creates new index settings
// Specifies the content allowed in the index
SearchIndexSettingsInfo indexSettings = new SearchIndexSettingsInfo()
{
    ID = Guid.NewGuid(),

    // Configures the indexed content properties (for a Page index)
    ClassNames = "", // Allows indexing for all page types
    Path = "/%",
    SiteName = SiteContext.CurrentSiteName,
    Type = SearchIndexSettingsInfo.TYPE_ALLOWED,
    IncludeAttachments = false,
    IncludeCategories = false
};

if (pageIndex != null && indexSettings != null && culture != null)
{
    // Saves the index settings to the database and assigns them to the search index
    SearchIndexSettings settings = new SearchIndexSettings();
    settings.SetSearchIndexSettingsInfo(indexSettings);
    pageIndex.IndexSettings = settings;

    // Saves the search index to the database
    SearchIndexInfoProvider.SetSearchIndexInfo(pageIndex);

    // Assigns the index to the current site
    SearchIndexSiteInfo.Provider.Add(pageIndex.IndexID, SiteContext.CurrentSiteID);

    // Assigns the 'en-US; culture to the index
    SearchIndexCultureInfo.Provider.Add(pageIndex.IndexID, culture.CultureID);

    // Creates a rebuild task for the index
    // The rebuild task will be processed as part of the next request handled by the application,
    // or by a scheduled task if the application is configured to handle search tasks using the scheduler.
    SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Rebuild, null, null, pageIndex.IndexName, pageIndex.IndexID);
}

> Back to list of examples