mirror of https://github.com/apache/lucene.git
- Reindented
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@483579 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e47e6bd920
commit
65cd9b86bf
|
@ -1,20 +1,20 @@
|
||||||
package org.apache.lucene.misc;
|
package org.apache.lucene.misc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright 2006 The Apache Software Foundation
|
* Copyright 2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.TermEnum;
|
import org.apache.lucene.index.TermEnum;
|
||||||
|
@ -37,120 +37,120 @@ import java.util.Date;
|
||||||
* index.
|
* index.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @version $Id:$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class LengthNormModifier {
|
public class LengthNormModifier {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command Line Execution method
|
* Command Line Execution method
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* Usage: LengthNormModifier /path/index package.SimilarityClassName field1 field2 ...
|
* Usage: LengthNormModifier /path/index package.SimilarityClassName field1 field2 ...
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
if (args.length < 3) {
|
if (args.length < 3) {
|
||||||
System.err.println("Usage: LengthNormModifier <index> <package.SimilarityClassName> <field1> [field2] ...");
|
System.err.println("Usage: LengthNormModifier <index> <package.SimilarityClassName> <field1> [field2] ...");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
Similarity s = null;
|
|
||||||
try {
|
|
||||||
Class simClass = Class.forName(args[1]);
|
|
||||||
s = (Similarity)simClass.newInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.err.println("Couldn't instantiate similarity with empty constructor: " + args[1]);
|
|
||||||
e.printStackTrace(System.err);
|
|
||||||
}
|
|
||||||
|
|
||||||
File index = new File(args[0]);
|
|
||||||
Directory d = FSDirectory.getDirectory(index,false);
|
|
||||||
|
|
||||||
LengthNormModifier lnm = new LengthNormModifier(d, s);
|
|
||||||
|
|
||||||
for (int i = 2; i < args.length; i++) {
|
|
||||||
System.out.print("Updating field: " + args[i] + " " +
|
|
||||||
(new Date()).toString() + " ... ");
|
|
||||||
lnm.reSetNorms(args[i]);
|
|
||||||
System.out.println(new Date().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
d.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private Directory dir;
|
|
||||||
private Similarity sim;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor for code that wishes to use this class progromaticaly
|
|
||||||
*
|
|
||||||
* @param d The Directory to modify
|
|
||||||
* @param s The Similarity to use in <code>reSetNorms</code>
|
|
||||||
*/
|
|
||||||
public LengthNormModifier(Directory d, Similarity s) {
|
|
||||||
dir = d;
|
|
||||||
sim = s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Re-Set the norms for the specified field.
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* Opens a new IndexReader on the Directory given to this instance,
|
|
||||||
* modifies the norms using the Similarity given to this instance,
|
|
||||||
* and closes the IndexReader.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param field the field whose norms
|
|
||||||
*/
|
|
||||||
public void reSetNorms(String field) throws IOException {
|
|
||||||
String fieldName = field.intern();
|
|
||||||
int[] termCounts = new int[0];
|
|
||||||
|
|
||||||
IndexReader reader = null;
|
|
||||||
TermEnum termEnum = null;
|
|
||||||
TermDocs termDocs = null;
|
|
||||||
try {
|
|
||||||
reader = IndexReader.open(dir);
|
|
||||||
termCounts = new int[reader.maxDoc()];
|
|
||||||
try {
|
|
||||||
termEnum = reader.terms(new Term(field,""));
|
|
||||||
try {
|
|
||||||
termDocs = reader.termDocs();
|
|
||||||
do {
|
|
||||||
Term term = termEnum.term();
|
|
||||||
if (term != null && term.field().equals(fieldName)) {
|
|
||||||
termDocs.seek(termEnum.term());
|
|
||||||
while (termDocs.next()) {
|
|
||||||
termCounts[termDocs.doc()] += termDocs.freq();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (termEnum.next());
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
if (null != termDocs) termDocs.close();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (null != termEnum) termEnum.close();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (null != reader) reader.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
reader = IndexReader.open(dir);
|
|
||||||
for (int d = 0; d < termCounts.length; d++) {
|
|
||||||
if (! reader.isDeleted(d)) {
|
|
||||||
byte norm = sim.encodeNorm
|
|
||||||
(sim.lengthNorm(fieldName, termCounts[d]));
|
|
||||||
reader.setNorm(d, fieldName, norm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
if (null != reader) reader.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Similarity s = null;
|
||||||
|
try {
|
||||||
|
Class simClass = Class.forName(args[1]);
|
||||||
|
s = (Similarity)simClass.newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Couldn't instantiate similarity with empty constructor: " + args[1]);
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
}
|
||||||
|
|
||||||
|
File index = new File(args[0]);
|
||||||
|
Directory d = FSDirectory.getDirectory(index,false);
|
||||||
|
|
||||||
|
LengthNormModifier lnm = new LengthNormModifier(d, s);
|
||||||
|
|
||||||
|
for (int i = 2; i < args.length; i++) {
|
||||||
|
System.out.print("Updating field: " + args[i] + " " +
|
||||||
|
(new Date()).toString() + " ... ");
|
||||||
|
lnm.reSetNorms(args[i]);
|
||||||
|
System.out.println(new Date().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
d.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Directory dir;
|
||||||
|
private Similarity sim;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for code that wishes to use this class progromaticaly
|
||||||
|
*
|
||||||
|
* @param d The Directory to modify
|
||||||
|
* @param s The Similarity to use in <code>reSetNorms</code>
|
||||||
|
*/
|
||||||
|
public LengthNormModifier(Directory d, Similarity s) {
|
||||||
|
dir = d;
|
||||||
|
sim = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Re-Set the norms for the specified field.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Opens a new IndexReader on the Directory given to this instance,
|
||||||
|
* modifies the norms using the Similarity given to this instance,
|
||||||
|
* and closes the IndexReader.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param field the field whose norms
|
||||||
|
*/
|
||||||
|
public void reSetNorms(String field) throws IOException {
|
||||||
|
String fieldName = field.intern();
|
||||||
|
int[] termCounts = new int[0];
|
||||||
|
|
||||||
|
IndexReader reader = null;
|
||||||
|
TermEnum termEnum = null;
|
||||||
|
TermDocs termDocs = null;
|
||||||
|
try {
|
||||||
|
reader = IndexReader.open(dir);
|
||||||
|
termCounts = new int[reader.maxDoc()];
|
||||||
|
try {
|
||||||
|
termEnum = reader.terms(new Term(field,""));
|
||||||
|
try {
|
||||||
|
termDocs = reader.termDocs();
|
||||||
|
do {
|
||||||
|
Term term = termEnum.term();
|
||||||
|
if (term != null && term.field().equals(fieldName)) {
|
||||||
|
termDocs.seek(termEnum.term());
|
||||||
|
while (termDocs.next()) {
|
||||||
|
termCounts[termDocs.doc()] += termDocs.freq();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (termEnum.next());
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (null != termDocs) termDocs.close();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (null != termEnum) termEnum.close();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (null != reader) reader.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
reader = IndexReader.open(dir);
|
||||||
|
for (int d = 0; d < termCounts.length; d++) {
|
||||||
|
if (! reader.isDeleted(d)) {
|
||||||
|
byte norm = sim.encodeNorm
|
||||||
|
(sim.lengthNorm(fieldName, termCounts[d]));
|
||||||
|
reader.setNorm(d, fieldName, norm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (null != reader) reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue