diff --git a/src/java/org/apache/solr/request/RequiredSolrParams.java b/src/java/org/apache/solr/request/RequiredSolrParams.java index e18d09d40e1..c07d10ed862 100755 --- a/src/java/org/apache/solr/request/RequiredSolrParams.java +++ b/src/java/org/apache/solr/request/RequiredSolrParams.java @@ -106,4 +106,14 @@ public class RequiredSolrParams extends SolrParams { public boolean getFieldBool(String field, String param, boolean def) { return params.getFieldBool(field, param, def); } + + @Override + public float getFieldFloat(String field, String param, float def) { + return params.getFieldFloat(field, param, def); + } + + @Override + public String getFieldParam(String field, String param, String def) { + return params.getFieldParam(field, param, def); + } } diff --git a/src/java/org/apache/solr/request/SolrParams.java b/src/java/org/apache/solr/request/SolrParams.java index afbe6aa2c91..8d45506aa5d 100644 --- a/src/java/org/apache/solr/request/SolrParams.java +++ b/src/java/org/apache/solr/request/SolrParams.java @@ -196,6 +196,14 @@ public abstract class SolrParams { return val!=null ? val : get(param); } + /** returns the String value of the field parameter, "f.field.param", or + * the value for "param" if that is not set. If that is not set, def + */ + public String getFieldParam(String field, String param, String def) { + String val = get(fpname(field,param)); + return val!=null ? val : get(param, def); + } + /** returns the String values of the field parameter, "f.field.param", or * the values for "param" if that is not set. */ @@ -299,6 +307,29 @@ public abstract class SolrParams { } } + /** Returns the float value of the field param. */ + public Float getFieldFloat(String field, String param) { + String val = getFieldParam(field, param); + try { + return val==null ? null : Float.valueOf(val); + } + catch( Exception ex ) { + throw new SolrException( 400, ex.getMessage(), ex ); + } + } + + /** Returns the float value of the field param, + or the value for param, or def if neither is set. */ + public float getFieldFloat(String field, String param, float def) { + String val = getFieldParam(field, param); + try { + return val==null ? def : Float.parseFloat(val); + } + catch( Exception ex ) { + throw new SolrException( 400, ex.getMessage(), ex ); + } + } + /** how to transform a String into a boolean... more flexible than * Boolean.parseBoolean() to enable easier integration with html forms. */ @@ -368,3 +399,4 @@ public abstract class SolrParams { + diff --git a/src/test/org/apache/solr/util/SolrParamTest.java b/src/test/org/apache/solr/util/SolrParamTest.java index 029d91e7809..1ed0b8ff8f7 100755 --- a/src/test/org/apache/solr/util/SolrParamTest.java +++ b/src/test/org/apache/solr/util/SolrParamTest.java @@ -82,7 +82,8 @@ public class SolrParamTest extends TestCase assertEquals( pstr , params.getFieldParam( "fl", "str" ) ); assertEquals( pbool , params.getFieldBool( "fl", "bool" ) ); assertEquals( pint , params.getFieldInt( "fl", "int" ) ); - + assertEquals( pfloat , params.getFieldFloat( "fl", "float" ) ); + // Test field defaulting (fall through to non-field-specific value) assertEquals( pint , params.getFieldInt( "fff", "int" ) ); @@ -112,6 +113,8 @@ public class SolrParamTest extends TestCase assertEquals( pfloat.floatValue() , params.getFloat( "xxx", pfloat ) ); assertEquals( pbool.booleanValue() , params.getFieldBool( "xxx", "bool", pbool ) ); assertEquals( pint.intValue() , params.getFieldInt( "xxx", "int", pint ) ); + assertEquals( pfloat.floatValue() , params.getFieldFloat("xxx", "float", pfloat ) ); + assertEquals( pstr , params.getFieldParam("xxx", "str", pstr ) ); // Required params testing uses decorator final SolrParams required = params.required();