1 package org.apache.lucene.facet.example.association;
3 import org.apache.lucene.document.Document;
4 import org.apache.lucene.document.Field;
5 import org.apache.lucene.document.Field.Index;
6 import org.apache.lucene.document.Field.Store;
7 import org.apache.lucene.index.IndexWriter;
8 import org.apache.lucene.index.IndexWriterConfig;
9 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
10 import org.apache.lucene.store.Directory;
12 import org.apache.lucene.facet.enhancements.EnhancementsDocumentBuilder;
13 import org.apache.lucene.facet.enhancements.association.AssociationProperty;
14 import org.apache.lucene.facet.example.ExampleUtils;
15 import org.apache.lucene.facet.example.simple.SimpleUtils;
16 import org.apache.lucene.facet.index.CategoryContainer;
17 import org.apache.lucene.facet.index.CategoryDocumentBuilder;
18 import org.apache.lucene.facet.taxonomy.CategoryPath;
19 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
20 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
23 * Licensed to the Apache Software Foundation (ASF) under one or more
24 * contributor license agreements. See the NOTICE file distributed with
25 * this work for additional information regarding copyright ownership.
26 * The ASF licenses this file to You under the Apache License, Version 2.0
27 * (the "License"); you may not use this file except in compliance with
28 * the License. You may obtain a copy of the License at
30 * http://www.apache.org/licenses/LICENSE-2.0
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
40 * Sample indexer creates an index, and adds to it sample documents with
41 * categories, which can be simple or contain associations.
43 * @lucene.experimental
45 public class AssociationIndexer {
48 * Create an index, and adds to it sample documents and categories.
51 * Directory in which the index should be created.
53 * Directory in which the taxonomy index should be created.
55 * on error (no detailed exception handling here for sample
58 public static void index(Directory indexDir, Directory taxoDir) throws Exception {
60 // create and open an index writer
61 IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
63 // create and open a taxonomy writer
64 TaxonomyWriter taxo = new LuceneTaxonomyWriter(taxoDir, OpenMode.CREATE);
66 // loop over sample documents
69 for (int docNum = 0; docNum < SimpleUtils.docTexts.length; docNum++) {
70 ExampleUtils.log(" ++++ DOC ID: " + docNum);
71 // obtain the sample categories for current document
72 CategoryContainer categoryContainer = new CategoryContainer();
73 for (CategoryPath path : SimpleUtils.categories[docNum]) {
74 categoryContainer.addCategory(path);
75 ExampleUtils.log("\t ++++ PATH: " + path);
77 // and also those with associations
78 CategoryPath[] associationsPaths = AssociationUtils.categories[docNum];
79 AssociationProperty[] associationProps = AssociationUtils.associations[docNum];
80 for (int i = 0; i < associationsPaths.length; i++) {
81 categoryContainer.addCategory(associationsPaths[i], associationProps[i]);
82 ExampleUtils.log("\t $$$$ Association: ("
83 + associationsPaths[i] + "," + associationProps[i]
87 // we do not alter indexing parameters!
88 // a category document builder will add the categories to a document
89 // once build() is called
90 CategoryDocumentBuilder categoryDocBuilder = new EnhancementsDocumentBuilder(
91 taxo, AssociationUtils.assocIndexingParams);
92 categoryDocBuilder.setCategories(categoryContainer);
94 // create a plain Lucene document and add some regular Lucene fields
96 Document doc = new Document();
97 doc.add(new Field(SimpleUtils.TITLE, SimpleUtils.docTitles[docNum],
98 Store.YES, Index.ANALYZED));
99 doc.add(new Field(SimpleUtils.TEXT, SimpleUtils.docTexts[docNum],
100 Store.NO, Index.ANALYZED));
102 // invoke the category document builder for adding categories to the
104 // as required, to the taxonomy index
105 categoryDocBuilder.build(doc);
107 // finally add the document to the index
111 nFacetsAdded += categoryContainer.size();
115 // we commit changes to the taxonomy index prior to committing them to
117 // this is important, so that all facets referred to by documents in the
119 // will indeed exist in the taxonomy index.
123 // close the taxonomy index and the index - all modifications are
124 // now safely in the provided directories: indexDir and taxoDir.
128 ExampleUtils.log("Indexed " + nDocsAdded + " documents with overall "
129 + nFacetsAdded + " facets.");