This page contains a phrase search definition as well as a description of phrase search with the GroupDocs.Search API.
Phrase search definition
Phrase search is a type of search that allows users to search for documents containing an exact sentence or phrase rather than containing a set of keywords in random order.
Phrase search in GroupDocs.Search
Phrase search allows you to search for an exact phrase and find a given sequence of words in the text of indexed documents. In text form, the following syntax is used to specify a phrase search query:
“word1 word2 word3 …”
Quotation marks are required, for example, in Python:
query = '"theory of relativity"'
Please note that if stop words are used in a search query, the phrase will still be found; however, any words can be found in place of the stop words.
The following example demonstrates performing the phrase search with a query in text and object form.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search for the phrase 'theory of relativity' in text formquery1='"theory of relativity"'result1=index.search(query1)# Search for the phrase 'theory of relativity' in object formword1=SearchQuery.create_word_query("theory")word2=SearchQuery.create_word_query("of")word3=SearchQuery.create_word_query("relativity")query2=SearchQuery.create_phrase_search_query(word1,word2,word3)result2=index.search(query2)# Printing the documents foundprint("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path))
In addition to words, phrase search queries can also use two kinds of wildcards:
* byte-number
* byte-number ~~ byte-number
where byte-number is a number from 0 to 255.
The first pattern represents the exact number of unknown words in a phrase, for example *2. The second pattern represents the range of the number of unknown words in a phrase, for example *1~~2. In object form, these wildcards are created with the create_wildcard_query method; such a query can be used only as a part of a phrase search query.
Phrase search query with wildcards is flexible enough to be used instead of span term query (span query).
The following example demonstrates the use of wildcards in phrase search queries in text and object form.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search for the phrase with a wildcard in text formquery1='"theory *1~~2 relativity"'result1=index.search(query1)# Search for the phrase with a wildcard in object formword1=SearchQuery.create_word_query("theory")wildcard2=SearchQuery.create_wildcard_query(1,2)word3=SearchQuery.create_word_query("relativity")query2=SearchQuery.create_phrase_search_query(word1,wildcard2,word3)result2=index.search(query2)# The search can find the following phrases:# "theory of relativity"# "theory of special relativity"# Printing the phrases found# (stop words and the words matched by a wildcard are reported as *)print("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path))forfieldindocument.found_fields:forsequenceinfield.term_sequences:print(" "," ".join(sequence))
Documents found by the text query: 2
Documents found by the object query: 2
einstein-biography.txt
theory * relativity
relativity-primer.txt
theory * relativity
theory * * relativity
Phrase search can be combined with other types of searches. The specification of search queries in text form is presented on the Query language specification page. A table of the possibility of nesting search queries in object form is presented on the Nesting search queries in object form page.
The following example demonstrates the use of both wildcards representing words and characters in words.
importosfromgroupdocs.searchimportIndex,SearchQueryfromgroupdocs.search.commonimportWordPattern# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search for the phrase in text formquery1='"Test?(0~1) *1~~2 relativity"'result1=index.search(query1)# Search for the phrase in object formpattern=WordPattern()pattern.append_string("Test")pattern.append_wildcard(0,1)word_pattern1=SearchQuery.create_word_pattern_query(pattern)wildcard2=SearchQuery.create_wildcard_query(1,2)word3=SearchQuery.create_word_query("relativity")query2=SearchQuery.create_phrase_search_query(word_pattern1,wildcard2,word3)result2=index.search(query2)# The search can find the following phrases:# "Test of special relativity"# "Tests of special relativity"# "Test of general relativity"# "Tests of general relativity"# Printing the phrases found# (stop words and the words matched by a wildcard are reported as *)print("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path))forfieldindocument.found_fields:forsequenceinfield.term_sequences:print(" "," ".join(sequence))