Index: src/test/org/apache/lucene/search/CheckHits.java =================================================================== --- src/test/org/apache/lucene/search/CheckHits.java (revision 543620) +++ src/test/org/apache/lucene/search/CheckHits.java (working copy) @@ -334,11 +334,77 @@ TestCase.assertNotNull("Explanation of [["+d+"]] for #"+doc+" is null", exp); - TestCase.assertEquals("Score of [["+d+"]] for #"+doc+ - " does not match explanation: " + exp.toString(), - score, exp.getValue(), SCORE_TOLERANCE_DELTA); + verifyExplanation(q,doc,score,exp); } + // check that an explanation has the expected score, and that its + // sub-details max/sum/factor match to that score. + private static void verifyExplanation (Query q, int doc, float score, Explanation expl) { + float value = expl.getValue(); + TestCase.assertEquals(q+": score(doc="+doc+")="+score+ + " != explanationScore="+value+" Explanation: "+expl, + score,value,SCORE_TOLERANCE_DELTA); + Explanation detail[] = expl.getDetails(); + if (detail!=null) { + if (detail.length==1) { + // simple containment, no matter what the description says, just verify contained expl has same score + verifyExplanation(q,doc,score,detail[0]); + } else { + // explanation must either: + // - end with one of: "product of:", "sum of:", "max of:", or + // - have "max plus times others" (where is float). + float x = 0; + String descr = expl.getDescription().toLowerCase(); + boolean productOf = descr.endsWith("product of:"); + boolean sumOf = descr.endsWith("sum of:"); + boolean maxOf = descr.endsWith("max of:"); + boolean maxTimesOthers = false; + if (!(productOf || sumOf || maxOf)) { + // maybe 'max plus x times others' + int k1 = descr.indexOf("max plus "); + if (k1>=0) { + k1 += "max plus ".length(); + int k2 = descr.indexOf(" ",k1); + try { + x = Float.parseFloat(descr.substring(k1,k2).trim()); + if (descr.substring(k2).trim().equals("times others of:")) { + maxTimesOthers = true; + } + } catch (NumberFormatException e) { + } + } + } + TestCase.assertTrue( + q+": multi valued explanation description="+descr+" must be 'max of plus x times others' or end with 'prodoct of' or 'sum of:' or 'max of:' - "+expl, + productOf || sumOf || maxOf || maxTimesOthers); + float sum = 0; + float product = 1; + float max = 0; + for (int i=0; i