Index: contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java
===================================================================
--- contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java (revision 495338)
+++ contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java (working copy)
@@ -18,23 +18,19 @@
*/
import java.io.IOException;
-import java.util.Arrays;
import junit.framework.TestCase;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.analysis.SimpleAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.search.DefaultSimilarity;
+import org.apache.lucene.search.HitCollector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Similarity;
-import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.HitCollector;
+import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.analysis.SimpleAnalyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
/**
* Tests changing of field norms with a custom similarity and with fake norms.
@@ -42,6 +38,10 @@
* @version $Id$
*/
public class TestFieldNormModifier extends TestCase {
+ private static final int N_FIELDS_WITH_NUM = 10;
+
+ private static final String FIELD_WITH_NUM = "fieldNum";
+
public TestFieldNormModifier(String name) {
super(name);
}
@@ -58,6 +58,14 @@
return (float)numTokens;
}
};
+ /** adds e.g. 7 for field called fieldNum7 */
+ public static Similarity simWithDelta = new DefaultSimilarity() {
+ public float lengthNorm(String fieldName, int numTokens) {
+ int k = fieldName.indexOf(FIELD_WITH_NUM);
+ int delta = (k<0 ? 0 : Integer.parseInt(fieldName.substring(FIELD_WITH_NUM.length())));
+ return (float)(delta+numTokens);
+ }
+ };
public void setUp() throws Exception {
IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true);
@@ -66,11 +74,17 @@
Document d = new Document();
d.add(new Field("field", "word", Field.Store.YES, Field.Index.TOKENIZED));
d.add(new Field("nonorm", "word", Field.Store.YES, Field.Index.NO_NORMS));
- d.add(new Field("untokfield", "20061212 20071212", Field.Store.YES, Field.Index.TOKENIZED));
+ d.add(new Field("untokfield", "20061212 20071212", Field.Store.YES, Field.Index.UN_TOKENIZED));
+ for (int k = 0; k < N_FIELDS_WITH_NUM; k++) {
+ d.add(new Field(FIELD_WITH_NUM+k, "word", Field.Store.YES, Field.Index.UN_TOKENIZED));
+ }
for (int j = 1; j <= i; j++) {
d.add(new Field("field", "crap", Field.Store.YES, Field.Index.TOKENIZED));
d.add(new Field("nonorm", "more words", Field.Store.YES, Field.Index.NO_NORMS));
+ for (int k = 0; k < N_FIELDS_WITH_NUM; k++) {
+ d.add(new Field(FIELD_WITH_NUM+k, "word", Field.Store.YES, Field.Index.UN_TOKENIZED));
+ }
}
writer.addDocument(d);
}
@@ -162,21 +176,54 @@
}
}
- public void testNormKiller() throws IOException {
+ public void testTokenizedFieldNormKiller() throws IOException {
+ IndexReader r = IndexReader.open(store);
+ assertTrue(r.hasNorms("field"));
+ r.close();
+
+ FieldNormModifier fnm = new FieldNormModifier(store, null);
+ fnm.killNorms("field");
+ r = IndexReader.open(store);
+ assertFalse(r.hasNorms("field"));
+ r.close();
+
+ // verify that we still get documents in the same order as originally
+ IndexSearcher searcher = new IndexSearcher(store);
+ final float[] scores = new float[NUM_DOCS];
+ float lastScore = 0.0f;
+
+ // default similarity should return the same score for all documents for this query
+ searcher.search(new TermQuery(new Term("field", "word")), new HitCollector() {
+ public final void collect(int doc, float score) {
+ scores[doc] = score;
+ }
+ });
+ searcher.close();
+
+ lastScore = scores[0];
+ for (int i = 0; i < NUM_DOCS; i++) {
+ String msg = "i=" + i + ", " + scores[i] + " == " + lastScore;
+ assertTrue(msg, scores[i] == lastScore);
+ //System.out.println(msg);
+ lastScore = scores[i];
+ }
+ }
+
+ public void testUnTokenizedFieldNormKiller() throws IOException {
IndexReader r = IndexReader.open(store);
- byte[] oldNorms = r.norms("untokfield");
+// byte[] oldNorms = r.norms("untokfield");
+ assertTrue(r.hasNorms("untokfield"));
r.close();
- FieldNormModifier fnm = new FieldNormModifier(store, s);
- fnm.reSetNorms("untokfield");
+ FieldNormModifier fnm = new FieldNormModifier(store, null);
+ fnm.killNorms("untokfield");
r = IndexReader.open(store);
- byte[] newNorms = r.norms("untokfield");
+// byte[] newNorms = r.norms("untokfield");
+ assertFalse(r.hasNorms("untokfield"));
r.close();
- assertFalse(Arrays.equals(oldNorms, newNorms));
-
// verify that we still get documents in the same order as originally
IndexSearcher searcher = new IndexSearcher(store);
final float[] scores = new float[NUM_DOCS];
@@ -198,4 +245,57 @@
lastScore = scores[i];
}
}
+
+ public void testModifiedNormValuesCombinedWithKill() throws Exception {
+ //verify initial norms
+ Similarity ds = new DefaultSimilarity();
+ IndexReader reader = IndexReader.open(store);
+ for (int i=0; i