Search for text containing special characters
Leave feedback
If a search query must contain special characters that are used as ordinary characters and not as query syntax elements, these characters must be escaped.
The special characters are:
( ) : " & | ! ^ ~ * ? \ space
These characters can be escaped by adding a \ sign to the left of the character. For example: \& - escaping & character; \~ - escaping ~ character; \s - escaping space character. See Search operation table for details on search query syntax.
However, if the escaped character is a separator, it will still not be found in the text, since separators are not indexed. As a result, words containing such characters will also not be found when searching for an exact match.
If the search query contains separator characters, words containing these characters will not be found, because during indexing these characters break the word into parts and are not indexed themselves.
Therefore, when performing a search, the query must be checked for the content of separator characters. Each word containing such characters should be broken into several smaller words and form a search phrase.
In the example below, the character types are set in the alphabet of a new index before documents are added. Since version 26.10, the alphabet can be changed only while the index contains no documents; otherwise, InvalidOperationException is thrown. For details, see the Alphabet page.
The order of processing each word in a search query is as follows:
If separators are present in the word, they are replaced by spaces, and the word search is converted to a phrase search.
If special characters are present in the word, they are escaped.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating a new index in the specified folder (the existing index files are deleted)Indexindex=newIndex(indexFolder,true);// Setting character types before indexing documentsindex.Dictionaries.Alphabet.SetRange(newchar[]{'&'},CharacterType.Letter);index.Dictionaries.Alphabet.SetRange(newchar[]{'-'},CharacterType.Separator);// Indexing documents from the specified folderindex.Add(documentsFolder);// Defining a search querystringword="rock&roll-music";// Replacing separators with the space charactersStringBuilderresult=newStringBuilder();for(inti=0;i<word.Length;i++){charcharacter=word[i];CharacterTypecharacterType=index.Dictionaries.Alphabet.GetCharacterType(character);if(characterType==CharacterType.Separator){result.Append(' ');}else{result.Append(character);}}// Escaping special charactersconststringspecialCharacters="():\"&|!^~*?\\";for(inti=result.Length-1;i>=0;i--){charc=result[i];if(specialCharacters.Contains(c.ToString())){result.Insert(i,'\\');}}stringquery=result.ToString();if(query.Contains(" ")){query="\""+query+"\"";}// SearchingSearchResultsearchResult=index.Search(query);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: