public class Index extends Object implements Closeable
Learn more
The example demonstrates a typical usage of the class.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
String query = "Einstein";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
SearchResult result = index.search(query); // Searching in index
Constructor and Description |
---|
()
Initializes a new instance of the
Index class in memory. |
(IndexSettings settings)
Initializes a new instance of the
Index class in memory with particular index settings. |
(String indexFolder)
Initializes a new instance of the
Index class. |
(String indexFolder,
boolean overwriteIfExists)
Initializes a new instance of the
Index class. |
(String indexFolder,
IndexSettings settings)
Initializes a new instance of the
Index class. |
(String indexFolder,
IndexSettings settings,
boolean overwriteIfExists)
Initializes a new instance of the
Index class. |
Modifier and Type | Method and Description |
---|---|
void |
add(Document[] documents,
IndexingOptions options)
Performs indexing operation.
|
void |
add(ExtractedData[] data,
IndexingOptions options)
Performs indexing operation.
|
void |
add(String path)
Performs indexing operation.
|
void |
add(String[] paths)
Performs indexing operation.
|
void |
add(String[] paths,
IndexingOptions options)
Performs indexing operation.
|
void |
add(String path,
IndexingOptions options)
Performs indexing operation.
|
void |
changeAttributes(AttributeChangeBatch batch)
Applies the specified batch of attribute changes to indexed documents without reindexing during the update operation.
|
void |
close()
Releases all resources used by the
Index . |
protected static AttributeChangeBatch |
createAttributeChangeBatch() |
DeleteResult |
delete(String[] paths,
UpdateOptions options)
Deletes indexed files or folders from the index.
|
DeleteResult |
delete(UpdateOptions options,
String[] documentKeys)
Deletes documents indexed from streams or structures.
|
protected void |
finalize()
Releases all resources used by the
Index . |
String[] |
getAttributes(String path)
Gets all the attributes associated with the specified indexed document.
|
DictionaryRepository |
getDictionaries()
Gets the dictionary repository.
|
void |
getDocumentText(DocumentInfo documentInfo,
OutputAdapter adapter)
Generates HTML formatted text for indexed document and transfers it through the output adapter.
|
void |
getDocumentText(DocumentInfo documentInfo,
OutputAdapter adapter,
TextOptions options)
Generates HTML formatted text for indexed document and transfers it through the output adapter.
|
EventHub |
getEvents()
Gets the event hub for subscribing to events.
|
DocumentInfo[] |
getIndexedDocumentItems(DocumentInfo documentInfo)
Gets an array of a document items.
|
DocumentInfo[] |
getIndexedDocuments()
Gets an array of all indexed documents.
|
String[] |
getIndexedPaths()
Gets an array of indexed paths - documents or folders.
|
protected static String |
getIndexFolder(Index index) |
protected static com.aspose.ms.System.Guid |
getIndexId(Index index) |
IndexInfo |
getIndexInfo()
Gets the basic information on the index.
|
IndexingReport[] |
getIndexingReports()
Gets the reports on indexing operations.
|
IndexSettings |
getIndexSettings()
Gets the index settings.
|
protected static IndexStatus |
getIndexStatus(Index index) |
protected static int |
getLRMSRC() |
IndexRepository |
getRepository()
Gets the index repository object if the index is contained in it.
|
SearchReport[] |
getSearchReports()
Gets the reports on search operations.
|
protected static String |
getSLRM() |
protected static String |
getVersion(Index index) |
void |
highlight(FoundDocument document,
Highlighter highlighter)
Generates HTML formatted text with highlighted found terms.
|
void |
highlight(FoundDocument document,
Highlighter highlighter,
HighlightOptions options)
Generates HTML formatted text with highlighted found terms.
|
protected static boolean |
isL() |
void |
merge(Index index,
MergeOptions options)
Merges the specified index into the current index.
|
void |
merge(IndexRepository repository,
MergeOptions options)
Merges indexes from the specified index repository into the current index.
|
boolean |
notifyIndex(Notification notification)
Passes the specified notification object to the index to perform the notification.
|
void |
optimize()
Minimizes the number of index segments by merging them one with another.
|
void |
optimize(MergeOptions options)
Minimizes the number of index segments by merging them one with another.
|
protected static void |
raiseErrorOccurred(EventHub events,
String message,
boolean isCritical) |
ImageSearchResult |
search(SearchImage image,
ImageSearchOptions options)
Performs a reverse image search in the index.
|
SearchResult |
search(SearchQuery query)
Searches in index.
|
SearchResult |
search(SearchQuery query,
SearchOptions options)
Searches in index.
|
SearchResult |
search(String query)
Searches in index.
|
SearchResult |
search(String query,
SearchOptions options)
Searches in index.
|
SearchResult |
searchNext(ChunkSearchToken chunkSearchToken)
Continues the chunk search started with method Search.
|
SearchResult |
searchNext(ChunkSearchToken chunkSearchToken,
Cancellation cancellation)
Continues the chunk search started with method Search.
|
void |
update()
Re-indexes documents that have been changed or deleted since last update.
|
void |
update(UpdateOptions options)
Re-indexes documents that have been changed or deleted since last update.
|
public Index()
Index
class in memory.
The example demonstrates how to create index in memory without saving files to disk.
Index index = new Index();
public Index(IndexSettings settings)
Index
class in memory with particular index settings.settings
- The index settings object.
The example demonstrates how to create index in memory without saving files to disk with particular index settings.
IndexSettings settings = new IndexSettings();
settings.setIndexType(IndexType.CompactIndex);
Index index = new Index(settings);
public Index(String indexFolder)
Index
class.
Creates a new or opens an existing index on disk.indexFolder
- The index folder path.
The example demonstrates how to create an index on a disk or open an existing index.
String indexFolder = "c:\\MyIndex\\";
Index index = new Index(indexFolder);
public Index(String indexFolder, IndexSettings settings)
Initializes a new instance of the Index
class.
Creates a new index with particular settings or opens an existing index on disk.
indexFolder
- The index folder path.settings
- The index settings object.
The example demonstrates how to create an index on a disk with particular index settings.
String indexFolder = "c:\\MyIndex\\";
IndexSettings settings = new IndexSettings();
settings.setIndexType(IndexType.CompactIndex);
Index index = new Index(indexFolder, settings);
public Index(String indexFolder, boolean overwriteIfExists)
Initializes a new instance of the Index
class.
Loads an existing index from disk if overwriteIfExists
is false
;
creates a new index on disk otherwise.
indexFolder
- The index folder path.overwriteIfExists
- The flag of overwriting the index folder.
The example demonstrates how to create a new index in a folder that already contains another index.
String indexFolder = "c:\\MyIndex\\";
Index index = new Index(indexFolder, true);
public Index(String indexFolder, IndexSettings settings, boolean overwriteIfExists)
Initializes a new instance of the Index
class.
Loads an existing index from disk if overwriteIfExists
is false
;
creates a new index on disk with particular index settings otherwise.
indexFolder
- The index folder path.settings
- The index settings object.overwriteIfExists
- The flag of overwriting the index folder.
The example demonstrates how to create an index on a disk with particular index settings.
String indexFolder = "c:\\MyIndex\\";
IndexSettings settings = new IndexSettings();
settings.setIndexType(IndexType.CompactIndex);
Index index = new Index(indexFolder, settings, true);
public final EventHub getEvents()
Gets the event hub for subscribing to events.
public final IndexInfo getIndexInfo()
Gets the basic information on the index.
public final IndexRepository getRepository()
Gets the index repository object if the index is contained in it.
public final IndexSettings getIndexSettings()
Gets the index settings.
public final DictionaryRepository getDictionaries()
Gets the dictionary repository.
protected static com.aspose.ms.System.Guid getIndexId(Index index)
protected static IndexStatus getIndexStatus(Index index)
protected static void raiseErrorOccurred(EventHub events, String message, boolean isCritical)
protected static boolean isL()
protected static String getSLRM()
protected static int getLRMSRC()
protected static AttributeChangeBatch createAttributeChangeBatch()
protected void finalize() throws Throwable
Releases all resources used by the Index
.
public final void close()
Releases all resources used by the Index
.
close
in interface Closeable
close
in interface AutoCloseable
public final void add(String path)
Performs indexing operation. Adds a file or folder by an absolute or relative path. Documents from all subfolders will be indexed.
path
- The path to a file or folder to be indexed.
The example demonstrates how to add documents to an index.
String indexFolder = "c:\\MyIndex\\";
String folderPath = "c:\\MyDocuments\\";
String filePath = "c:\\Documents\\MyFile.txt";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(folderPath); // Indexing documents in the specified folder
index.add(filePath); // Indexing the specified document
public final void add(String path, IndexingOptions options)
Performs indexing operation. Adds a file or folder by an absolute or relative path. Documents from all subfolders will be indexed.
path
- The path to a file or folder to be indexed.options
- The indexing options.
The example demonstrates how to add documents to an index with particular indexing options.
String indexFolder = "c:\\MyIndex\\";
String folderPath = "c:\\MyDocuments\\";
String filePath = "c:\\Documents\\MyFile.txt";
Index index = new Index(indexFolder); // Creating index in the specified folder
IndexingOptions options = new IndexingOptions();
options.setThreads(2); // Setting the number of indexing threads
index.add(folderPath, options); // Indexing documents in the specified folder
index.add(filePath, options); // Indexing the specified document
public final void add(String[] paths)
Performs indexing operation. Adds files or folders by an absolute or relative path. Documents from all subfolders will be indexed.
paths
- The paths to a files or folders to be indexed.
The example demonstrates how to add documents to an index.
String indexFolder = "c:\\MyIndex\\";
String folderPath = "c:\\MyDocuments\\";
String filePath = "c:\\Documents\\MyFile.txt";
Index index = new Index(indexFolder); // Creating index in the specified folder
String[] paths = new String[] { folderPath, filePath };
index.add(paths); // Indexing documents at the specified paths
public final void add(String[] paths, IndexingOptions options)
Performs indexing operation. Adds files or folders by an absolute or relative path. Documents from all subfolders will be indexed.
paths
- The paths to a files or folders to be indexed.options
- The indexing options.
The example demonstrates how to add documents to an index with particular indexing options.
String indexFolder = "c:\\MyIndex\\";
String folderPath = "c:\\MyDocuments\\";
String filePath = "c:\\Documents\\MyFile.txt";
Index index = new Index(indexFolder); // Creating index in the specified folder
IndexingOptions options = new IndexingOptions();
options.setThreads(2); // Setting the number of indexing threads
String[] paths = new String[] { folderPath, filePath };
index.add(paths, options); // Indexing documents at the specified paths
public final void add(Document[] documents, IndexingOptions options)
Performs indexing operation. Adds documents from file system, stream or structure.
documents
- The documents from file system, stream or structure.options
- The indexing options.public final void add(ExtractedData[] data, IndexingOptions options)
Performs indexing operation. Adds the extracted data to the index.
data
- The extracted data.options
- The indexing options.public final void update()
Re-indexes documents that have been changed or deleted since last update. Adds new files that have been added to the indexed folders.
The example demonstrates how to update an index.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
// Delete documents from the documents folder or modify them or add new documents to the folder
index.update(); // Updating the index
public final void update(UpdateOptions options)
Re-indexes documents that have been changed or deleted since last update. Adds new files that have been added to the indexed folders.
options
- The update options.
The example demonstrates how to update an index with particular update options.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
// Delete documents from the documents folder or modify them or add new documents to the folder
UpdateOptions options = new UpdateOptions();
options.setThreads(2); // Setting the number of indexing threads
index.update(options); // Updating the index
public final IndexingReport[] getIndexingReports()
Gets the reports on indexing operations.
The example demonstrates how to get indexing reports.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
IndexingReport[] reports = index.getIndexingReports(); // Getting indexing reports
public final SearchReport[] getSearchReports()
Gets the reports on search operations.
The example demonstrates how to get search reports.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
String query1 = "Einstein";
String query2 = "Newton";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
SearchResult result1 = index.search(query1); // Searching
SearchResult result2 = index.search(query2);
SearchResult result3 = index.search(query1 + " & " + query2);
SearchReport[] reports = index.getSearchReports(); // Getting search reports
public final SearchResult search(String query)
Searches in index.
query
- The search query.
The following example demonstrates how to perform simple search.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
String query = "Einstein";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
SearchResult result = index.search(query); // Searching
The following example demonstrates how to perform Regex search.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
String query = "^[0-9]{3,}"; // The caret symbol at the beginning of the search query tells the index that it is a Regex query
SearchResult result = index.search(query); // Searching
The following example demonstrates how to perform faceted search.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
String query = "content:Newton"; // The word before the colon in the query means the document field name to search
SearchResult result = index.search(query); // Searching
public final SearchResult search(String query, SearchOptions options)
Searches in index.
query
- The search query.options
- The search options.
The following example demonstrates how to perform fuzzy search.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
SearchOptions options = new SearchOptions();
options.getFuzzySearch().setEnabled(true); // Enabling the fuzzy search
options.getFuzzySearch().setFuzzyAlgorithm(new TableDiscreteFunction(1)); // Setting the number of possible differences for each word
// Double quotes at the beginning and end tells the index that it is phrase search query
String query = "\"The Pursuit of Happiness\"";
SearchResult result = index.search(query, options); // Searching
The following example demonstrates how to perform synonym search.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
SearchOptions options = new SearchOptions();
options.setUseSynonymSearch(true); // Enabling the synonym search
String query = "cry";
SearchResult result = index.search(query, options); // Searching
public final SearchResult search(SearchQuery query)
Searches in index.
query
- The search query.
The following example demonstrates how to perform search using query in object form.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
// Creating subquery 1
SearchQuery subquery1 = SearchQuery.createWordQuery("accommodation");
subquery1.setSearchOptions(new SearchOptions()); // Setting search options only for subquery 1
subquery1.getSearchOptions().getFuzzySearch().setEnabled(true); // Enabling the fuzzy search
subquery1.getSearchOptions().getFuzzySearch().setFuzzyAlgorithm(new TableDiscreteFunction(3)); // Setting maximum number of differences
// Creating subquery 2
SearchQuery subquery2 = SearchQuery.createNumericRangeQuery(1, 1000000);
// Creating subquery 3
SearchQuery subquery3 = SearchQuery.createRegexQuery("(.)\\1");
// Combining subqueries into one query
SearchQuery query = SearchQuery.createPhraseSearchQuery(subquery1, subquery2, subquery3);
SearchResult result = index.search(query); // Searching
public final SearchResult search(SearchQuery query, SearchOptions options)
Searches in index.
query
- The search query.options
- The search options.
The following example demonstrates how to perform search using query in object form.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
// Creating subquery of date range search
SearchQuery subquery1 = SearchQuery.createDateRangeQuery(new Date(2011, 6, 17), new Date(2013, 1, 1));
// Creating subquery of wildcard with number of missed words from 0 to 2
SearchQuery subquery2 = SearchQuery.createWildcardQuery(0, 2);
// Creating subquery of simple word
SearchQuery subquery3 = SearchQuery.createWordQuery("birth");
subquery3.setSearchOptions(new SearchOptions()); // Setting search options only for subquery 3
subquery3.getSearchOptions().getFuzzySearch().setEnabled(true);
subquery3.getSearchOptions().getFuzzySearch().setFuzzyAlgorithm(new TableDiscreteFunction(1));
// Combining subqueries into one query
SearchQuery query = SearchQuery.createPhraseSearchQuery(subquery1, subquery2, subquery3);
// Creating search options object with increased capacity of found occurrences
SearchOptions options = new SearchOptions(); // Overall search options
options.setMaxOccurrenceCountPerTerm(1000000);
options.setMaxTotalOccurrenceCount(10000000);
SearchResult result = index.search(query, options); // Searching
public final ImageSearchResult search(SearchImage image, ImageSearchOptions options)
Performs a reverse image search in the index.
image
- The image to search.options
- The image search options.public final SearchResult searchNext(ChunkSearchToken chunkSearchToken)
Continues the chunk search started with method Search.
chunkSearchToken
- The chunk search token.
The example demonstrates how to perform chunk search.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
String query = "Einstein";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
SearchOptions options = new SearchOptions();
options.setChunkSearch(true); // Enabling chunk search
SearchResult result = index.search(query, options); // Starting chunk search
System.out.println("Document count: " + result.getDocumentCount());
System.out.println("Occurrence count: " + result.getOccurrenceCount());
while (result.getNextChunkSearchToken() != null) {
result = index.searchNext(result.getNextChunkSearchToken()); // Continuing chunk search
System.out.println("Document count: " + result.getDocumentCount());
System.out.println("Occurrence count: " + result.getOccurrenceCount());
}
public final SearchResult searchNext(ChunkSearchToken chunkSearchToken, Cancellation cancellation)
Continues the chunk search started with method Search.
chunkSearchToken
- The chunk search token.cancellation
- The cancellation object.
The example demonstrates how to perform search using query in object form.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder = "c:\\MyDocuments\\";
String query = "Einstein";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder); // Indexing documents from the specified folder
Cancellation cancellation = new Cancellation(); // This cancellation object aborts all search continuations if canceled
SearchOptions options = new SearchOptions();
options.setChunkSearch(true); // Enabling chunk search
options.setCancellation(cancellation);
SearchResult result = index.search(query, options); // Starting chunk search
System.out.println("Document count: " + result.getDocumentCount());
System.out.println("Occurrence count: " + result.getOccurrenceCount());
while (result.getNextChunkSearchToken() != null) {
result = index.searchNext(result.getNextChunkSearchToken(), cancellation); // Continuing chunk search
System.out.println("Document count: " + result.getDocumentCount());
System.out.println("Occurrence count: " + result.getOccurrenceCount());
}
public final void optimize()
Minimizes the number of index segments by merging them one with another. This operation improves search performance.
The example demonstrates how to merge segments of an index.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder1 = "c:\\MyDocuments1\\";
String documentsFolder2 = "c:\\MyDocuments2\\";
String documentsFolder3 = "c:\\MyDocuments3\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder1); // Indexing documents from the specified folder
index.add(documentsFolder2); // Each call to AddToIndex creates at least one new segment in the index
index.add(documentsFolder3);
// Merging segments of the index
index.optimize();
public final void optimize(MergeOptions options)
Minimizes the number of index segments by merging them one with another. This operation improves search performance.
options
- The merge options.
The example demonstrates how to merge segments of an index with particular merge options.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder1 = "c:\\MyDocuments1\\";
String documentsFolder2 = "c:\\MyDocuments2\\";
String documentsFolder3 = "c:\\MyDocuments3\\";
Index index = new Index(indexFolder); // Creating index in the specified folder
index.add(documentsFolder1); // Indexing documents from the specified folder
index.add(documentsFolder2); // Each call to AddToIndex creates at least one new segment in the index
index.add(documentsFolder3);
MergeOptions options = new MergeOptions();
options.setAsync(true); // Asynchronous operation
options.setCancellation(new Cancellation()); // Creating cancellation object
// Merging segments of the index
index.optimize(options); // This method will return before the operation is completed
options.getCancellation().cancelAfter(10000); // Setting maximum duration of the operation to 10 seconds
public final void merge(Index index, MergeOptions options)
Merges the specified index into the current index. Note that the other index will not be changed.
If the other index has a previous version, it must be updated before merging with IndexUpdater
.
index
- The index to merge into.options
- The merge options.
The example demonstrates how to merge an index into the current index.
String indexFolder1 = "c:\\MyIndex1\\";
String indexFolder2 = "c:\\MyIndex2\\";
String documentsFolder1 = "c:\\MyDocuments1\\";
String documentsFolder2 = "c:\\MyDocuments2\\";
Index index1 = new Index(indexFolder1); // Creating index1
index1.add(documentsFolder1); // Indexing documents
Index index2 = new Index(indexFolder2); // Creating index2
index2.add(documentsFolder2); // Indexing documents
MergeOptions options = new MergeOptions();
options.setCancellation(new Cancellation()); // Creating cancellation object
// Merging index2 into index1. Note that index2 files will not be changed.
index1.merge(index2, options);
public final void merge(IndexRepository repository, MergeOptions options)
Merges indexes from the specified index repository into the current index. Note that indexes in the repository will not be changed.
If other indexes have a previous version, they must be updated before merging with IndexUpdater
.
repository
- The index repository to merge into.options
- The merge options.
The example demonstrates how to merge an index repository into the current index.
String indexFolder1 = "c:\\MyIndex1\\";
String indexFolder2 = "c:\\MyIndex2\\";
String indexFolder3 = "c:\\MyIndex3\\";
String documentsFolder1 = "c:\\MyDocuments1\\";
String documentsFolder2 = "c:\\MyDocuments2\\";
String documentsFolder3 = "c:\\MyDocuments3\\";
Index index1 = new Index(indexFolder1); // Creating index1
index1.add(documentsFolder1); // Indexing documents
IndexRepository repository = new IndexRepository(); // Creating index repository
Index index2 = repository.create(indexFolder2); // Creating index2
index2.add(documentsFolder2); // Indexing documents
Index index3 = repository.create(indexFolder3); // Creating index3
index3.add(documentsFolder3); // Indexing documents
MergeOptions options = new MergeOptions();
options.setCancellation(new Cancellation()); // Creating cancellation object
// Merging all indexes in the index repository into index1. Note that index2 and index3 will not be changed.
index1.merge(repository, options);
public final void highlight(FoundDocument document, Highlighter highlighter)
Generates HTML formatted text with highlighted found terms.
document
- The found document.highlighter
- The search result highlighter.
The example demonstrates how to highlight occurrences in HTML formatted text.
String indexFolder = "c:\\MyIndex\\";
String documentFolder = "c:\\MyDocuments\\";
// Creating an index
Index index = new Index(indexFolder);
// Indexing documents from the specified folder
index.add(documentFolder);
// Search for the word 'eternity'
SearchResult result = index.search("eternity");
// Highlighting occurrences in text
if (result.getDocumentCount() > 0) {
FoundDocument document = result.getFoundDocument(0); // Getting the first found document
OutputAdapter outputAdapter = new FileOutputAdapter("c:\\Highlighted.html"); // Creating an output adapter to the file
Highlighter highlighter = new HtmlHighlighter(outputAdapter); // Creating the highlighter object
index.highlight(document, highlighter); // Generating HTML formatted text with highlighted occurrences
}
public final void highlight(FoundDocument document, Highlighter highlighter, HighlightOptions options)
Generates HTML formatted text with highlighted found terms.
document
- The found document.highlighter
- The search result highlighter.options
- The highlight options.
The example demonstrates how to highlight occurrences in HTML formatted text.
String indexFolder = "c:\\MyIndex\\";
String documentFolder = "c:\\MyDocuments\\";
// Creating an index
Index index = new Index(indexFolder);
// Indexing documents from the specified folder
index.add(documentFolder);
// Search for the word 'eternity'
SearchResult result = index.search("eternity");
// Highlighting occurrences in text
if (result.getDocumentCount() > 0) {
FoundDocument document = result.getFoundDocument(0); // Getting the first found document
OutputAdapter outputAdapter = new FileOutputAdapter("c:\\Highlighted.html"); // Creating an output adapter to the file
Highlighter highlighter = new HtmlHighlighter(outputAdapter); // Creating the highlighter object
HighlightOptions options = new HighlightOptions(); // Creating the highlight options object
options.setTermsBefore(5);
options.setTermsAfter(5);
options.setTermsTotal(15);
index.highlight(document, highlighter, options); // Generating HTML formatted text with highlighted occurrences
}
public final DocumentInfo[] getIndexedDocuments()
Gets an array of all indexed documents.
The example demonstrates how to get a list of indexed documents from an index.
String indexFolder = "c:\\MyIndex\\";
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);
// Getting list of indexed documents
DocumentInfo[] documents = index.getIndexedDocuments();
public final DocumentInfo[] getIndexedDocumentItems(DocumentInfo documentInfo)
Gets an array of a document items.
documentInfo
- A document info.
The example demonstrates how to get a list of items of an indexed document from an index.
String indexFolder = "c:\\MyIndex\\";
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);
// Getting list of indexed documents
DocumentInfo[] documents = index.getIndexedDocuments();
for (int i = 0; i < documents.length; i++) {
DocumentInfo document = documents[i];
System.out.println(document.getFilePath());
DocumentInfo[] items = index.getIndexedDocumentItems(document); // Getting list of document items
for (int j = 0; j < items.length; j++) {
DocumentInfo item = items[j];
System.out.println("\t" + item.getInnerPath());
}
}
public final void getDocumentText(DocumentInfo documentInfo, OutputAdapter adapter)
Generates HTML formatted text for indexed document and transfers it through the output adapter.
documentInfo
- The indexed document info.adapter
- The output adapter.
The example demonstrates how to get the text of an indexed document from an index.
String indexFolder = "c:\\MyIndex\\";
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);
// Getting list of indexed documents
DocumentInfo[] documents = index.getIndexedDocuments();
// Getting a document text
if (documents.length > 0) {
FileOutputAdapter outputAdapter = new FileOutputAdapter("C:\\Text.html");
index.getDocumentText(documents[0], outputAdapter);
}
public final void getDocumentText(DocumentInfo documentInfo, OutputAdapter adapter, TextOptions options)
Generates HTML formatted text for indexed document and transfers it through the output adapter.
documentInfo
- The indexed document info.adapter
- The output adapter.options
- The text retrieving options.public final String[] getIndexedPaths()
Gets an array of indexed paths - documents or folders.
public final DeleteResult delete(String[] paths, UpdateOptions options)
Deletes indexed files or folders from the index. Then updates the index without deleted paths. Note that an individual document cannot be deleted from the index if it was added to the index as part of a folder.
paths
- The paths to files or folders to delete.options
- The update options.
The example demonstrates how to delete indexed paths from an index.
String indexFolder = "c:\\MyIndex\\";
String documentsFolder1 = "c:\\MyDocuments\\";
String documentsFolder2 = "c:\\MyDocuments2\\";
// Creating an index in the specified folder
Index index = new Index(indexFolder);
// Indexing documents from the specified folders
index.add(documentsFolder1);
index.add(documentsFolder2);
// Getting indexed paths from the index
String[] indexedPaths1 = index.getIndexedPaths();
// Writing indexed paths to the console
System.out.println("Indexed paths:");
for (String path : indexedPaths1) {
System.out.println("\t" + path);
}
// Deleting index path from the index
DeleteResult deleteResult = index.delete(new String[] { documentsFolder1 }, new UpdateOptions());
// Getting indexed paths after deletion
String[] indexedPaths2 = index.getIndexedPaths();
System.out.println("\nDeleted paths: " + deleteResult.getSuccessCount());
System.out.println("\nIndexed paths:");
for (String path : indexedPaths2) {
System.out.println("\t" + path);
}
public final DeleteResult delete(UpdateOptions options, String[] documentKeys)
Deletes documents indexed from streams or structures. Then updates the index without deleted documents.
options
- The update options.documentKeys
- The keys of documents added from streams or structures.public final boolean notifyIndex(Notification notification)
Passes the specified notification object to the index to perform the notification.
notification
- The notification object.true
if the notification was successfully performed; otherwise false
.public final void changeAttributes(AttributeChangeBatch batch)
Applies the specified batch of attribute changes to indexed documents without reindexing during the update operation.
batch
- The attribute change batch.