SOLR-6540 Fix NPE from strdist() func when doc value source does not exist in a doc

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1631555 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2014-10-13 21:19:47 +00:00
parent d0dd912bba
commit 269545dd91
4 changed files with 38 additions and 3 deletions

View File

@ -216,6 +216,8 @@ Bug Fixes
* SOLR-6545: Query field list with wild card on dynamic field fails.
(Burke Webster, Xu Zhang, shalin)
* SOLR-6540 Fix NPE from strdist() func when doc value source does not exist in a doc (hossman)
Optimizations
----------------------

View File

@ -51,7 +51,18 @@ public class StringDistanceFunction extends ValueSource {
@Override
public float floatVal(int doc) {
return dist.getDistance(str1DV.strVal(doc), str2DV.strVal(doc));
String s1 = str1DV.strVal(doc);
String s2 = str2DV.strVal(doc);
if (null == s1 || null == s2) {
// the only thing a missing value scores 1.0 with is another missing value
return (s1 == s2) ? 1.0F : 0.0F;
}
return dist.getDistance(s1, s2);
}
@Override
public boolean exists(int doc) {
return str1DV.exists(doc) && str2DV.exists(doc);
}
@Override

View File

@ -379,14 +379,13 @@ public class StatsComponentTest extends AbstractSolrTestCase {
// stats over a string function
assertQ("strdist func stats",
req("q", "*:*",
"fq", "-id:4", // SOLR-6540
"stats","true",
"stats.field","{!func}strdist('string22',active_s,edit)")
, "//double[@name='min'][.='0.75']"
, "//double[@name='max'][.='0.875']"
, "//double[@name='sum'][.='2.375']"
, "//long[@name='count'][.='3']"
,"//long[@name='missing'][.='0']" // SOLR-6540 ==> '1'
,"//long[@name='missing'][.='1']"
);
}

View File

@ -554,6 +554,29 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
assertQ(req("fl", "*,score", "q", "{!func}strdist(x_s, 'foit', edit)", "fq", "id:1"), "//float[@name='score']='0.75'");
assertQ(req("fl", "*,score", "q", "{!func}strdist(x_s, 'foit', jw)", "fq", "id:1"), "//float[@name='score']='0.8833333'");
assertQ(req("fl", "*,score", "q", "{!func}strdist(x_s, 'foit', ngram, 2)", "fq", "id:1"), "//float[@name='score']='0.875'");
// strdist on a missing valuesource should itself by missing, so the ValueSourceAugmenter
// should supress it...
assertQ(req("q", "id:1",
"fl", "good:strdist(x_s, 'toil', edit)",
"fl", "bad1:strdist(missing1_s, missing2_s, edit)",
"fl", "bad2:strdist(missing1_s, 'something', edit)",
"fl", "bad3:strdist(missing1_s, x_s, edit)")
, "//float[@name='good']='0.75'"
, "count(//float[starts-with(@name,'bad')])=0"
);
// in a query context, there is always a number...
//
// if a ValueSource is missing, it is maximally distant from every other
// value source *except* for another missing value source
// ie: strdist(null,null)==1 but strdist(null,anything)==0
assertQ(req("fl","score","fq", "id:1", "q", "{!func}strdist(missing1_s, missing2_s, edit)"),
"//float[@name='score']='1.0'");
assertQ(req("fl","score","fq", "id:1", "q", "{!func}strdist(missing1_s, x_s, edit)"),
"//float[@name='score']='0.0'");
assertQ(req("fl","score","fq", "id:1", "q", "{!func}strdist(missing1_s, 'const', edit)"),
"//float[@name='score']='0.0'");
}
public void dofunc(String func, double val) throws Exception {