The StopWordDictionary class is designed to store stop words in an index. Stop words are excluded from indexing only if the UseStopWords index setting is enabled. Information on using stop words during indexing is provided on the Indexing with stop words page.
Note
Stop words define which terms of documents are indexed. Therefore, the stop word dictionary and the UseStopWords setting of an index can be changed only while the index contains no documents and is not performing an operation; otherwise, a change throws InvalidOperationException. A call that does not change the content of the dictionary is allowed. Configure stop words after creating a new index and before adding documents. See also the Index format and compatibility page.
The dictionary keeps words in lower case without leading and trailing white space. Therefore, the letter case of words does not matter when words are added, removed or checked.
To get the number of stop words in the dictionary, use the Count property.
To add stop words to the dictionary, use the AddRange method.
To remove words from the dictionary, use the RemoveRange method.
To check for a word in the dictionary, use the Contains method. The method ignores the letter case of the word.
To remove all words from the dictionary, use the Clear method.
To import words from a file, use the ImportDictionary method. Errors of exporting and importing are reported by the ErrorOccurred event of the index instead of exceptions.
The following example demonstrates the use of methods of the stop word dictionary.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index settings instanceIndexSettingssettings=newIndexSettings();settings.UseStopWords=true;// Enabling the use of stop words during indexing// Creating a new index in the specified folder; the dictionary can be changed only while the index contains no documentsIndexindex=newIndex(indexFolder,settings,true);if(index.Dictionaries.StopWordDictionary.Count>0){// Removing all words from the dictionaryindex.Dictionaries.StopWordDictionary.Clear();}// Adding stop words to the dictionarystring[]words=newstring[]{"a","an","the","but","by"};index.Dictionaries.StopWordDictionary.AddRange(words);// The letter case of words is ignoredif(index.Dictionaries.StopWordDictionary.Contains("But")&&index.Dictionaries.StopWordDictionary.Contains("BY")){// Removing words from the dictionaryindex.Dictionaries.StopWordDictionary.RemoveRange(newstring[]{"but","by"});}// Exporting words to a fileindex.Dictionaries.StopWordDictionary.ExportDictionary(@"c:\StopWords.txt");// Importing words from a fileindex.Dictionaries.StopWordDictionary.ImportDictionary(@"c:\StopWords.txt");// Indexing documents; after that, the stop words of the index cannot be changedindex.Add(documentsFolder);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: