Mastering Efficient Document Search with GroupDocs.Search for Java
Finding the right passage inside thousands of PDFs, Word files, or spreadsheets can feel like searching for a needle in a haystack. How to create index quickly and retrieve that needle is what makes a document‑search solution valuable. In this tutorial you’ll learn how to use GroupDocs.Search for Java, a high‑performance java search library, to create index, add documents to index, and extract text from documents in multiple formats such as files, streams, strings, and structured data. By the end you’ll have a production‑ready indexing pipeline that scales to large document collections while keeping memory usage low.
Quick Answers
- What is the primary purpose? To how to create index and retrieve document content instantly.
- Which library should I use? The GroupDocs.Search for Java java search library.
- Can I output text to a file? Yes – the library provides output text to file adapters for HTML, plain text, and more.
- Is structured extraction supported? Absolutely – use the structured text extraction adapter to get field‑level data.
- Do I need a license? A trial license works for development; a permanent license is required for production deployments.
What You’ll Learn
- How to how to create index and add documents to index using GroupDocs.Search for Java.
- Techniques for output text to file, streams, strings, and structured formats.
- Performance‑optimization tips that keep indexing fast and memory‑efficient.
- Real‑world scenarios where these features shine, such as legal‑contract repositories and academic‑paper archives.
Why Use GroupDocs.Search for Java?
GroupDocs.Search supports 50+ input and output formats – including DOCX, XLSX, PPTX, PDF, HTML, and common image types – and can index multi‑gigabyte files without loading the whole file into memory. Benchmarks show that a 1 GB document collection can be indexed in under 2 minutes on a standard 8‑core server, while search queries return results in less than 100 ms.
Prerequisites
- Java Development Kit (JDK) 8 or newer.
- GroupDocs.Search for Java library (trial or licensed).
- Maven for dependency management.
- Basic Java I/O knowledge.
Setting Up GroupDocs.Search for Java
First, add the GroupDocs.Search Maven repository and dependency to your project’s pom.xml. This step ensures the library is available on the classpath.
Maven Setup
Add the following repository and dependency configurations in your pom.xml file:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/search/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-search</artifactId>
<version>25.4</version>
</dependency>
</dependencies>
For those preferring a direct download, you can obtain the latest version from GroupDocs.Search for Java releases.
License Acquisition
To use GroupDocs.Search in production, acquire a trial or permanent license from the official site. The trial license is unrestricted for development and testing.
How to create index java with custom settings
Index is the core class that represents a searchable collection of documents.IndexSettings configures options such as compression for the index.CompressionLevel defines the degree of compression applied to stored text.
Load the Index object with compression enabled, point it at a folder, and add all supported files. This direct‑answer paragraph tells you exactly what to do: instantiate Index with new Index("indexFolder", new IndexSettings().setCompressionLevel(CompressionLevel.High)), then call index.add("documentsFolder", true) to recursively index every supported file. The high‑compression mode reduces on‑disk size by up to 70 % while keeping search speed fast.
Creating the index is the foundation for any search‑driven application. The example below walks you through the process, explains each setting, and shows how to verify that the index was built successfully.
Index Creation and Document Indexing
Overview
The Index class is the core component that represents a searchable collection of documents. It stores inverted indexes, term dictionaries, and metadata required for fast look‑ups.
import com.groupdocs.search.*;
import java.io.ByteArrayOutputStream;
public class FeatureIndexCreation {
public static void main(String[] args) {
// Define the folder paths for indexing
String indexFolder = YOUR_DOCUMENT_DIRECTORY + "/OutputAdapters/Index";
String documentsFolder = YOUR_DOCUMENT_DIRECTORY + "/DocumentsPath"; // Adjust as needed
// Creating an index settings instance with compression enabled
IndexSettings settings = new IndexSettings();
settings.setTextStorageSettings(new TextStorageSettings(Compression.High));
// Creating the index in the specified folder
Index index = new Index(indexFolder, settings);
// Adding documents from the specified folder to the index
index.add(documentsFolder);
}
}
Explanation
- Index Settings: We enable high compression for text storage, optimizing disk space usage without compromising query speed.
- Adding Documents: The
index.add()method adds documents to index, scanning the folder recursively and handling all supported formats automatically.
How to output text to file, stream, string, and structured formats
After indexing, you often need to extract the raw or formatted text of a document. GroupDocs.Search offers four adapters that let you write the extracted content to a file, an in‑memory stream, a Java String, or a structured object model.
Document Text Output to File
FileOutputAdapter writes extracted document text to a file in the chosen format.
Overview
The FileOutputAdapter writes the extracted text in the chosen format (HTML, plain text, etc.) directly to a file on disk. This is useful for generating human‑readable reports or feeding downstream processing pipelines.
import com.groupdocs.search.*;
public class FeatureOutputToFile {
public static void main(String[] args) {
String indexFolder = YOUR_DOCUMENT_DIRECTORY + "/OutputAdapters/Index";
Index index = new Index(indexFolder);
// Assuming documents are already indexed, retrieve the first document
DocumentInfo[] documents = index.getIndexedDocuments();
if (documents.length > 0) {
DocumentInfo document = documents[0];
// Output document text to an HTML file
FileOutputAdapter fileOutputAdapter = new FileOutputAdapter(OutputFormat.Html, YOUR_OUTPUT_DIRECTORY + "/Text.html");
index.getDocumentText(document, fileOutputAdapter);
}
}
}
Explanation
- FileOutputAdapter: Converts the indexed document’s text into HTML and writes it to the specified file path, preserving basic formatting such as headings and tables.
Document Text Output to Stream
StreamOutputAdapter streams extracted document text into a ByteArrayOutputStream without creating a temporary file.
Overview
When you need the content only temporarily—e.g., to send it over HTTP or embed it in a web response—use the StreamOutputAdapter. It streams the text into a ByteArrayOutputStream, avoiding the overhead of creating a temporary file.
import com.groupdocs.search.*;
import java.io.ByteArrayOutputStream;
public class FeatureOutputToStream {
public static void main(String[] args) {
String indexFolder = YOUR_DOCUMENT_DIRECTORY + "/OutputAdapters/Index";
Index index = new Index(indexFolder);
// Assuming documents are already indexed, retrieve the first document
DocumentInfo[] documents = index.getIndexedDocuments();
if (documents.length > 0) {
DocumentInfo document = documents[0];
// Output document text to a stream in HTML format
ByteArrayOutputStream stream = new ByteArrayOutputStream();
StreamOutputAdapter streamOutputAdapter = new StreamOutputAdapter(OutputFormat.Html, stream);
index.getDocumentText(document, streamOutputAdapter);
}
}
}
Explanation
- StreamOutputAdapter: Streams the document’s text into a
ByteArrayOutputStream, allowing flexible handling without touching the file system.
Document Text Output to String
StringOutputAdapter captures the entire document text in a single String object.
Overview
For quick logging, debugging, or UI display, the StringOutputAdapter captures the entire document text in a single String object.
import com.groupdocs.search.*;
public class FeatureOutputToString {
public static void main(String[] args) {
String indexFolder = YOUR_DOCUMENT_DIRECTORY + "/OutputAdapters/Index";
Index index = new Index(indexFolder);
// Assuming documents are already indexed, retrieve the first document
DocumentInfo[] documents = index.getIndexedDocuments();
if (documents.length > 0) {
DocumentInfo document = documents[0];
// Output document text to a string in HTML format
StringOutputAdapter stringOutputAdapter = new StringOutputAdapter(OutputFormat.Html);
index.getDocumentText(document, stringOutputAdapter);
String result = stringOutputAdapter.getResult();
}
}
}
Explanation
- StringOutputAdapter: Captures the document’s text in a
String, making it easy to embed in logs, console output, or UI components.
Document Text Output to Structured Format
StructuredOutputAdapter returns a rich object model that contains paragraphs, tables, and custom metadata.
Overview
The StructuredOutputAdapter returns a rich object model that contains paragraphs, tables, and custom metadata. This format is ideal for downstream natural‑language‑processing (NLP) or data‑extraction workflows.
import com.groupdocs.search.*;
public class FeatureOutputToStructure {
public static void main(String[] args) {
String indexFolder = YOUR_DOCUMENT_DIRECTORY + "/OutputAdapters/Index";
Index index = new Index(indexFolder);
// Assuming documents are already indexed, retrieve the first document
DocumentInfo[] documents = index.getIndexedDocuments();
if (documents.length > 0) {
DocumentInfo document = documents[0];
// Output document text to a structured format like PlainText
StructuredOutputAdapter structuredOutputAdapter = new StructuredOutputAdapter(OutputFormat.PlainText);
index.getDocumentText(document, structuredOutputAdapter);
}
}
}
Explanation
- StructuredOutputAdapter: Extracts document text into a structured text extraction format, enabling fine‑grained analysis, field extraction, and integration with machine‑learning pipelines.
Common Issues and Solutions
| Issue | Cause | Fix |
|---|---|---|
| Index not created | Incorrect folder path or missing write permissions | Verify indexFolder exists and the application has write access |
| No documents returned | index.add() not called or wrong source folder | Ensure documentsFolder points to the correct directory and contains supported file types |
| Output file empty | Output adapter path invalid or missing directories | Create the target directory (YOUR_OUTPUT_DIRECTORY) before running |
| Memory spikes with large files | Loading entire file into memory | Use StreamOutputAdapter to process data incrementally |
Frequently Asked Questions
Q: Can I use GroupDocs.Search with other JVM languages like Kotlin or Scala?
A: Yes, the library is pure Java and works seamlessly with any JVM language.
Q: How does compression affect search speed?
A: High compression reduces disk usage by up to 70 % and adds only a minimal CPU overhead during indexing; query latency remains under 100 ms for typical workloads.
Q: Is it possible to update an existing index without rebuilding it?
A: Absolutely. Use index.add() for new files and index.remove() to delete outdated ones, allowing incremental updates.
Q: Which output format is best for natural‑language‑processing pipelines?
A: The PlainText result from the structured text extraction adapter provides clean, language‑agnostic content ideal for NLP tasks.
Q: Do I need a license for development and testing?
A: A free trial license suffices for development and evaluation; production deployments require a purchased license.
Conclusion
You now have a complete, production‑ready workflow for how to create index, add documents, and extract their text in every format you might need. Start by configuring the Index with compression, add your document collection, and choose the appropriate output adapter for your downstream scenario—whether that’s generating HTML reports, feeding an NLP model, or streaming content to a web client. Experiment with the incremental update APIs to keep your index fresh without costly rebuilds, and you’ll enjoy fast, reliable search across any document repository.
Last Updated: 2026-06-27
Tested With: GroupDocs.Search 25.4 for Java
Author: GroupDocs