FileDocCategorySizeDatePackage
FunctionTestSetup.javaAPI DocApache Lucene 2.2.05249Sat Jun 16 22:20:26 BST 2007org.apache.lucene.search.function

FunctionTestSetup

public abstract class FunctionTestSetup extends TestCase
Setup for function tests

Fields Summary
public static float
TEST_SCORE_TOLERANCE_DELTA
Actual score computation order is slightly different than assumptios this allows for a small amount of variation
protected static final boolean
DBG
protected static final int
N_DOCS
protected static final String
ID_FIELD
protected static final String
TEXT_FIELD
protected static final String
INT_FIELD
protected static final String
FLOAT_FIELD
private static final String[]
DOC_TEXT_LINES
protected Directory
dir
protected Analyzer
anlzr
Constructors Summary
public FunctionTestSetup(String name)

  
  /* @override constructor */
     
    super(name);
  
Methods Summary
private voidaddDoc(org.apache.lucene.index.IndexWriter iw, int i)

    Document d = new Document();
    Fieldable f;
    int scoreAndID = i+1;
    
    f = new Field(ID_FIELD,id2String(scoreAndID),Field.Store.YES,Field.Index.UN_TOKENIZED); // for debug purposes
    f.setOmitNorms(true);
    d.add(f);
    
    f = new Field(TEXT_FIELD,"text of doc"+scoreAndID+textLine(i),Field.Store.NO,Field.Index.TOKENIZED); // for regular search
    f.setOmitNorms(true);
    d.add(f);
    
    f = new Field(INT_FIELD,""+scoreAndID,Field.Store.NO,Field.Index.UN_TOKENIZED); // for function scoring
    f.setOmitNorms(true);
    d.add(f);
    
    f = new Field(FLOAT_FIELD,scoreAndID+".000",Field.Store.NO,Field.Index.UN_TOKENIZED); // for function scoring
    f.setOmitNorms(true);
    d.add(f);

    iw.addDocument(d);
    log("added: "+d);
  
protected floatexpectedFieldScore(java.lang.String docIDFieldVal)

    return Float.parseFloat(docIDFieldVal.substring(2)); 
  
protected java.lang.Stringid2String(int scoreAndID)

    String s = "000000000"+scoreAndID;
    int n = (""+N_DOCS).length() + 3;
    int k = s.length() - n; 
    return "ID"+s.substring(k);
  
protected voidlog(java.lang.Object o)

    if (DBG) {
      System.out.println(o.toString());
    }
  
protected voidsetUp()

    // prepare a small index with just a few documents.  
    super.setUp();
    dir = new RAMDirectory();
    anlzr = new StandardAnalyzer();
    IndexWriter iw = new IndexWriter(dir,anlzr);
    // add docs not exactly in natural ID order, to verify we do check the order of docs by scores
    int remaining = N_DOCS;
    boolean done[] = new boolean[N_DOCS];
    int i = 0;
    while (remaining>0) {
      if (done[i]) {
        throw new Exception("to set this test correctly N_DOCS="+N_DOCS+" must be primary and greater than 2!");
      }
      addDoc(iw,i);
      done[i] = true;
      i = (i+4)%N_DOCS;
      remaining --;
    }
    iw.close();
  
protected voidtearDown()

    super.tearDown();
    dir = null;
    anlzr = null;
  
private java.lang.StringtextLine(int docNum)

    return DOC_TEXT_LINES[docNum % DOC_TEXT_LINES.length];