Wildcard search allows you to search for words with unknown letters or ranges of letters.
In a search query in text form, there are 2 forms of wildcard characters:
? for a single character;
?(n~m) for a group of characters, where n and m are numbers from 0 to 255, and n <= m.
Wildcard search is similar to regular expression search, but it works significantly faster when there are fewer groups of wildcard characters and they are closer to the end of a search query.
Wildcard search is flexible enough to be used for prefix queries, since a prefix query is a special case of a wildcard query. Since version 26.10, wildcard search uses as many threads as there are processor cores by default.
Examples of wildcard search with queries in text form are presented below.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Search in the indexSearchResultresult1=index.Search("m?rry");// Search for 'merry', 'marry', etc.SearchResultresult2=index.Search("card?(1~6)");// Search for 'cardiff', 'cardinal', 'cardio', 'cards', etc.
To build a query for the wildcard search in object form, use the WordPattern class. This class contains the following methods for adding known parts of a word and wildcards to a pattern:
AppendString appends a string of known characters.
AppendWildcard appends a wildcard for the specified minimum and maximum numbers of characters (?(min~max) in text form). Both values must be in the range from 0 to 255, and the minimum must not be greater than the maximum; otherwise, ArgumentOutOfRangeException is thrown.
The known characters are converted to lower case, since words are stored in the index in lower case. An example of constructing a query in object form is presented below.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Search with pattern "m?rry"// Search for 'merry', 'marry', etc.WordPatternpattern1=newWordPattern();pattern1.AppendString("m");pattern1.AppendOneCharacterWildcard();pattern1.AppendString("rry");SearchQueryquery1=SearchQuery.CreateWordPatternQuery(pattern1);SearchResultresult1=index.Search(query1);// Search with pattern "card?(1~6)"// Search for 'cardiff', 'cardinal', 'cardio', 'cards', etc.WordPatternpattern2=newWordPattern();pattern2.AppendString("card");pattern2.AppendWildcard(1,6);SearchQueryquery2=SearchQuery.CreateWordPatternQuery(pattern2);SearchResultresult2=index.Search(query2);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: