Wildcard search allows you to search for words with unknown letters or ranges of letters.
In text form of a search query, 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 groups of wildcard characters are less and closer to the end of a search query.
It is important to know that wildcard search is flexible enough to use for prefix queries, since prefix query is a special case of a wildcard query.
Examples of wildcard search with queries in text form are presented below.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index in the specified folder
constindex=newgroupdocs.search.Index(indexFolder);// Indexing documents from the specified folder
index.add(documentsFolder);// Search in the index
constquery1='m???is';constresult1=index.search(query1);// Search for 'mauris', 'mollis', 'mattis', 'magnis', etc.
constquery2='pri?(1~7)';constresult2=index.search(query2);// Search for 'private', 'principles', 'principle', etc.
To build a query for the wildcard search in object form, use the WordPattern class. This class contains methods for adding known parts of a word and wildcards to a template. An example of constructing a query in object form is presented below.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index in the specified folder
constindex=newgroupdocs.search.Index(indexFolder);// Indexing documents from the specified folder
index.add(documentsFolder);// Search with pattern "m???is"
// Search for 'mauris', 'mollis', 'mattis', 'magnis', etc.
constpattern1=newgroupdocs.search.WordPattern();pattern1.appendString('m');pattern1.appendOneCharacterWildcard();pattern1.appendOneCharacterWildcard();pattern1.appendOneCharacterWildcard();pattern1.appendString('is');constquery1=groupdocs.search.SearchQuery.createWordPatternQuery(pattern1);constresult1=index.search(query1);// Search with pattern "pri?(1~7)"
// Search for 'private', 'principles', 'principle', etc.
constpattern2=newgroupdocs.search.WordPattern();pattern2.appendString('pri');pattern2.appendWildcard(1,7);constquery2=groupdocs.search.SearchQuery.createWordPatternQuery(pattern2);constresult2=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: