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 in the text of which there are both words “theory” and “relativity”.
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 text querySearchResultresult1=index.Search("theory AND relativity");// Search with object querySearchQuerywordQuery1=SearchQuery.CreateWordQuery("theory");SearchQuerywordQuery2=SearchQuery.CreateWordQuery("relativity");SearchQueryandQuery=SearchQuery.CreateAndQuery(wordQuery1,wordQuery2);SearchResultresult2=index.Search(andQuery);
Operator OR
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 in the text of which there is at least one of the words “Einstein” and “relativity”.
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 text querySearchResultresult1=index.Search("Einstein OR relativity");// Search with object querySearchQuerywordQuery1=SearchQuery.CreateWordQuery("Einstein");SearchQuerywordQuery2=SearchQuery.CreateWordQuery("relativity");SearchQueryorQuery=SearchQuery.CreateOrQuery(wordQuery1,wordQuery2);SearchResultresult2=index.Search(orQuery);
Operator NOT
The NOT operator allows you to invert the result of a nested search query and find all documents in which for the nested search query are found no occurrences. The example demonstrates the use of the NOT operator in text and object form queries. The queries search for documents in the text of which the word “relativity” is presented but the word “Einstein” is not.
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 text querySearchResultresult1=index.Search("relativity AND NOT Einstein");// Search with object querySearchQuerywordQuery1=SearchQuery.CreateWordQuery("relativity");SearchQuerywordQuery2=SearchQuery.CreateWordQuery("Einstein");SearchQuerynotQuery=SearchQuery.CreateNotQuery(wordQuery2);SearchQueryandQuery=SearchQuery.CreateAndQuery(wordQuery1,notQuery);SearchResultresult2=index.Search(andQuery);
Complex queries
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”.
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 text querySearchResultresult1=index.Search("(theory AND relativity) AND NOT (Einstein OR Albert)");// Search with object querySearchQuerytheoryWordQuery=SearchQuery.CreateWordQuery("theory");SearchQueryrelativityWordQuery=SearchQuery.CreateWordQuery("relativity");SearchQueryandQuery=SearchQuery.CreateAndQuery(theoryWordQuery,relativityWordQuery);SearchQueryeinsteinWordQuery=SearchQuery.CreateWordQuery("Einstein");SearchQueryalbertWordQuery=SearchQuery.CreateWordQuery("Albert");SearchQueryorQuery=SearchQuery.CreateOrQuery(einsteinWordQuery,albertWordQuery);SearchQuerynotQuery=SearchQuery.CreateNotQuery(orQuery);SearchQueryrootQuery=SearchQuery.CreateAndQuery(andQuery,notQuery);SearchResultresult2=index.Search(rootQuery);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: