1 package org.apache.lucene.index;
4 * Copyright 2006 The Apache Software Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.io.IOException;
21 import java.util.Date;
23 import org.apache.lucene.search.Similarity;
24 import org.apache.lucene.store.Directory;
25 import org.apache.lucene.store.FSDirectory;
26 import org.apache.lucene.util.StringHelper;
29 * Given a directory and a list of fields, updates the fieldNorms in place for every document.
31 * If Similarity class is specified, uses its computeNorm method to set norms.
32 * If -n command line argument is used, removed field norms, as if
33 * {@link org.apache.lucene.document.Field.Index}.NO_NORMS was used.
36 * NOTE: This will overwrite any length normalization or field/document boosts.
40 public class FieldNormModifier {
43 * Command Line Execution method.
46 * Usage: FieldNormModifier /path/index <package.SimilarityClassName | -n> field1 field2 ...
49 public static void main(String[] args) throws IOException {
50 if (args.length < 3) {
51 System.err.println("Usage: FieldNormModifier <index> <package.SimilarityClassName | -n> <field1> [field2] ...");
56 if (!args[1].equals("-n")) {
58 s = Class.forName(args[1]).asSubclass(Similarity.class).newInstance();
59 } catch (Exception e) {
60 System.err.println("Couldn't instantiate similarity with empty constructor: " + args[1]);
61 e.printStackTrace(System.err);
66 Directory d = FSDirectory.open(new File(args[0]));
67 FieldNormModifier fnm = new FieldNormModifier(d, s);
69 for (int i = 2; i < args.length; i++) {
70 System.out.print("Updating field: " + args[i] + " " + (new Date()).toString() + " ... ");
71 fnm.reSetNorms(args[i]);
72 System.out.println(new Date().toString());
79 private Directory dir;
80 private Similarity sim;
83 * Constructor for code that wishes to use this class programmatically
84 * If Similarity is null, kill the field norms.
86 * @param d the Directory to modify
87 * @param s the Similarity to use (can be null)
89 public FieldNormModifier(Directory d, Similarity s) {
95 * Resets the norms for the specified field.
98 * Opens a new IndexReader on the Directory given to this instance,
99 * modifies the norms (either using the Similarity given to this instance, or by using fake norms,
100 * and closes the IndexReader.
103 * @param field the field whose norms should be reset
105 public void reSetNorms(String field) throws IOException {
106 String fieldName = StringHelper.intern(field);
107 int[] termCounts = new int[0];
109 IndexReader reader = null;
110 TermEnum termEnum = null;
111 TermDocs termDocs = null;
113 reader = IndexReader.open(dir, true);
114 termCounts = new int[reader.maxDoc()];
116 termEnum = reader.terms(new Term(field));
118 termDocs = reader.termDocs();
120 Term term = termEnum.term();
121 if (term != null && term.field().equals(fieldName)) {
122 termDocs.seek(termEnum.term());
123 while (termDocs.next()) {
124 termCounts[termDocs.doc()] += termDocs.freq();
127 } while (termEnum.next());
130 if (null != termDocs) termDocs.close();
133 if (null != termEnum) termEnum.close();
136 if (null != reader) reader.close();
140 reader = IndexReader.open(dir, false);
141 final FieldInvertState invertState = new FieldInvertState();
142 invertState.setBoost(1.0f);
143 for (int d = 0; d < termCounts.length; d++) {
144 if (! reader.isDeleted(d)) {
146 reader.setNorm(d, fieldName, Similarity.encodeNorm(1.0f));
148 invertState.setLength(termCounts[d]);
149 reader.setNorm(d, fieldName, sim.encodeNormValue(sim.computeNorm(fieldName, invertState)));
155 if (null != reader) reader.close();