Opanowanie wydajnego wyszukiwania dokumentów z GroupDocs.Search dla Javy
Finding the right passage inside thousands of PDFs, Word files, or spreadsheets can feel like searching for a needle in a haystack. Jak utworzyć indeks 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 utworzyć indeks, dodawać dokumenty do indeksu, 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.
Szybkie odpowiedzi
- Jaki jest główny cel? Aby utworzyć indeks i retrieve document content instantly.
- Którą bibliotekę powinienem użyć? The GroupDocs.Search for Java java search library.
- Czy mogę wyprowadzić tekst do pliku? Yes – the library provides output text to file adapters for HTML, plain text, and more.
- Czy obsługiwana jest strukturalna ekstrakcja? Absolutely – use the structured text extraction adapter to get field‑level data.
- Czy potrzebna jest licencja? A trial license works for development; a permanent license is required for production deployments.
Czego się nauczysz
- Jak utworzyć indeks i dodawać dokumenty do indeksu 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.
Dlaczego używać GroupDocs.Search dla Javy?
GroupDocs.Search obsługuje ponad 50 formatów wejściowych i wyjściowych – 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.
Wymagania wstępne
- Java Development Kit (JDK) 8 lub nowszy.
- GroupDocs.Search for Java library (trial or licensed).
- Maven do zarządzania zależnościami.
- Basic Java I/O knowledge.
Konfiguracja GroupDocs.Search dla Javy
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.
Jak utworzyć indeks w Javie z niestandardowymi ustawieniami
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.
Tworzenie indeksu i indeksowanie dokumentów
Przegląd
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);
}
}
Wyjaśnienie
- Ustawienia indeksu: We enable high compression for text storage, optimizing disk space usage without compromising query speed.
- Dodawanie dokumentów: The
index.add()method adds documents to index, scanning the folder recursively and handling all supported formats automatically.
Jak wyprowadzić tekst do pliku, strumienia, ciągu znaków i formatów strukturalnych
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.
Wyjście tekstu dokumentu do pliku
FileOutputAdapter writes extracted document text to a file in the chosen format.
Przegląd
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);
}
}
}
Wyjaśnienie
- 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.
Wyjście tekstu dokumentu do strumienia
StreamOutputAdapter streams extracted document text into a ByteArrayOutputStream without creating a temporary file.
Przegląd
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);
}
}
}
Wyjaśnienie
- StreamOutputAdapter: Streams the document’s text into a
ByteArrayOutputStream, allowing flexible handling without touching the file system.
Wyjście tekstu dokumentu do ciągu znaków
StringOutputAdapter captures the entire document text in a single String object.
Przegląd
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();
}
}
}
Wyjaśnienie
- StringOutputAdapter: Captures the document’s text in a
String, making it easy to embed in logs, console output, or UI components.
Wyjście tekstu dokumentu do formatu strukturalnego
StructuredOutputAdapter returns a rich object model that contains paragraphs, tables, and custom metadata.
Przegląd
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);
}
}
}
Wyjaśnienie
- StructuredOutputAdapter: Extracts document text into a structured text extraction format, enabling fine‑grained analysis, field extraction, and integration with machine‑learning pipelines.
Częste problemy i rozwiązania
| Problem | Przyczyna | Rozwiązanie |
|---|---|---|
| Indeks nie został utworzony | Nieprawidłowa ścieżka folderu lub brak uprawnień do zapisu | Verify indexFolder exists and the application has write access |
| Brak zwróconych dokumentów | index.add() nie został wywołany lub nieprawidłowy folder źródłowy | Ensure documentsFolder points to the correct directory and contains supported file types |
| Plik wyjściowy pusty | Ścieżka adaptera wyjściowego nieprawidłowa lub brakujące katalogi | Create the target directory (YOUR_OUTPUT_DIRECTORY) before running |
| Wzrost zużycia pamięci przy dużych plikach | Ładowanie całego pliku do pamięci | Use StreamOutputAdapter to process data incrementally |
Najczęściej zadawane pytania
Q: Czy mogę używać GroupDocs.Search z innymi językami JVM, takimi jak Kotlin lub Scala?
A: Yes, the library is pure Java and works seamlessly with any JVM language.
Q: Jak kompresja wpływa na szybkość wyszukiwania?
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: Czy można zaktualizować istniejący indeks bez jego przebudowy?
A: Absolutely. Use index.add() for new files and index.remove() to delete outdated ones, allowing incremental updates.
Q: Który format wyjściowy jest najlepszy dla pipeline’ów przetwarzania języka naturalnego?
A: The PlainText result from the structured text extraction adapter provides clean, language‑agnostic content ideal for NLP tasks.
Q: Czy potrzebna jest licencja do rozwoju i testowania?
A: A free trial license suffices for development and evaluation; production deployments require a purchased license.
Podsumowanie
You now have a complete, production‑ready workflow for jak utworzyć indeks, 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