This page contains the boolean search definition, a description of all boolean operators used for boolean search, and boolean query examples.
Boolean search terms
Boolean search is a type of search that allows you to combine queries with boolean operators AND, OR, NOT to create a boolean query and further obtain more relevant results. For example, a boolean query might be “Albert AND Einstein”. This will limit the search results to only documents that contain both words.
Operator AND
The AND operator allows you to find only those documents that are found for each nested search query separately. The following example demonstrates the use of the AND operator in text and object form queries. The queries search for documents whose text contains both words “theory” and “relativity”.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with text queryresult1=index.search("theory AND relativity")# Search with object queryword_query1=SearchQuery.create_word_query("theory")word_query2=SearchQuery.create_word_query("relativity")and_query=SearchQuery.create_and_query(word_query1,word_query2)result2=index.search(and_query)# 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))
The OR operator allows you to find all the documents that are found for at least one nested search query. The example demonstrates the use of the OR operator in text and object form queries. The queries search for documents whose text contains at least one of the words “Einstein” and “relativity”.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with text queryresult1=index.search("Einstein OR relativity")# Search with object queryword_query1=SearchQuery.create_word_query("Einstein")word_query2=SearchQuery.create_word_query("relativity")or_query=SearchQuery.create_or_query(word_query1,word_query2)result2=index.search(or_query)# 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),document.terms)
Documents found by the text query: 4
Documents found by the object query: 4
einstein-biography.txt ['einstein', 'relativity']
gravity-notes.txt ['relativity']
nobel-prize.txt ['einstein']
relativity-primer.txt ['relativity']
The NOT operator allows you to invert the result of a nested search query and find all documents in which the nested search query finds no occurrences. The example demonstrates the use of the NOT operator in text and object form queries. The queries search for documents whose text contains the word “relativity” but does not contain the word “Einstein”.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with text queryresult1=index.search("relativity AND NOT Einstein")# Search with object queryword_query1=SearchQuery.create_word_query("relativity")word_query2=SearchQuery.create_word_query("Einstein")not_query=SearchQuery.create_not_query(word_query2)and_query=SearchQuery.create_and_query(word_query1,not_query)result2=index.search(and_query)# 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))
Boolean search operators can be combined using parentheses. The example below shows how to use parentheses to construct complex boolean search queries. In the example the query is presented in text and object form and searches for documents containing the words “theory” and “relativity” and not containing the words “Einstein” and “Albert”.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with text queryresult1=index.search("(theory AND relativity) AND NOT (Einstein OR Albert)")# Search with object querytheory_word_query=SearchQuery.create_word_query("theory")relativity_word_query=SearchQuery.create_word_query("relativity")and_query=SearchQuery.create_and_query(theory_word_query,relativity_word_query)einstein_word_query=SearchQuery.create_word_query("Einstein")albert_word_query=SearchQuery.create_word_query("Albert")or_query=SearchQuery.create_or_query(einstein_word_query,albert_word_query)not_query=SearchQuery.create_not_query(or_query)root_query=SearchQuery.create_and_query(and_query,not_query)result2=index.search(root_query)# 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 object form, the create_and_query and create_or_query methods also take two or more child queries, as separate arguments or as one list. For example, the following query finds documents containing all three words.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with object query combining three words with the AND operatorand_query=SearchQuery.create_and_query(SearchQuery.create_word_query("theory"),SearchQuery.create_word_query("special"),SearchQuery.create_word_query("relativity"))result=index.search(and_query)# Printing the documents foundprint("Query:",and_query.to_string())print("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))