# Boolean search This article defines boolean search and describes the boolean operators AND, OR, NOT with boolean query examples. 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". **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Search with text query SearchResult result1 = index.Search("theory AND relativity"); // Search with object query SearchQuery wordQuery1 = SearchQuery.CreateWordQuery("theory"); SearchQuery wordQuery2 = SearchQuery.CreateWordQuery("relativity"); SearchQuery andQuery = SearchQuery.CreateAndQuery(wordQuery1, wordQuery2); SearchResult result2 = 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 whose text contains at least one of the words "Einstein" and "relativity". **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Search with text query SearchResult result1 = index.Search("Einstein OR relativity"); // Search with object query SearchQuery wordQuery1 = SearchQuery.CreateWordQuery("Einstein"); SearchQuery wordQuery2 = SearchQuery.CreateWordQuery("relativity"); SearchQuery orQuery = SearchQuery.CreateOrQuery(wordQuery1, wordQuery2); SearchResult result2 = 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 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". **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Search with text query SearchResult result1 = index.Search("relativity AND NOT Einstein"); // Search with object query SearchQuery wordQuery1 = SearchQuery.CreateWordQuery("relativity"); SearchQuery wordQuery2 = SearchQuery.CreateWordQuery("Einstein"); SearchQuery notQuery = SearchQuery.CreateNotQuery(wordQuery2); SearchQuery andQuery = SearchQuery.CreateAndQuery(wordQuery1, notQuery); SearchResult result2 = 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#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Search with text query SearchResult result1 = index.Search("(theory AND relativity) AND NOT (Einstein OR Albert)"); // Search with object query SearchQuery theoryWordQuery = SearchQuery.CreateWordQuery("theory"); SearchQuery relativityWordQuery = SearchQuery.CreateWordQuery("relativity"); SearchQuery andQuery = SearchQuery.CreateAndQuery(theoryWordQuery, relativityWordQuery); SearchQuery einsteinWordQuery = SearchQuery.CreateWordQuery("Einstein"); SearchQuery albertWordQuery = SearchQuery.CreateWordQuery("Albert"); SearchQuery orQuery = SearchQuery.CreateOrQuery(einsteinWordQuery, albertWordQuery); SearchQuery notQuery = SearchQuery.CreateNotQuery(orQuery); SearchQuery rootQuery = SearchQuery.CreateAndQuery(andQuery, notQuery); SearchResult result2 = index.Search(rootQuery); ``` In object form, the [CreateAndQuery](https://reference.groupdocs.com/search/net/groupdocs.search/searchquery/createandquery/) and [CreateOrQuery](https://reference.groupdocs.com/search/net/groupdocs.search/searchquery/createorquery/) methods also have overloads that take an array of two or more child queries. For example, the following query finds documents containing all three words. **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Search with object query combining three words with the AND operator SearchQuery andQuery = SearchQuery.CreateAndQuery( SearchQuery.CreateWordQuery("theory"), SearchQuery.CreateWordQuery("special"), SearchQuery.CreateWordQuery("relativity")); SearchResult result = index.Search(andQuery); ``` When the index uses stop words (see the [Indexing with stop words]({{< ref "search/net/developer-guide/advanced-usage/indexing/indexing-with-stop-words.md" >}}) page), a stop word of a query and a part of a query that consists of stop words only do not narrow the result. For example, the queries "the AND relativity" and "relativity AND NOT the" find the documents that contain the word "relativity". Since version 26.11, this applies also to a stop word excluded with the NOT operator and to a part of stop words only, such as "(the OR a)" in "relativity AND (the OR a)"; before, the query "relativity AND NOT the" found all the documents of the index, and the query "relativity AND (the OR a)" found nothing. ## More resources ### GitHub examples You may easily run the code from documentation articles and see the features in action in our GitHub examples: * [GroupDocs.Search for .NET examples](https://github.com/groupdocs-search/GroupDocs.Search-for-.NET) * [GroupDocs.Search for Java examples](https://github.com/groupdocs-search/GroupDocs.Search-for-Java) ### Free online document search App Along with full featured .NET library we provide simple, but powerful free Apps. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online [Free Online Document Search App](https://products.groupdocs.app/search).