FileDocCategorySizeDatePackage
TestFieldNormModifier.javaAPI DocApache Lucene 2.1.06538Wed Feb 14 10:46:22 GMT 2007org.apache.lucene.index

TestFieldNormModifier

public class TestFieldNormModifier extends TestCase
Tests changing of field norms with a custom similarity and with fake norms.
version
$Id$

Fields Summary
public static byte
DEFAULT_NORM
public static int
NUM_DOCS
public Directory
store
public static Similarity
s
inverts the normal notion of lengthNorm
Constructors Summary
public TestFieldNormModifier(String name)

    super(name);
  
Methods Summary
public voidsetUp()

  
       
    IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true);
    
    for (int i = 0; i < NUM_DOCS; i++) {
      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));
      
      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));
      }
      writer.addDocument(d);
    }
    writer.close();
  
public voidtestFieldWithNoNorm()

    
    IndexReader r = IndexReader.open(store);
    byte[] norms = r.norms("nonorm");
    
    // sanity check, norms should all be 1
    assertTrue("Whoops we have norms?", !r.hasNorms("nonorm"));
    for (int i = 0; i< norms.length; i++) {
      assertEquals(""+i, DEFAULT_NORM, norms[i]);
    }
    
    r.close();
    
    FieldNormModifier fnm = new FieldNormModifier(store, s);
    try {
      fnm.reSetNorms("nonorm");
    } catch (Exception e) {
      assertNull("caught something", e);
    }
    
    // nothing should have changed
    r = IndexReader.open(store);
    
    norms = r.norms("nonorm");
    assertTrue("Whoops we have norms?", !r.hasNorms("nonorm"));
    for (int i = 0; i< norms.length; i++) {
      assertEquals(""+i, DEFAULT_NORM, norms[i]);
    }

    r.close();
  
public voidtestGoodCases()

    
    IndexSearcher searcher = new IndexSearcher(store);
    final float[] scores = new float[NUM_DOCS];
    float lastScore = 0.0f;
    
    // default similarity should put docs with shorter length first
    searcher.search(new TermQuery(new Term("field", "word")), new HitCollector() {
      public final void collect(int doc, float score) {
        scores[doc] = score;
      }
    });
    searcher.close();
    
    lastScore = Float.MAX_VALUE;
    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];
    }

    FieldNormModifier fnm = new FieldNormModifier(store, s);
    fnm.reSetNorms("field");
    
    // new norm (with default similarity) should put longer docs first
    searcher = new IndexSearcher(store);
    searcher.search(new TermQuery(new Term("field", "word")),  new HitCollector() {
      public final void collect(int doc, float score) {
        scores[doc] = score;
      }
    });
    searcher.close();
    
    lastScore = 0.0f;
    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 voidtestMissingField()

    FieldNormModifier fnm = new FieldNormModifier(store, s);
    try {
      fnm.reSetNorms("nobodyherebutuschickens");
    } catch (Exception e) {
      assertNull("caught something", e);
    }
  
public voidtestNormKiller()


    IndexReader r = IndexReader.open(store);
    byte[] oldNorms = r.norms("untokfield");    
    r.close();
    
    FieldNormModifier fnm = new FieldNormModifier(store, s);
    fnm.reSetNorms("untokfield");

    r = IndexReader.open(store);
    byte[] newNorms = r.norms("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];
    float lastScore = 0.0f;
    
    // default similarity should return the same score for all documents for this query
    searcher.search(new TermQuery(new Term("untokfield", "20061212")), 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];
    }