Efficient management of search results is fundamental to a powerful document retrieval system, and knowing how to interpret and make use of these results can greatly improve data-driven decisions. In this guide, we delve into how to work with search results using Java. From establishing an index repository to setting up fuzzy search parameters, this tutorial offers a straightforward, hands-on method for handling search queries and their corresponding results. Whether you are analyzing simple term matches or more intricate phrases, this approach enables you to process results thoroughly and identify valuable patterns. By the end, you’ll be proficient in how to retrieve search results in Java and apply them across various use cases.
Steps to Work with Search Results using Java
- Add the GroupDocs.Search for Java library to your project to work with search results
- Create an index object using the Index class and specify the folder location for storing the index
- Use the Index.add method to index documents from the designated folder
- Create a SearchOptions instance and enable fuzzy search by calling FuzzySearch.setEnabled(true)
- Set the maximum allowed differences (e.g., 3) with the setFuzzyAlgorithm method
- Call the Index.search method to search for documents containing the specified terms or phrases, along with the configured SearchOptions
- Display the number of documents found using getDocumentCount method and the total occurrences of search terms with getOccurrenceCount method
- Iterate through the search results using the getFoundDocument method, and for each document, show its file path and occurrence count
- For each document, loop through the FoundDocumentField collection and display details like field names, occurrence counts, found terms, and phrases
To effectively handle search results, the first step is to establish an index repository for your documents. With the provided code snippet, an index is created in a designated folder, and documents from another folder are added to this index. Next, search options are configured to activate fuzzy search, enabling flexible matching with a defined threshold for differences. This configuration supports searches for terms like “Einstein” or phrases such as “Theory of Relativity” using sophisticated algorithms. After executing the search, the results are retrieved through the SearchResult object. The number of documents matching the query and their occurrence counts are shown. Detailed data for each document is extracted, including the file path and term occurrences. By iterating through the found fields in each document, you can pinpoint specific fields with matches and analyze terms and phrases along with their frequencies. Below code shows how to access index search results with Java, offering an organized view of the retrieved information.
Code to Work with Search Results using Java
import com.groupdocs.search.Index; | |
import com.groupdocs.search.licenses.License; | |
import com.groupdocs.search.options.SearchOptions; | |
import com.groupdocs.search.options.TableDiscreteFunction; | |
import com.groupdocs.search.results.FoundDocument; | |
import com.groupdocs.search.results.FoundDocumentField; | |
import com.groupdocs.search.results.SearchResult; | |
public class WorkwithSearchResultsUsingJava { | |
public static void main(String[] args) throws Exception { | |
// Apply the license to remove the restrictions | |
// imposed by the Search library | |
License license = new License(); | |
license.setLicense("GroupDocs.Search.lic"); | |
// The path where the index will be stored | |
String indexFolder = "c:\\MyIndex\\"; | |
// The folder containing the documents you want to search | |
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); | |
// Creating search options | |
SearchOptions options = new SearchOptions(); | |
options.getFuzzySearch().setEnabled(true); // Enabling the fuzzy search | |
options.getFuzzySearch().setFuzzyAlgorithm(new TableDiscreteFunction(3)); // Setting the maximum number of differences to 3 | |
// Search for documents containing the word 'Einstein' or the phrase 'Theory of Relativity' | |
SearchResult result = index.search("Einstein OR \"Theory of Relativity\"", options); | |
// Printing the result | |
System.out.println("Documents: " + result.getDocumentCount()); | |
System.out.println("Total occurrences: " + result.getOccurrenceCount()); | |
for (int i = 0; i < result.getDocumentCount(); i++) { | |
FoundDocument document = result.getFoundDocument(i); | |
System.out.println("\tDocument: " + document.getDocumentInfo().getFilePath()); | |
System.out.println("\tOccurrences: " + document.getOccurrenceCount()); | |
for (FoundDocumentField field : document.getFoundFields()) { | |
System.out.println("\t\tField: " + field.getFieldName()); | |
System.out.println("\t\tOccurrences: " + document.getOccurrenceCount()); | |
// Printing found terms | |
if (field.getTerms() != null) { | |
for (int k = 0; k < field.getTerms().length; k++) { | |
System.out.println("\t\t\t" + field.getTerms()[k] + " - " + field.getTermsOccurrences()[k]); | |
} | |
} | |
// Printing found phrases | |
if (field.getTermSequences() != null) { | |
for (int k = 0; k < field.getTermSequences().length; k++) { | |
String[] terms = field.getTermSequences()[k]; | |
String sequence = ""; | |
for (String term : terms) { | |
sequence += term + " "; | |
} | |
System.out.println("\t\t\t" + sequence + " - " + field.getTermSequencesOccurrences()[k]); | |
} | |
} | |
} | |
} | |
} | |
} |
Properly managing and analyzing search results is essential for extracting the full potential from indexed data. This process extends beyond merely retrieving documents; it includes analyzing term occurrences, identifying matches within specific fields, and examining phrase patterns. The method outlined here enables you to examine results in a thorough and organized way, unlocking deeper data insights. Additionally, the library’s cross-platform compatibility guarantees smooth integration across different systems and environments, enhancing its flexibility for search-related tasks. By mastering this technique, you will be able to efficiently handle search results in Java and apply them to a wide variety of use cases, from data analysis to real-time document retrieval.
Earlier, we provided an in-depth guide on creating search reports using Java. To follow the full step-by-step process, make sure to read our detailed article on how to create search report using Java.