// create and open an index writer
IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
// create and open a taxonomy writer
TaxonomyWriter taxo = new LuceneTaxonomyWriter(taxoDir, OpenMode.CREATE);
// loop over sample documents
int nDocsAdded = 0;
int nFacetsAdded = 0;
for (int docNum = 0; docNum < SimpleUtils.docTexts.length; docNum++) {
ExampleUtils.log(" ++++ DOC ID: " + docNum);
// obtain the sample categories for current document
CategoryContainer categoryContainer = new CategoryContainer();
for (CategoryPath path : SimpleUtils.categories[docNum]) {
categoryContainer.addCategory(path);
ExampleUtils.log("\t ++++ PATH: " + path);
}
// and also those with associations
CategoryPath[] associationsPaths = AssociationUtils.categories[docNum];
AssociationProperty[] associationProps = AssociationUtils.associations[docNum];
for (int i = 0; i < associationsPaths.length; i++) {
categoryContainer.addCategory(associationsPaths[i], associationProps[i]);
ExampleUtils.log("\t $$$$ Association: ("
+ associationsPaths[i] + "," + associationProps[i]
+ ")");
}
// we do not alter indexing parameters!
// a category document builder will add the categories to a document
// once build() is called
CategoryDocumentBuilder categoryDocBuilder = new EnhancementsDocumentBuilder(
taxo, AssociationUtils.assocIndexingParams);
categoryDocBuilder.setCategories(categoryContainer);
// create a plain Lucene document and add some regular Lucene fields
// to it
Document doc = new Document();
doc.add(new Field(SimpleUtils.TITLE, SimpleUtils.docTitles[docNum],
Store.YES, Index.ANALYZED));
doc.add(new Field(SimpleUtils.TEXT, SimpleUtils.docTexts[docNum],
Store.NO, Index.ANALYZED));
// invoke the category document builder for adding categories to the
// document and,
// as required, to the taxonomy index
categoryDocBuilder.build(doc);
// finally add the document to the index
iw.addDocument(doc);
nDocsAdded++;
nFacetsAdded += categoryContainer.size();
}
// commit changes.
// we commit changes to the taxonomy index prior to committing them to
// the search index.
// this is important, so that all facets referred to by documents in the
// search index
// will indeed exist in the taxonomy index.
taxo.commit();
iw.commit();
// close the taxonomy index and the index - all modifications are
// now safely in the provided directories: indexDir and taxoDir.
taxo.close();
iw.close();
ExampleUtils.log("Indexed " + nDocsAdded + " documents with overall "
+ nFacetsAdded + " facets.");
}