Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active August 8, 2021 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/4fab169c40c4a8d27581d156002daea3 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/4fab169c40c4a8d27581d156002daea3 to your computer and use it in GitHub Desktop.
Search and highlight text in all the documents of folder using Java
// Highlight the full text search results of multiple documents in a folder in Java
Index index = new Index("path/indexingFolder");
index.add("path/documentsFolderPath"); // Synchronous indexing documents from the specified folder
String query = "draw"; // Specify a search query
SearchResult result = index.search(query); // Searching in the index
for (int i = 0; i < result.getDocumentCount(); i++)
{
FoundDocument document = result.getFoundDocument(i);
String path = "path/Highlighted-"+ i +".html";
OutputAdapter outputAdapter = new FileOutputAdapter(path);
HtmlHighlighter highlighter = new HtmlHighlighter(outputAdapter); // Creating the highlighter
index.highlight(document, highlighter); // Generates HTML formatted output document with highlighted search results
}
// Search specified text in multiple PDF, Word, Excel, HTML documents with in a folder using Java
Index index = new Index("path/indexingFolder");
index.add("path/documentsFolderPath");
// Searching in index for specified text
SearchResult result = index.search("Draw");
for (int i = 0; i < result.getDocumentCount(); i++) {
FoundDocument document = result.getFoundDocument(i);
System.out.println("Document Path: " + document.getDocumentInfo().getFilePath());
System.out.println("Occurrence : " + document.getOccurrenceCount());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment