1
package org.apache.lucene.index;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.store.Directory;
21
import org.apache.lucene.store.FSDirectory;
22
import org.apache.lucene.util.Constants;
23
import org.apache.lucene.util.Version;
26
import java.io.IOException;
27
import java.io.PrintStream;
28
import java.util.Collection;
31
* This is an easy-to-use tool that upgrades all segments of an index from previous Lucene versions
32
* to the current segment file format. It can be used from command line:
34
* java -cp lucene-core.jar org.apache.lucene.index.IndexUpgrader [-delete-prior-commits] [-verbose] indexDir
36
* Alternatively this class can be instantiated and {@link #upgrade} invoked. It uses {@link UpgradeIndexMergePolicy}
37
* and triggers the upgrade via an forceMerge request to {@link IndexWriter}.
38
* <p>This tool keeps only the last commit in an index; for this
39
* reason, if the incoming index has more than one commit, the tool
40
* refuses to run by default. Specify {@code -delete-prior-commits}
41
* to override this, allowing the tool to delete all but the last commit.
42
* From Java code this can be enabled by passing {@code true} to
43
* {@link #IndexUpgrader(Directory,Version,PrintStream,boolean)}.
44
* <p><b>Warning:</b> This tool may reorder documents if the index was partially
45
* upgraded before execution (e.g., documents were added). If your application relies
46
* on "monotonicity" of doc IDs (which means that the order in which the documents
47
* were added to the index is preserved), do a full forceMerge instead.
48
* The {@link MergePolicy} set by {@link IndexWriterConfig} may also reorder
51
public final class IndexUpgrader {
53
private static void printUsage() {
54
System.err.println("Upgrades an index so all segments created with a previous Lucene version are rewritten.");
55
System.err.println("Usage:");
56
System.err.println(" java " + IndexUpgrader.class.getName() + " [-delete-prior-commits] [-verbose] indexDir");
57
System.err.println("This tool keeps only the last commit in an index; for this");
58
System.err.println("reason, if the incoming index has more than one commit, the tool");
59
System.err.println("refuses to run by default. Specify -delete-prior-commits to override");
60
System.err.println("this, allowing the tool to delete all but the last commit.");
61
System.err.println("WARNING: This tool may reorder document IDs!");
65
@SuppressWarnings("deprecation")
66
public static void main(String[] args) throws IOException {
68
boolean deletePriorCommits = false;
69
PrintStream out = null;
70
for (String arg : args) {
71
if ("-delete-prior-commits".equals(arg)) {
72
deletePriorCommits = true;
73
} else if ("-verbose".equals(arg)) {
75
} else if (dir == null) {
85
new IndexUpgrader(FSDirectory.open(new File(dir)), Version.LUCENE_CURRENT, out, deletePriorCommits).upgrade();
88
private final Directory dir;
89
private final PrintStream infoStream;
90
private final IndexWriterConfig iwc;
91
private final boolean deletePriorCommits;
93
/** Creates index upgrader on the given directory, using an {@link IndexWriter} using the given
94
* {@code matchVersion}. The tool refuses to upgrade indexes with multiple commit points. */
95
public IndexUpgrader(Directory dir, Version matchVersion) {
96
this(dir, new IndexWriterConfig(matchVersion, null), null, false);
99
/** Creates index upgrader on the given directory, using an {@link IndexWriter} using the given
100
* {@code matchVersion}. You have the possibility to upgrade indexes with multiple commit points by removing
101
* all older ones. If {@code infoStream} is not {@code null}, all logging output will be sent to this stream. */
102
public IndexUpgrader(Directory dir, Version matchVersion, PrintStream infoStream, boolean deletePriorCommits) {
103
this(dir, new IndexWriterConfig(matchVersion, null), infoStream, deletePriorCommits);
106
/** Creates index upgrader on the given directory, using an {@link IndexWriter} using the given
107
* config. You have the possibility to upgrade indexes with multiple commit points by removing
108
* all older ones. If {@code infoStream} is not {@code null}, all logging output will be sent to this stream. */
109
public IndexUpgrader(Directory dir, IndexWriterConfig iwc, PrintStream infoStream, boolean deletePriorCommits) {
112
this.infoStream = infoStream;
113
this.deletePriorCommits = deletePriorCommits;
116
public void upgrade() throws IOException {
117
if (!IndexReader.indexExists(dir)) {
118
throw new IndexNotFoundException(dir.toString());
121
if (!deletePriorCommits) {
122
final Collection<IndexCommit> commits = IndexReader.listCommits(dir);
123
if (commits.size() > 1) {
124
throw new IllegalArgumentException("This tool was invoked to not delete prior commit points, but the following commits were found: " + commits);
128
final IndexWriterConfig c = (IndexWriterConfig) iwc.clone();
129
c.setMergePolicy(new UpgradeIndexMergePolicy(c.getMergePolicy()));
130
c.setIndexDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
132
final IndexWriter w = new IndexWriter(dir, c);
134
w.setInfoStream(infoStream);
135
w.message("Upgrading all pre-" + Constants.LUCENE_MAIN_VERSION + " segments of index directory '" + dir + "' to version " + Constants.LUCENE_MAIN_VERSION + "...");
137
w.message("All segments upgraded to version " + Constants.LUCENE_MAIN_VERSION);