Search for text containing special characters
Leave feedback
In the case where the search query must contain special characters that are not control characters, 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 detailes on search query syntax.
However, it must be remembered that if the escaped character is a separator, then it will still not be found in the text, since it is not indexed. And 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. Since, in fact, when indexing, these characters break the word into parts, and they themselves are deleted and not indexed.
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.
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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index in the specified folder
constindex=newgroupdocs.search.Index(indexFolder);// Setting character types
constcharacters1=java.newArray('char',['&']);constcharacters2=java.newArray('char',['-']);index.getDictionaries().getAlphabet().setRange(characters1,groupdocs.search.CharacterType.Letter);index.getDictionaries().getAlphabet().setRange(characters2,groupdocs.search.CharacterType.Separator);// Indexing documents from the specified folder
index.add(documentsFolder);// Defining a search query
constword='rock&roll-music';// Replacing separators with the space characters
letresult='';for(leti=0;i<word.length;i++){constcharacter=word.charAt(i);constch=java.newChar(character);constcharacterType=index.getDictionaries().getAlphabet().getCharacterType(ch);if(String(characterType)==String(groupdocs.search.CharacterType.Separator)){result+=' ';}else{result+=character;}}// Escaping special characters
constspecialCharacters='():"&|!^~*?\\';for(leti=result.length-1;i>=0;i--){constc=result.charAt(i);if(specialCharacters.indexOf(c)!=-1){result=result.slice(0,i)+'\\'+result.slice(i);}}letquery=result.toString();if(query.includes(' ')){query='"'+query+'"';}// Searching
constsearchResult=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: