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

TestOrdValues

public class TestOrdValues extends FunctionTestSetup
Test search based on OrdFieldSource and ReverseOrdFieldSource.

Tests here create an index with a few documents, each having an indexed "id" field. The ord values of this field are later used for scoring.

The order tests use Hits to verify that docs are ordered as expected.

The exact score tests use TopDocs top to verify the exact score.

Fields Summary
Constructors Summary
public TestOrdValues(String name)

    super(name);
  
Methods Summary
private voiddoTestCaching(java.lang.String field, boolean inOrder)

    IndexSearcher s = new IndexSearcher(dir);
    Object innerArray = null;

    for (int i=0; i<10; i++) {
      ValueSource vs;
      if (inOrder) {
        vs = new OrdFieldSource(field);
      } else {
        vs = new ReverseOrdFieldSource(field);
      }
      ValueSourceQuery q = new ValueSourceQuery(vs);
      Hits h = s.search(q);
      assertEquals("All docs should be matched!",N_DOCS,h.length());
      if (i==0) {
        innerArray = q.valSrc.getValues(s.getIndexReader()).getInnerArray();
      } else {
        log(i+".  compare: "+innerArray+" to "+q.valSrc.getValues(s.getIndexReader()).getInnerArray());
        assertSame("field values should be cached and reused!", innerArray, q.valSrc.getValues(s.getIndexReader()).getInnerArray());
      }
    }
    
    ValueSource vs;
    ValueSourceQuery q;
    Hits h;
    
    // verify that different values are loaded for a different field
    String field2 = INT_FIELD;
    assertFalse(field.equals(field2)); // otherwise this test is meaningless.
    if (inOrder) {
      vs = new OrdFieldSource(field2);
    } else {
      vs = new ReverseOrdFieldSource(field2);
    }
    q = new ValueSourceQuery(vs);
    h = s.search(q);
    assertEquals("All docs should be matched!",N_DOCS,h.length());
    log("compare (should differ): "+innerArray+" to "+q.valSrc.getValues(s.getIndexReader()).getInnerArray());
    assertNotSame("different values shuold be loaded for a different field!", innerArray, q.valSrc.getValues(s.getIndexReader()).getInnerArray());

    // verify new values are reloaded (not reused) for a new reader
    s = new IndexSearcher(dir);
    if (inOrder) {
      vs = new OrdFieldSource(field);
    } else {
      vs = new ReverseOrdFieldSource(field);
    }
    q = new ValueSourceQuery(vs);
    h = s.search(q);
    assertEquals("All docs should be matched!",N_DOCS,h.length());
    log("compare (should differ): "+innerArray+" to "+q.valSrc.getValues(s.getIndexReader()).getInnerArray());
    assertNotSame("cached field values should not be reused if reader as changed!", innerArray, q.valSrc.getValues(s.getIndexReader()).getInnerArray());
  
private voiddoTestExactScore(java.lang.String field, boolean inOrder)

    IndexSearcher s = new IndexSearcher(dir);
    ValueSource vs;
    if (inOrder) {
      vs = new OrdFieldSource(field);
    } else {
      vs = new ReverseOrdFieldSource(field);
    }
    Query q = new ValueSourceQuery(vs);
    TopDocs td = s.search(q,null,1000);
    assertEquals("All docs should be matched!",N_DOCS,td.totalHits);
    ScoreDoc sd[] = td.scoreDocs;
    for (int i=0; i<sd.length; i++) {
      float score = sd[i].score;
      String id = s.getIndexReader().document(sd[i].doc).get(ID_FIELD);
      log("-------- "+i+". Explain doc "+id);
      log(s.explain(q,sd[i].doc));
      float expectedScore =  N_DOCS-i;
      assertEquals("score of result "+i+" shuould be "+expectedScore+" != "+score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA);
      String expectedId =  inOrder 
        ? id2String(N_DOCS-i) // in-order ==> larger  values first 
        : id2String(i+1);     // reverse  ==> smaller values first 
      assertTrue("id of result "+i+" shuould be "+expectedId+" != "+score, expectedId.equals(id));
    }
  
private voiddoTestRank(java.lang.String field, boolean inOrder)

    IndexSearcher s = new IndexSearcher(dir);
    ValueSource vs;
    if (inOrder) {
      vs = new OrdFieldSource(field);
    } else {
      vs = new ReverseOrdFieldSource(field);
    }
        
    Query q = new ValueSourceQuery(vs);
    log("test: "+q);
    QueryUtils.check(q,s);
    Hits h = s.search(q);
    assertEquals("All docs should be matched!",N_DOCS,h.length());
    String prevID = inOrder
      ? "IE"   // greater than all ids of docs in this test ("ID0001", etc.)
      : "IC";  // smaller than all ids of docs in this test ("ID0001", etc.)
          
    for (int i=0; i<h.length(); i++) {
      String resID = h.doc(i).get(ID_FIELD);
      log(i+".   score="+h.score(i)+"  -  "+resID);
      log(s.explain(q,h.id(i)));
      if (inOrder) {
        assertTrue("res id "+resID+" should be < prev res id "+prevID, resID.compareTo(prevID)<0);
      } else {
        assertTrue("res id "+resID+" should be > prev res id "+prevID, resID.compareTo(prevID)>0);
      }
      prevID = resID;
    }
  
protected voidsetUp()

    // prepare a small index with just a few documents.  
    super.setUp();
  
protected voidtearDown()

    super.tearDown();
  
public voidtesCachingReverseOrd()
Test caching for ReverseOrdFieldSource

    doTestCaching(ID_FIELD,false);
  
public voidtestCachingOrd()
Test caching OrdFieldSource

    doTestCaching(ID_FIELD,true);
  
public voidtestOrdFieldExactScore()
Test exact score for OrdFieldSource

    doTestExactScore(ID_FIELD,true);
  
public voidtestOrdFieldRank()
Test OrdFieldSource

    doTestRank(ID_FIELD,true);
  
public voidtestReverseOrdFieldExactScore()
Test exact score for ReverseOrdFieldSource

    doTestExactScore(ID_FIELD,false);
  
public voidtestReverseOrdFieldRank()
Test ReverseOrdFieldSource

    doTestRank(ID_FIELD,false);