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