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.
constindexFolder='c:/MyIndex';constdocumentFilePath='c:/MyDocuments/ExampleDocument.pdf';// Creating an index
constsettings=newgroupdocs.search.IndexSettings();constindex=newgroupdocs.search.Index(indexFolder,settings);// Creating a document object
constdocument=groupdocs.search.Document.createFromFile(documentFilePath);constdocuments=java.newArray('com.groupdocs.search.Document',[document]);// Indexing document from the file
constoptions=newgroupdocs.search.IndexingOptions();options.setUseRawTextExtraction(false);index.add(documents,options);
Indexing from a stream
The following example demonstrates how to index a document from a stream.
constindexFolder='c:/MyIndex';constdocumentFilePath='c:/MyDocuments/ExampleDocument.pdf';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Creating a document object
conststream=java.newInstanceSync('java.io.FileInputStream',documentFilePath);// Opening a stream
constdocument=groupdocs.search.Document.createFromStream(documentFilePath,newDate(),'.pdf',stream);constdocuments=java.newArray('com.groupdocs.search.Document',[document]);// Indexing document from the stream
constoptions=newgroupdocs.search.IndexingOptions();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.
constindexFolder='c:/MyIndex';constdocumentFilePath='c:/MyDocuments/ExampleDocument.pdf';// Creating an index
constindex=newgroupdocs.search.Index(indexFolder);// Creating a document object
consttext=fs.readFileSync(documentFilePath,'utf8').toString();constfields=java.newArray('com.groupdocs.search.common.DocumentField',[newgroupdocs.search.DocumentField(groupdocs.search.CommonFieldNames.Content,text),]);constdocument=groupdocs.search.Document.createFromStructure('ExampleDocument',java.newInstanceSync('java.util.Date'),fields,);constdocuments=java.newArray('com.groupdocs.search.Document',[document]);// Indexing document from the structure
constoptions=newgroupdocs.search.IndexingOptions();index.add(documents,options);
Indexing from URL
The following example demonstrates how to index a document by URL when lazy initialized.
constindexFolder='c:/MyIndex';consturl='http://example.com/ExampleDocument.pdf';// Creating an index
constsettings=newgroupdocs.search.IndexSettings();settings.setTextStorageSettings(newgroupdocs.search.TextStorageSettings(groupdocs.search.Compression.High));constindex=newgroupdocs.search.Index(indexFolder,settings,true);index.getEvents().ErrorOccurred.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log(args.getMessage());},}),);// Creating a document object
constdocumentKey=url;constextension=path.extname(url);constdocumentLoader=java.newProxy('com.groupdocs.search.common.IDocumentLoader',{loadDocument:function(){consturlInstance=java.newInstanceSync('java.net.URL',url);conststream=urlInstance.openStream();constdocument=groupdocs.search.Document.createFromStream(documentKey,java.newInstanceSync('java.util.Date'),extension,stream,);returndocument;},closeDocument:function(){},});constdocument=groupdocs.search.Document.createLazy(groupdocs.search.DocumentSourceKind.Stream,documentKey,documentLoader,);constdocuments=java.newArray('com.groupdocs.search.Document',[document]);// Indexing the lazy-loaded document
constoptions=newgroupdocs.search.IndexingOptions();options.setUseRawTextExtraction(false);index.add(documents,options);
Indexing from FTP
The following example demonstrates how to index a document from FTP when lazy initialized.
constindexFolder='c:/MyIndex';consturl='ftp://example.com/ExampleDocument.pdf';// Creating an index
constsettings=newgroupdocs.search.IndexSettings();settings.setTextStorageSettings(newgroupdocs.search.TextStorageSettings(groupdocs.search.Compression.High));constindex=newgroupdocs.search.Index(indexFolder,settings,true);index.getEvents().ErrorOccurred.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log(args.getMessage());},}),);// Creating a document object
constdocumentKey=url;constextension=path.extname(url);constdocumentLoader=java.newProxy('com.groupdocs.search.common.IDocumentLoader',{loadDocument:function(){consturlInstance=java.newInstanceSync('java.net.URL',url);conststream=urlInstance.openStream();constdocument=groupdocs.search.Document.createFromStream(documentKey,java.newInstanceSync('java.util.Date'),extension,stream,);returndocument;},closeDocument:function(){},});constdocument=groupdocs.search.Document.createLazy(groupdocs.search.DocumentSourceKind.Stream,documentKey,documentLoader,);constdocuments=java.newArray('com.groupdocs.search.Document',[document]);// Indexing the lazy-loaded document
constoptions=newgroupdocs.search.IndexingOptions();options.setUseRawTextExtraction(false);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: