GroupDocs.Search allows indexing documents from various sources:
From files in the file system.
From a stream.
From a data structure as an array of fields.
The library also allows indexing from all presented sources with lazy initialization.
Please note that the update operation automatically generates a list of changed files only when indexing from the local file system. When indexing from streams or structures, documents cannot be updated with the update operation. To update documents from these sources, you must re-index the modified documents by passing their keys and updated data to the add method.
Indexing from a file
It should be borne in mind that the add method with the parameter of type Document[] allows indexing only documents individually, and not entire folders. The advantage of using this method overload is that you can add attributes and additional fields to the indexed document before calling the add method. The following example demonstrates how to index a document from a file.
Java
StringindexFolder="c:\\MyIndex";StringdocumentFilePath="c:\\MyDocuments\\ExampleDocument.pdf"";// Creating an index
IndexSettingssettings=newIndexSettings();settings.setUseRawTextExtraction(false);Indexindex=newIndex(indexFolder,settings);// Creating a document object
Documentdocument=Document.createFromFile(documentFilePath);Document[]documents=newDocument[]{document,};// Indexing document from the file
IndexingOptionsoptions=newIndexingOptions();index.add(documents,options);
Indexing from a stream
The following example demonstrates how to index a document from a stream.
Java
StringindexFolder="c:\\MyIndex";StringdocumentFilePath="c:\\MyDocuments\\ExampleDocument.pdf";// Creating an index
Indexindex=newIndex(indexFolder);// Creating a document object
InputStreamstream=newFileInputStream(documentFilePath);// Opening a stream
Documentdocument=Document.createFromStream(documentFilePath,newDate(),".pdf",stream);Document[]documents=newDocument[]{document,};// Indexing document from the stream
IndexingOptionsoptions=newIndexingOptions();index.add(documents,options);// Closing the document stream after indexing is complete
stream.close();
Indexing from a structure
The following example demonstrates how to index a document from a structure.
Java
StringindexFolder="c:\\MyIndex";StringdocumentFilePath="c:\\MyDocuments\\ExampleDocument.txt";// Creating an index
Indexindex=newIndex(indexFolder);// Creating a document object
Stringtext=newString(Files.readAllBytes(Paths.get(documentFilePath)),StandardCharsets.UTF_8);DocumentField[]fields=newDocumentField[]{newDocumentField(CommonFieldNames.Content,text),};Documentdocument=Document.createFromStructure("ExampleDocument",newDate(),fields);Document[]documents=newDocument[]{document,};// Indexing document from the structure
IndexingOptionsoptions=newIndexingOptions();index.add(documents,options);
Indexing from URL
The following example demonstrates how to index a document by URL when lazy initialized.
Java
privatestaticclassDocumentLoaderFromUrlimplementsIDocumentLoader{privatefinalStringdocumentKey;privatefinalStringurl;privatefinalStringextension;publicDocumentLoaderFromUrl(StringdocumentKey,Stringurl,Stringextension){this.documentKey=documentKey;this.url=url;this.extension=extension;}@OverridepublicfinalDocumentloadDocument(){try{URLurlInstance=newURL(url);InputStreamstream=urlInstance.openStream();Documentdocument=Document.createFromStream(documentKey,newDate(),extension,stream);returndocument;}catch(java.io.IOExceptionex){thrownewRuntimeException(ex);}}@OverridepublicfinalvoidcloseDocument(){}}StringindexFolder="c:\\MyIndex";Stringurl="http://example.com/ExampleDocument.pdf";// Creating an index
IndexSettingssettings=newIndexSettings();settings.setTextStorageSettings(newTextStorageSettings(Compression.High));settings.setUseRawTextExtraction(false);Indexindex=newIndex(indexFolder,settings,true);index.getEvents().ErrorOccurred.add(newEventHandler<IndexErrorEventArgs>(){@Overridepublicvoidinvoke(Objects,IndexErrorEventArgsa){System.out.println(a.getMessage());}});// Creating a document object
StringdocumentKey=url;IDocumentLoaderdocumentLoader=newDocumentLoaderFromUrl(documentKey,url,".pdf");Documentdocument=Document.createLazy(DocumentSourceKind.Stream,documentKey,documentLoader);Document[]documents=newDocument[]{document,};// Indexing the lazy-loaded document
IndexingOptionsoptions=newIndexingOptions();index.add(documents,options);
Indexing from FTP
The following example demonstrates how to index a document from FTP when lazy initialized.
Java
privatestaticclassDocumentLoaderFromUrlimplementsIDocumentLoader{privatefinalStringdocumentKey;privatefinalStringurl;privatefinalStringextension;publicDocumentLoaderFromUrl(StringdocumentKey,Stringurl,Stringextension){this.documentKey=documentKey;this.url=url;this.extension=extension;}@OverridepublicfinalDocumentloadDocument(){try{URLurlInstance=newURL(url);InputStreamstream=urlInstance.openStream();Documentdocument=Document.createFromStream(documentKey,newDate(),extension,stream);returndocument;}catch(java.io.IOExceptionex){thrownewRuntimeException(ex);}}@OverridepublicfinalvoidcloseDocument(){}}StringindexFolder="c:\\MyIndex";Stringurl="ftp://example.com/ExampleDocument.pdf";// Creating an index
IndexSettingssettings=newIndexSettings();settings.setTextStorageSettings(newTextStorageSettings(Compression.High));settings.setUseRawTextExtraction(false);Indexindex=newIndex(indexFolder,settings,true);index.getEvents().ErrorOccurred.add(newEventHandler<IndexErrorEventArgs>(){@Overridepublicvoidinvoke(Objects,IndexErrorEventArgsa){System.out.println(a.getMessage());}});// Creating a document object
StringdocumentKey=url;IDocumentLoaderdocumentLoader=newDocumentLoaderFromUrl(documentKey,url,".pdf");Documentdocument=Document.createLazy(DocumentSourceKind.Stream,documentKey,documentLoader);Document[]documents=newDocument[]{document,};// Indexing the lazy-loaded document
IndexingOptionsoptions=newIndexingOptions();index.add(documents,options);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: