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.
importosfromgroupdocs.searchimportIndex# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search in the indexresult1=index.search("m?rry")# Search for 'merry', 'marry', etc.result2=index.search("card?(1~6)")# Search for 'cardiff', 'cardinal', 'cardio', 'cards', etc.# Printing the words foundforquery,resultin(("m?rry",result1),("card?(1~6)",result2)):print(query,"- documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path),document.terms)
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:
append_wildcard 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 (ValueError in Python).
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.
importosfromgroupdocs.searchimportIndex,SearchQueryfromgroupdocs.search.commonimportWordPattern# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with pattern "m?rry"# Search for 'merry', 'marry', etc.pattern1=WordPattern()pattern1.append_string("m")pattern1.append_one_character_wildcard()pattern1.append_string("rry")query1=SearchQuery.create_word_pattern_query(pattern1)result1=index.search(query1)# Search with pattern "card?(1~6)"# Search for 'cardiff', 'cardinal', 'cardio', 'cards', etc.pattern2=WordPattern()pattern2.append_string("card")pattern2.append_wildcard(1,6)query2=SearchQuery.create_word_pattern_query(pattern2)result2=index.search(query2)# Printing the words foundforpattern,resultin((pattern1,result1),(pattern2,result2)):print(pattern.to_string(),"- documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path),document.terms)