dtSearch .NET Standard API 2023.02
SearchJob Class

Use to search indexes or to search without an index.

public class SearchJob : JobBase;
Search criteria

There are three components to the search request that can be included in a SearchJob: 

(1) Request: The search request, which can be in the "boolean", "all words", or "anywords" syntax, depending on the value of SearchFlags

(2) BooleanConditions: A string in the boolean syntax expressing any conditions that must be satisfied in addition to whatever is in Request. This provides a way to add boolean conditions (such as field criteria) to a user-supplied query, which may be in the all words or any words syntax. 

(3) FileConditions: A string expressing conditions such as filename matches, file size matches, etc., that documents must satisfy. 

For more information, see: Search Requests Overview

Monitoring and cancelling searches

To receive callbacks during a search with the name of each document as it is found, create an object that implements the ISearchStatusHandler interface and attach it to the SearchJob's StatusHandler

In server applications, to limit the amount of time and resources a search can consume, use TimeoutSeconds, MaxFilesToRetrieve, and AutoStopLimit

For more information, see: Limiting Searches.

Search filters

A SearchFilter represents a set of documents that can be returned in a search. To limit a search to the documents in a SearchFilter, set up a SearchFilter object with the filter to use, and assign it to SearchJob.SearchFilter. Example:

using (dtSearch.Engine.SearchFilter filter = new SearchFilter()) { int iIndex = filter.AddIndex(m_indexPath); // This will limit the search to documents with DocIds between 1 and 9999. filter.SelectItems(iIndex, 1, 9999, true); searchJob.SearchFilter = filter; // Now when searchJob is executed, only documents identified by searchFilter can be // retrieved. ...

To generate a search filter based on the results of a search, pass the SearchFilter to the SearchJob.Execute method, like this:

using (SearchJob searchJob = new SearchJob()) using (SearchResults searchResults = new SearchResults()) using (SearchFilter resultsFilter = new SearchFilter()) { searchJob.IndexesToSearch.Add(IndexPath); searchJob.Request = request; searchJob.Execute(searchResults, resultsFilter); // Now resultsFilter will select all of the documents that were found // in the search. // ... }

To optimize the search to generate a SearchFilter only, set the flag dtsSearchFastSearchFilterOnly in SearchJob. 

After the search, every document retrieved in the search will be selected in the output search filter. The generated search filter can then be used to limit a subsequent search, which can be useful for implementing a "search within these results" feature. 

SearchJob.MaxFilesToRetrieve does not affect the generated output SearchFilter, which will include every item retrieved in the search. However, if AutoStopLimit causes the search to terminate before it is complete, then only items found before the search stopped will be selected in the SearchFilter

For more information, see: Limiting searches with SearchFilters.


SearchJob, SearchResults, and SearchFilter all require the IDisposable Pattern.

// Allocate SearchJob and SearchResults to store results using (dtSearch.Engine.SearchJob searchJob = new dtSearch.Engine.SearchJob()) using (SearchResults results = new SearchResults()) { searchJob.Request = searchRequest.Text; searchJob.IndexesToSearch.Add(indexPath.Text); searchJob.MaxFilesToRetrieve = 100; searchJob.SearchFlags = SearchFlags.dtsSearchDelayDocInfo; searchJob.Execute(results); searchJob.Execute(); if (searchJob.Errors.Count > 0) // ... display error messages ... else { // ... display search results ... } }