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