1
package org.apache.lucene.facet.example.merge;
3
import java.io.IOException;
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;
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.directory.DirectoryTaxonomyWriter;
15
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap;
16
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap;
17
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap;
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
27
* http://www.apache.org/licenses/LICENSE-2.0
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.
37
* @lucene.experimental
39
public class TaxonomyMergeUtils {
42
* Merges the given taxonomy and index directories. Note that this method
43
* opens {@link DirectoryTaxonomyWriter} 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, DirectoryTaxonomyWriter)}
49
* @see #merge(Directory, Directory, IndexWriter, DirectoryTaxonomyWriter)
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
DirectoryTaxonomyWriter destTaxWriter = new DirectoryTaxonomyWriter(destTaxDir);
56
merge(srcIndexDir, srcTaxDir, new MemoryOrdinalMap(), destIndexWriter, destTaxWriter);
57
destTaxWriter.close();
58
destIndexWriter.close();
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, DirectoryTaxonomyWriter.OrdinalMap, IndexWriter, DirectoryTaxonomyWriter)}
66
* by passing {@link DiskOrdinalMap}.
68
* @see #merge(Directory, Directory, DirectoryTaxonomyWriter.OrdinalMap, IndexWriter, DirectoryTaxonomyWriter)
70
public static void merge(Directory srcIndexDir, Directory srcTaxDir,
71
IndexWriter destIndexWriter,
72
DirectoryTaxonomyWriter destTaxWriter) throws IOException {
73
merge(srcIndexDir, srcTaxDir, new MemoryOrdinalMap(), destIndexWriter, destTaxWriter);
77
* Merges the given taxonomy and index directories and commits the changes to
80
public static void merge(Directory srcIndexDir, Directory srcTaxDir,
81
OrdinalMap map, IndexWriter destIndexWriter,
82
DirectoryTaxonomyWriter destTaxWriter) throws IOException {
83
// merge the taxonomies
84
destTaxWriter.addTaxonomies(new Directory[] { srcTaxDir }, new OrdinalMap[] { map });
86
PayloadProcessorProvider payloadProcessor = new FacetsPayloadProcessorProvider(
87
srcIndexDir, map.getMap(), new DefaultFacetIndexingParams());
88
destIndexWriter.setPayloadProcessorProvider(payloadProcessor);
90
IndexReader reader = IndexReader.open(srcIndexDir);
92
destIndexWriter.addIndexes(reader);
94
// commit changes to taxonomy and index respectively.
95
destTaxWriter.commit();
96
destIndexWriter.commit();