add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / examples / org / apache / lucene / facet / example / multiCL / MultiCLIndexer.java
1 package org.apache.lucene.facet.example.multiCL;
2
3 import java.util.List;
4 import java.util.Random;
5
6 import org.apache.lucene.document.Document;
7 import org.apache.lucene.document.Field;
8 import org.apache.lucene.document.Field.Index;
9 import org.apache.lucene.document.Field.Store;
10 import org.apache.lucene.index.IndexWriter;
11 import org.apache.lucene.index.IndexWriterConfig;
12 import org.apache.lucene.index.Term;
13 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
14 import org.apache.lucene.store.Directory;
15 import org.apache.lucene.store.RAMDirectory;
16
17 import org.apache.lucene.DocumentBuilder;
18 import org.apache.lucene.facet.example.ExampleUtils;
19 import org.apache.lucene.facet.example.simple.SimpleUtils;
20 import org.apache.lucene.facet.index.CategoryDocumentBuilder;
21 import org.apache.lucene.facet.index.params.CategoryListParams;
22 import org.apache.lucene.facet.index.params.FacetIndexingParams;
23 import org.apache.lucene.facet.index.params.PerDimensionIndexingParams;
24 import org.apache.lucene.facet.taxonomy.CategoryPath;
25 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
26
27 /**
28  * Licensed to the Apache Software Foundation (ASF) under one or more
29  * contributor license agreements.  See the NOTICE file distributed with
30  * this work for additional information regarding copyright ownership.
31  * The ASF licenses this file to You under the Apache License, Version 2.0
32  * (the "License"); you may not use this file except in compliance with
33  * the License.  You may obtain a copy of the License at
34  *
35  *     http://www.apache.org/licenses/LICENSE-2.0
36  *
37  * Unless required by applicable law or agreed to in writing, software
38  * distributed under the License is distributed on an "AS IS" BASIS,
39  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40  * See the License for the specific language governing permissions and
41  * limitations under the License.
42  */
43
44 /**
45  * Sample indexer creates an index, and adds to it sample documents and facets 
46  * with multiple CategoryLists specified for different facets, so there are different
47  * category lists for different facets.
48  * 
49  * @lucene.experimental
50  */
51 public class MultiCLIndexer {
52
53   // Number of documents to index
54   public static int NUM_DOCS = 100;
55   // Number of facets to add per document
56   public static int NUM_FACETS_PER_DOC = 10;
57   // Number of tokens in title
58   public static int TITLE_LENGTH = 5;
59   // Number of tokens in text
60   public static int TEXT_LENGTH = 100;
61   
62   // Lorum ipsum to use as content - this will be tokenized and used for document
63   // titles/text.
64   static String words = "Sed ut perspiciatis unde omnis iste natus error sit "
65       + "voluptatem accusantium doloremque laudantium totam rem aperiam "
66       + "eaque ipsa quae ab illo inventore veritatis et quasi architecto "
67       + "beatae vitae dicta sunt explicabo Nemo enim ipsam voluptatem "
68       + "quia voluptas sit aspernatur aut odit aut fugit sed quia consequuntur "
69       + "magni dolores eos qui ratione voluptatem sequi nesciunt Neque porro "
70       + "quisquam est qui dolorem ipsum quia dolor sit amet consectetur adipisci velit "
71       + "sed quia non numquam eius modi tempora incidunt ut labore et dolore "
72       + "magnam aliquam quaerat voluptatem Ut enim ad minima veniam "
73       + "quis nostrum exercitationem ullam corporis suscipit laboriosam "
74       + "nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure"
75       + "reprehenderit qui in ea voluptate velit esse quam nihil molestiae "
76       + "consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur";
77   // PerDimensionIndexingParams for multiple category lists
78   public static PerDimensionIndexingParams MULTI_IPARAMS = new PerDimensionIndexingParams();
79
80   // Initialize PerDimensionIndexingParams
81   static {
82     MULTI_IPARAMS.addCategoryListParams(new CategoryPath("0"),
83         new CategoryListParams(new Term("$Digits", "Zero")));
84     MULTI_IPARAMS.addCategoryListParams(new CategoryPath("1"),
85         new CategoryListParams(new Term("$Digits", "One")));
86     MULTI_IPARAMS.addCategoryListParams(new CategoryPath("2"),
87         new CategoryListParams(new Term("$Digits", "Two")));
88     MULTI_IPARAMS.addCategoryListParams(new CategoryPath("3"),
89         new CategoryListParams(new Term("$Digits", "Three")));
90     MULTI_IPARAMS.addCategoryListParams(new CategoryPath("4"),
91         new CategoryListParams(new Term("$Digits", "Four")));
92     MULTI_IPARAMS.addCategoryListParams(new CategoryPath("5"),
93         new CategoryListParams(new Term("$Digits", "Five")));
94   }
95   
96   /**
97    * Create an index, and adds to it sample documents and facets.
98    * @param indexDir Directory in which the index should be created.
99    * @param taxoDir Directory in which the taxonomy index should be created.
100    * @throws Exception on error (no detailed exception handling here for sample simplicity
101    */
102   public static void index(Directory indexDir, Directory taxoDir)
103       throws Exception {
104
105     Random random = new Random(2003);
106
107     String[] docTitles = new String[NUM_DOCS];
108     String[] docTexts = new String[NUM_DOCS];
109     CategoryPath[][] cPaths = new CategoryPath[NUM_DOCS][NUM_FACETS_PER_DOC];
110
111     String[] tokens = words.split(" ");
112     for (int docNum = 0; docNum < NUM_DOCS; docNum++) {
113       String title = "";
114       String text = "";
115       for (int j = 0; j < TITLE_LENGTH; j++) {
116         title = title + tokens[random.nextInt(tokens.length)] + " ";
117       }
118       docTitles[docNum] = title;
119
120       for (int j = 0; j < TEXT_LENGTH; j++) {
121         text = text + tokens[random.nextInt(tokens.length)] + " ";
122       }
123       docTexts[docNum] = text;
124
125       for (int facetNum = 0; facetNum < NUM_FACETS_PER_DOC; facetNum++) {
126         cPaths[docNum][facetNum] = new CategoryPath(Integer
127             .toString(random.nextInt(7)), Integer.toString(random.nextInt(10)));
128       }
129     }
130     index(indexDir, taxoDir, MULTI_IPARAMS, docTitles, docTexts, cPaths);
131   }
132   
133   /**
134    * More advanced method for specifying custom indexing params, doc texts, 
135    * doc titles and category paths.
136    */
137   public static void index(Directory indexDir, Directory taxoDir,
138       FacetIndexingParams iParams, String[] docTitles,
139       String[] docTexts, CategoryPath[][] cPaths) throws Exception {
140     // create and open an index writer
141     IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(
142         ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer).setOpenMode(OpenMode.CREATE));
143     // create and open a taxonomy writer
144     LuceneTaxonomyWriter taxo = new LuceneTaxonomyWriter(taxoDir, OpenMode.CREATE);
145     index(iw, taxo, iParams, docTitles, docTexts, cPaths);
146   }
147   
148   /**
149    * More advanced method for specifying custom indexing params, doc texts, 
150    * doc titles and category paths.
151    * <p>
152    * Create an index, and adds to it sample documents and facets.
153    * @throws Exception
154    *             on error (no detailed exception handling here for sample
155    *             simplicity
156    */
157   public static void index(IndexWriter iw, LuceneTaxonomyWriter taxo,
158       FacetIndexingParams iParams, String[] docTitles,
159       String[] docTexts, CategoryPath[][] cPaths) throws Exception {
160
161     // loop over sample documents
162     int nDocsAdded = 0;
163     int nFacetsAdded = 0;
164     for (int docNum = 0; docNum < SimpleUtils.docTexts.length; docNum++) {
165       List<CategoryPath> facetList = SimpleUtils.categoryPathArrayToList(cPaths[docNum]);
166
167       // we do not alter indexing parameters!
168       // a category document builder will add the categories to a document
169       // once build() is called
170       DocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(
171           taxo, iParams).setCategoryPaths(facetList);
172
173       // create a plain Lucene document and add some regular Lucene fields
174       // to it
175       Document doc = new Document();
176       doc.add(new Field(SimpleUtils.TITLE, docTitles[docNum], Store.YES, Index.ANALYZED));
177       doc.add(new Field(SimpleUtils.TEXT, docTexts[docNum], Store.NO, Index.ANALYZED));
178
179       // finally add the document to the index
180       categoryDocBuilder.build(doc);
181       iw.addDocument(doc);
182       
183       nDocsAdded++;
184       nFacetsAdded += facetList.size();
185     }
186
187     // commit changes.
188     // we commit changes to the taxonomy index prior to committing them to
189     // the search index.
190     // this is important, so that all facets referred to by documents in the
191     // search index
192     // will indeed exist in the taxonomy index.
193     taxo.commit();
194     iw.commit();
195
196     // close the taxonomy index and the index - all modifications are
197     // now safely in the provided directories: indexDir and taxoDir.
198     taxo.close();
199     iw.close();
200
201     ExampleUtils.log("Indexed " + nDocsAdded + " documents with overall "
202         + nFacetsAdded + " facets.");
203   }
204
205   public static void main(String[] args) throws Exception {
206     index(new RAMDirectory(), new RAMDirectory());
207   }
208   
209 }