1 package org.apache.lucene.facet.example.simple;
5 import org.apache.lucene.document.Document;
6 import org.apache.lucene.document.Field;
7 import org.apache.lucene.document.Field.Index;
8 import org.apache.lucene.document.Field.Store;
9 import org.apache.lucene.index.IndexWriter;
10 import org.apache.lucene.index.IndexWriterConfig;
11 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
12 import org.apache.lucene.store.Directory;
14 import org.apache.lucene.facet.example.ExampleUtils;
15 import org.apache.lucene.facet.index.CategoryDocumentBuilder;
16 import org.apache.lucene.facet.taxonomy.CategoryPath;
17 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
18 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
21 * Licensed to the Apache Software Foundation (ASF) under one or more
22 * contributor license agreements. See the NOTICE file distributed with
23 * this work for additional information regarding copyright ownership.
24 * The ASF licenses this file to You under the Apache License, Version 2.0
25 * (the "License"); you may not use this file except in compliance with
26 * the License. You may obtain a copy of the License at
28 * http://www.apache.org/licenses/LICENSE-2.0
30 * Unless required by applicable law or agreed to in writing, software
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
38 * Sample indexer creates an index, and adds to it sample documents and facets.
40 * @lucene.experimental
42 public class SimpleIndexer {
45 * Create an index, and adds to it sample documents and facets.
46 * @param indexDir Directory in which the index should be created.
47 * @param taxoDir Directory in which the taxonomy index should be created.
48 * @throws Exception on error (no detailed exception handling here for sample simplicity
50 public static void index (Directory indexDir, Directory taxoDir) throws Exception {
52 // create and open an index writer
53 IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
55 // create and open a taxonomy writer
56 TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
58 // loop over sample documents
61 for (int docNum=0; docNum<SimpleUtils.docTexts.length; docNum++) {
63 // obtain the sample facets for current document
64 List<CategoryPath> facetList = SimpleUtils.categoryPathArrayToList(SimpleUtils.categories[docNum]);
66 // we do not alter indexing parameters!
67 // a category document builder will add the categories to a document once build() is called
68 CategoryDocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo).setCategoryPaths(facetList);
70 // create a plain Lucene document and add some regular Lucene fields to it
71 Document doc = new Document();
72 doc.add(new Field(SimpleUtils.TITLE, SimpleUtils.docTitles[docNum], Store.YES, Index.ANALYZED));
73 doc.add(new Field(SimpleUtils.TEXT, SimpleUtils.docTexts[docNum], Store.NO, Index.ANALYZED));
75 // invoke the category document builder for adding categories to the document and,
76 // as required, to the taxonomy index
77 categoryDocBuilder.build(doc);
79 // finally add the document to the index
83 nFacetsAdded += facetList.size();
87 // we commit changes to the taxonomy index prior to committing them to the search index.
88 // this is important, so that all facets referred to by documents in the search index
89 // will indeed exist in the taxonomy index.
93 // close the taxonomy index and the index - all modifications are
94 // now safely in the provided directories: indexDir and taxoDir.
98 ExampleUtils.log("Indexed "+nDocsAdded+" documents with overall "+nFacetsAdded+" facets.");