add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / examples / org / apache / lucene / facet / example / simple / SimpleIndexer.java
1 package org.apache.lucene.facet.example.simple;
2
3 import java.util.List;
4
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;
13
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;
20
21 /**
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
28  *
29  *     http://www.apache.org/licenses/LICENSE-2.0
30  *
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.
36  */
37
38 /**
39  * Sample indexer creates an index, and adds to it sample documents and facets.
40  * 
41  * @lucene.experimental
42  */
43 public class SimpleIndexer {
44
45   /**
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
50    */
51   public static void index (Directory indexDir, Directory taxoDir) throws Exception {
52
53     // create and open an index writer
54     IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
55
56     // create and open a taxonomy writer
57     TaxonomyWriter taxo = new LuceneTaxonomyWriter(taxoDir, OpenMode.CREATE);
58
59     // loop over  sample documents 
60     int nDocsAdded = 0;
61     int nFacetsAdded = 0;
62     for (int docNum=0; docNum<SimpleUtils.docTexts.length; docNum++) {
63
64       // obtain the sample facets for current document
65       List<CategoryPath> facetList = SimpleUtils.categoryPathArrayToList(SimpleUtils.categories[docNum]);
66
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);
70
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));
75
76       // invoke the category document builder for adding categories to the document and,
77       // as required, to the taxonomy index 
78       categoryDocBuilder.build(doc);
79
80       // finally add the document to the index
81       iw.addDocument(doc);
82
83       nDocsAdded ++;
84       nFacetsAdded += facetList.size(); 
85     }
86
87     // commit changes.
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.
91     taxo.commit();
92     iw.commit();
93
94     // close the taxonomy index and the index - all modifications are 
95     // now safely in the provided directories: indexDir and taxoDir.
96     taxo.close();
97     iw.close();
98
99     ExampleUtils.log("Indexed "+nDocsAdded+" documents with overall "+nFacetsAdded+" facets.");
100   }
101   
102 }