Sometimes, we have a lot of different formatted documents. We create index then perform search and API shows hits from all the indexed documents. Ever thought to get search results from desired documents only?

Note: the code examples in this article have been updated to the API of GroupDocs.Search for .NET 26.10. The features described here were introduced in version 19.5 and are still available in the current version. The examples use the namespaces GroupDocs.Search, GroupDocs.Search.Common, GroupDocs.Search.Options, GroupDocs.Search.Results and System.Text.RegularExpressions.

Let’s dig it further. Suppose, you have multiple TXT, XLSX and DOCX documents. What if you want to search a word or text only in TXT and DOCX files with specific word occurrence in file names? We’ve now improved the ISearchDocumentFilter interface for such a purpose in GroupDocs.Search. This interface represents search document filter and the SearchDocumentFilter class is used for creation of filter instances.

Let’s now understand this with a use-case.

C#

string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string documentsFolder = @"c:\MyDocuments\"; // Specify path to the folder with documents

// Creating an index and indexing documents
Index index = new Index(indexFolder);
index.Add(documentsFolder);

// Creating a filter that returns only TXT and DOCX documents
ISearchDocumentFilter extensionFilter = SearchDocumentFilter.CreateFileExtension(".txt", ".docx");

// Creating a filter that returns only documents with the word 'task' in the file name
// The pattern is applied to the full path, so it requires that no path separator follows the word
ISearchDocumentFilter nameFilter = SearchDocumentFilter.CreateFilePathRegularExpression(@"task[^\\/]*$", RegexOptions.IgnoreCase);

// Combining the filters with the logical AND
ISearchDocumentFilter filter = SearchDocumentFilter.CreateAnd(extensionFilter, nameFilter);

// Setting the filter in the search options
SearchOptions options = new SearchOptions();
options.SearchDocumentFilter = filter;

// Searching for the word 'hobbit'
SearchResult result = index.Search("hobbit", options);

// Printing the paths of the found documents
foreach (FoundDocument document in result)
{
    Console.WriteLine(document.DocumentInfo.FilePath);
}

What we did in this example? We initiated a request to search occurrence of a word hobbit in all the TXT and DOCX files with word task in their file names. In this snippet, you can see a method CreateAnd (named CreateConjuction in the API of version 19.5). It creates logical conjunction (logical and) of the specified filters. Filters can also be combined with the CreateOr and CreateNot methods, and a filter by document attributes can be created with the CreateAttribute method. For details, see the Document filtering in search result documentation article.

Settings for Log Functionality

This improvement allows to set up log file name and maximum log file size. In version 19.5, the LogSettings class with the FileName and MaxSize properties was added for this purpose, and the settings were assigned through the LogSettings property of the Index class. In the current version of the API, the log is configured with the FileLogger class: its constructor takes the path to the log file and the maximum size of the file in megabytes, and the logger is assigned to the Logger property of the index settings.

Let’s go through the implementation:

C#

string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string documentsFolder = @"c:\MyDocuments\"; // Specify path to the folder with documents
string logPath = @"c:\Log.txt"; // Specify path to the log file

// Creating index settings with a file logger
IndexSettings settings = new IndexSettings();
settings.Logger = new FileLogger(logPath, 4.0); // Setting the log file name and its maximum size of 4 MB

// Creating an index with the settings
Index index = new Index(indexFolder, settings);

// Indexing documents, the events are written to the log file
index.Add(documentsFolder);

The logger is not saved in the index, so it has to be assigned again each time the index is opened.

Add English Synonyms

This improvement adds English synonyms to default synonym dictionary. A new index already contains them, so synonym search works without filling the dictionary.

C#

string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder

// Creating an index
Index index = new Index(indexFolder);

// Getting the synonyms of the word 'answer' from the default synonym dictionary
string[] synonyms = index.Dictionaries.SynonymDictionary.GetSynonyms("answer");
foreach (string synonym in synonyms)
{
    Console.WriteLine(synonym);
}

// Searching for the word 'answer' and its synonyms
SearchOptions options = new SearchOptions();
options.UseSynonymSearch = true;
SearchResult result = index.Search("answer", options);

Above are the major improvements introduced in version 19.5. The same features were released in GroupDocs.Search for Java 19.5; see the Java documentation for the Java API. Please go through all the other changes in the release notes of GroupDocs.Search for .NET and GroupDocs.Search for Java.

Avail these exciting improvements now in your project. This release is available for download. Please share your feedback or concerns here.