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
newIndex.IndexDisplayName = "New index";
newIndex.IndexName = "NewIndex";
newIndex.IndexIsCommunityGroup = false;
newIndex.IndexAnalyzerType = SearchAnalyzerTypeEnum.StandardAnalyzer;
newIndex.StopWordsFile = "";

// Sets the index type to Pages
newIndex.IndexType = TreeNode.OBJECT_TYPE;

/* 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
 * Forums: PredefinedObjectType.FORUM)
 * On-line forms: SearchHelper.ONLINEFORMINDEX
 * General: SearchHelper.GENERALINDEX
 * Custom: SearchHelper.CUSTOM_SEARCH_INDEX
*/

// 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.IncludeBlogs = true;
	indexSettings.IncludeForums = true;
	indexSettings.IncludeMessageCommunication = true;
	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
	SearchIndexSiteInfoProvider.AddSearchIndexToSite(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 = SearchIndexSiteInfoProvider.GetSearchIndexSiteInfo(removeIndex.IndexID, SiteContext.CurrentSiteID);

	// Removes the index from the site
	SearchIndexSiteInfoProvider.DeleteSearchIndexSiteInfo(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 = CultureInfoProvider.GetCultureInfo("en-us");

if ((index != null) && (culture != null))
{
	// Assigns the culture to the index
	SearchIndexCultureInfoProvider.AddSearchIndexCulture(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 = CultureInfoProvider.GetCultureInfo("en-us");

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

	// Removes the culture from the index
	SearchIndexCultureInfoProvider.DeleteSearchIndexCultureInfo(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
// NOTE: This API is available from hotfix version 12.0.32 and onwards
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

Using an index to search 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

Creating an update task for search indexes

// Gets a TreeProvider instance
TreeProvider provider = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page from the current site
TreeNode node = provider.SelectNodes()
	.Path("/")
	.OnCurrentSite()
	.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


Was this page helpful?