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
```
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.
Parameters:
Parameter
Type
Description
indexFolder
java.lang.String
The index folder path.
overwriteIfExists
boolean
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.
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);
```
|
getEvents()
public final EventHub getEvents()
Gets the event hub for subscribing to events.
Returns:EventHub - The event hub for subscribing to events.
getIndexInfo()
public final IndexInfo getIndexInfo()
Gets the basic information on the index.
Returns:IndexInfo - The basic information on the index.
getRepository()
public final IndexRepository getRepository()
Gets the index repository object if the index is contained in it.
Performs indexing operation.
Adds a file or folder by an absolute or relative path.
Documents from all subfolders will be indexed.
Parameters:
Parameter
Type
Description
path
java.lang.String
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
```
|
add(String path, IndexingOptions options)
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.
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
```
|
add(String[] paths)
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.
Parameters:
Parameter
Type
Description
paths
java.lang.String[]
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
```
|
add(String[] paths, IndexingOptions options)
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.
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
```
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
```
update(UpdateOptions options)
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.
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
```
|
getIndexingReports()
public final IndexingReport[] getIndexingReports()
Gets the reports on indexing operations.
Returns:
com.groupdocs.search.common.IndexingReport[] - The indexing reports.
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
```
getSearchReports()
public final SearchReport[] getSearchReports()
Gets the reports on search operations.
Returns:
com.groupdocs.search.common.SearchReport[] - The search reports.
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
```
search(String query)
public final SearchResult search(String query)
Searches in index.
Parameters:
Parameter
Type
Description
query
java.lang.String
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
```
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
```
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
```
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();
```
optimize(MergeOptions options)
public final void optimize(MergeOptions options)
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 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
```
|
merge(Index index, MergeOptions options)
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
.
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
}
```
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
}
```
|
getIndexedDocuments()
public final DocumentInfo[] getIndexedDocuments()
Gets an array of all indexed documents.
Returns:
com.groupdocs.search.results.DocumentInfo[] - 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();
```
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());
}
}
```
|
Returns:
com.groupdocs.search.results.DocumentInfo[] - An array of a document items.
GetIndexedDocument(String documentKey)
public DocumentInfo GetIndexedDocument(String documentKey)
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);
}
```
Gets an array of indexed paths - documents or folders.
Returns:
java.lang.String[] - An array of indexed paths.
delete(String[] paths, UpdateOptions options)
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.
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);
}
```
|
Returns:DeleteResult - An object describing the result of deleting files or folders from the index.