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

TestFieldScoreQuery

public class TestFieldScoreQuery extends FunctionTestSetup
Test FieldScoreQuery search.

Tests here create an index with a few documents, each having an int value indexed field and a float value indexed field. The values of these fields are later used for scoring.

The rank tests use Hits to verify that docs are ordered (by score) as expected.

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

Fields Summary
Constructors Summary
public TestFieldScoreQuery(String name)

    super(name);
  
Methods Summary
private voiddoTestCaching(java.lang.String field, org.apache.lucene.search.function.FieldScoreQuery$Type tp)

    // prepare expected array types for comparison
    HashMap expectedArrayTypes = new HashMap();
    expectedArrayTypes.put(FieldScoreQuery.Type.BYTE, new byte[0]);
    expectedArrayTypes.put(FieldScoreQuery.Type.SHORT, new short[0]);
    expectedArrayTypes.put(FieldScoreQuery.Type.INT, new int[0]);
    expectedArrayTypes.put(FieldScoreQuery.Type.FLOAT, new float[0]);
    
    IndexSearcher s = new IndexSearcher(dir);
    Object innerArray = null;

    for (int i=0; i<10; i++) {
      FieldScoreQuery q = new FieldScoreQuery(field,tp);
      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();
        log(i+".  compare: "+innerArray.getClass()+" to "+expectedArrayTypes.get(tp).getClass());
        assertEquals("field values should be cached in the correct array type!", innerArray.getClass(),expectedArrayTypes.get(tp).getClass());
      } 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());
      }
    }
    
    // verify new values are reloaded (not reused) for a new reader
    s = new IndexSearcher(dir);
    FieldScoreQuery q = new FieldScoreQuery(field,tp);
    Hits h = s.search(q);
    assertEquals("All docs should be matched!",N_DOCS,h.length());
    log("compare: "+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, org.apache.lucene.search.function.FieldScoreQuery$Type tp)

    IndexSearcher s = new IndexSearcher(dir);
    Query q = new FieldScoreQuery(field,tp);
    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;
      log(s.explain(q,sd[i].doc));
      String id = s.getIndexReader().document(sd[i].doc).get(ID_FIELD);
      float expectedScore = expectedFieldScore(id); // "ID7" --> 7.0
      assertEquals("score of "+id+" shuould be "+expectedScore+" != "+score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA);
    }
  
private voiddoTestRank(java.lang.String field, org.apache.lucene.search.function.FieldScoreQuery$Type tp)

    IndexSearcher s = new IndexSearcher(dir);
    Query q = new FieldScoreQuery(field,tp);
    log("test: "+q);
    QueryUtils.check(q,s);
    Hits h = s.search(q);
    assertEquals("All docs should be matched!",N_DOCS,h.length());
    String prevID = "ID"+(N_DOCS+1); // greater than all ids of docs in this test
    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)));
      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 voidtestCachingByte()
Test that FieldScoreQuery of Type.BYTE caches/reuses loaded values and consumes the proper RAM resources.

    // INT field values are small enough to be parsed as byte
    doTestCaching(INT_FIELD,FieldScoreQuery.Type.BYTE);
  
public voidtestCachingFloat()
Test that FieldScoreQuery of Type.FLOAT caches/reuses loaded values and consumes the proper RAM resources.

    // INT field values can be parsed as float
    doTestCaching(INT_FIELD,FieldScoreQuery.Type.FLOAT);
    // same values, but in flot format
    doTestCaching(FLOAT_FIELD,FieldScoreQuery.Type.FLOAT);
  
public voidtestCachingInt()
Test that FieldScoreQuery of Type.INT caches/reuses loaded values and consumes the proper RAM resources.

    doTestCaching(INT_FIELD,FieldScoreQuery.Type.INT);
  
public voidtestCachingShort()
Test that FieldScoreQuery of Type.SHORT caches/reuses loaded values and consumes the proper RAM resources.

    // INT field values are small enough to be parsed as short
    doTestCaching(INT_FIELD,FieldScoreQuery.Type.SHORT);
  
public voidtestExactScoreByte()
Test that FieldScoreQuery of Type.BYTE returns the expected scores.

    // INT field values are small enough to be parsed as byte
    doTestExactScore(INT_FIELD,FieldScoreQuery.Type.BYTE);
  
public voidtestExactScoreFloat()
Test that FieldScoreQuery of Type.FLOAT returns the expected scores.

    // INT field can be parsed as float
    doTestExactScore(INT_FIELD,FieldScoreQuery.Type.FLOAT);
    // same values, but in flot format
    doTestExactScore(FLOAT_FIELD,FieldScoreQuery.Type.FLOAT);
  
public voidtestExactScoreInt()
Test that FieldScoreQuery of Type.INT returns the expected scores.

    doTestExactScore(INT_FIELD,FieldScoreQuery.Type.INT);
  
public voidtestExactScoreShort()
Test that FieldScoreQuery of Type.SHORT returns the expected scores.

    // INT field values are small enough to be parsed as short
    doTestExactScore(INT_FIELD,FieldScoreQuery.Type.SHORT);
  
public voidtestRankByte()
Test that FieldScoreQuery of Type.BYTE returns docs in expected order.

    // INT field values are small enough to be parsed as byte
    doTestRank(INT_FIELD,FieldScoreQuery.Type.BYTE);
  
public voidtestRankFloat()
Test that FieldScoreQuery of Type.FLOAT returns docs in expected order.

    // INT field can be parsed as float
    doTestRank(INT_FIELD,FieldScoreQuery.Type.FLOAT);
    // same values, but in flot format
    doTestRank(FLOAT_FIELD,FieldScoreQuery.Type.FLOAT);
  
public voidtestRankInt()
Test that FieldScoreQuery of Type.INT returns docs in expected order.

    doTestRank(INT_FIELD,FieldScoreQuery.Type.INT);
  
public voidtestRankShort()
Test that FieldScoreQuery of Type.SHORT returns docs in expected order.

    // INT field values are small enough to be parsed as short
    doTestRank(INT_FIELD,FieldScoreQuery.Type.SHORT);