add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / examples / org / apache / lucene / facet / example / merge / TaxonomyMergeUtils.java
1 package org.apache.lucene.facet.example.merge;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.IndexWriter;
7 import org.apache.lucene.index.IndexWriterConfig;
8 import org.apache.lucene.index.PayloadProcessorProvider;
9 import org.apache.lucene.store.Directory;
10
11 import org.apache.lucene.facet.example.ExampleUtils;
12 import org.apache.lucene.facet.index.FacetsPayloadProcessorProvider;
13 import org.apache.lucene.facet.index.params.DefaultFacetIndexingParams;
14 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
15 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter.DiskOrdinalMap;
16 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter.MemoryOrdinalMap;
17 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter.OrdinalMap;
18
19 /**
20  * Licensed to the Apache Software Foundation (ASF) under one or more
21  * contributor license agreements.  See the NOTICE file distributed with
22  * this work for additional information regarding copyright ownership.
23  * The ASF licenses this file to You under the Apache License, Version 2.0
24  * (the "License"); you may not use this file except in compliance with
25  * the License.  You may obtain a copy of the License at
26  *
27  *     http://www.apache.org/licenses/LICENSE-2.0
28  *
29  * Unless required by applicable law or agreed to in writing, software
30  * distributed under the License is distributed on an "AS IS" BASIS,
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32  * See the License for the specific language governing permissions and
33  * limitations under the License.
34  */
35
36 /**
37  * @lucene.experimental
38  */
39 public class TaxonomyMergeUtils {
40
41   /**
42    * Merges the given taxonomy and index directories. Note that this method
43    * opens {@link LuceneTaxonomyWriter} and {@link IndexWriter} on the
44    * respective destination indexes. Therefore if you have a writer open on any
45    * of them, it should be closed, or you should use
46    * {@link #merge(Directory, Directory, IndexWriter, LuceneTaxonomyWriter)}
47    * instead.
48    * 
49    * @see #merge(Directory, Directory, IndexWriter, LuceneTaxonomyWriter)
50    */
51   public static void merge(Directory srcIndexDir, Directory srcTaxDir,
52                             Directory destIndexDir, Directory destTaxDir) throws IOException {
53     IndexWriter destIndexWriter = new IndexWriter(destIndexDir,
54         new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, null));
55     LuceneTaxonomyWriter destTaxWriter = new LuceneTaxonomyWriter(destTaxDir);
56     merge(srcIndexDir, srcTaxDir, new MemoryOrdinalMap(), destIndexWriter, destTaxWriter);
57     destTaxWriter.close();
58     destIndexWriter.close();
59   }
60
61   /**
62    * Merges the given taxonomy and index directories and commits the changes to
63    * the given writers. This method uses {@link MemoryOrdinalMap} to store the
64    * mapped ordinals. If you cannot afford the memory, you can use
65    * {@link #merge(Directory, Directory, OrdinalMap, IndexWriter, LuceneTaxonomyWriter)}
66    * by passing {@link DiskOrdinalMap}.
67    * 
68    * @see #merge(Directory, Directory, OrdinalMap, IndexWriter, LuceneTaxonomyWriter)
69    */
70   public static void merge(Directory srcIndexDir, Directory srcTaxDir,
71                             IndexWriter destIndexWriter, 
72                             LuceneTaxonomyWriter destTaxWriter) throws IOException {
73     merge(srcIndexDir, srcTaxDir, new MemoryOrdinalMap(), destIndexWriter, destTaxWriter);
74   }
75   
76   /**
77    * Merges the given taxonomy and index directories and commits the changes to
78    * the given writers.
79    */
80   public static void merge(Directory srcIndexDir, Directory srcTaxDir,
81                             OrdinalMap map, IndexWriter destIndexWriter,
82                             LuceneTaxonomyWriter destTaxWriter) throws IOException {
83     // merge the taxonomies
84     destTaxWriter.addTaxonomies(new Directory[] { srcTaxDir }, new OrdinalMap[] { map });
85
86     PayloadProcessorProvider payloadProcessor = new FacetsPayloadProcessorProvider(
87         srcIndexDir, map.getMap(), new DefaultFacetIndexingParams());
88     destIndexWriter.setPayloadProcessorProvider(payloadProcessor);
89
90     IndexReader reader = IndexReader.open(srcIndexDir);
91     try {
92       destIndexWriter.addIndexes(reader);
93       
94       // commit changes to taxonomy and index respectively.
95       destTaxWriter.commit();
96       destIndexWriter.commit();
97     } finally {
98       reader.close();
99     }
100   }
101   
102 }