SOLR-13049: Make contrib/ltr Feature.defaultValue configurable. (Stanislav Livotov, Christine Poerschke)

This commit is contained in:
Christine Poerschke 2019-05-09 17:19:46 +01:00
parent c988b04b18
commit 3857388136
13 changed files with 132 additions and 7 deletions

View File

@ -79,6 +79,8 @@ New Features
* SOLR-13445: Preferred replicas on nodes with same system properties as the query master (Cao Manh Dat)
* SOLR-13049: Make contrib/ltr Feature.defaultValue configurable. (Stanislav Livotov, Christine Poerschke)
================== 8.1.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

View File

@ -61,6 +61,7 @@ public abstract class Feature extends Query {
final protected String name;
private int index = -1;
private float defaultValue = 0.0f;
private Object defaultValueObject = null;
final private Map<String,Object> params;
@ -113,8 +114,21 @@ public abstract class Feature extends Query {
return defaultValue;
}
public void setDefaultValue(String value){
defaultValue = Float.parseFloat(value);
public void setDefaultValue(Object obj){
this.defaultValueObject = obj;
if (obj instanceof String) {
defaultValue = Float.parseFloat((String)obj);
} else if (obj instanceof Double) {
defaultValue = ((Double) obj).floatValue();
} else if (obj instanceof Float) {
defaultValue = ((Float) obj).floatValue();
} else if (obj instanceof Integer) {
defaultValue = ((Integer) obj).floatValue();
} else if (obj instanceof Long) {
defaultValue = ((Long) obj).floatValue();
} else {
throw new FeatureException("Invalid type for 'defaultValue' in params for " + this);
}
}
@ -184,6 +198,15 @@ public abstract class Feature extends Query {
}
public abstract LinkedHashMap<String,Object> paramsToMap();
protected LinkedHashMap<String,Object> defaultParamsToMap() {
final LinkedHashMap<String,Object> params = new LinkedHashMap<>();
if (defaultValueObject != null) {
params.put("defaultValue", defaultValueObject);
}
return params;
}
/**
* Weight for a feature
**/

View File

@ -56,7 +56,7 @@ public class FieldLengthFeature extends Feature {
@Override
public LinkedHashMap<String,Object> paramsToMap() {
final LinkedHashMap<String,Object> params = new LinkedHashMap<>(1, 1.0f);
final LinkedHashMap<String,Object> params = defaultParamsToMap();
params.put("field", field);
return params;
}

View File

@ -58,7 +58,7 @@ public class FieldValueFeature extends Feature {
@Override
public LinkedHashMap<String,Object> paramsToMap() {
final LinkedHashMap<String,Object> params = new LinkedHashMap<>(1, 1.0f);
final LinkedHashMap<String,Object> params = defaultParamsToMap();
params.put("field", field);
return params;
}

View File

@ -47,7 +47,7 @@ public class OriginalScoreFeature extends Feature {
@Override
public LinkedHashMap<String,Object> paramsToMap() {
return null;
return defaultParamsToMap();
}
@Override

View File

@ -92,7 +92,7 @@ public class SolrFeature extends Feature {
@Override
public LinkedHashMap<String,Object> paramsToMap() {
final LinkedHashMap<String,Object> params = new LinkedHashMap<>(3, 1.0f);
final LinkedHashMap<String,Object> params = defaultParamsToMap();
if (df != null) {
params.put("df", df);
}

View File

@ -82,7 +82,7 @@ public class ValueFeature extends Feature {
@Override
public LinkedHashMap<String,Object> paramsToMap() {
final LinkedHashMap<String,Object> params = new LinkedHashMap<>(2, 1.0f);
final LinkedHashMap<String,Object> params = defaultParamsToMap();
params.put("value", value);
if (required != null) {
params.put("required", required);

View File

@ -24,6 +24,7 @@ import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
@ -475,4 +476,59 @@ public class TestRerankBase extends RestTestBase {
scn.close();
}
protected static void doTestParamsToMap(String featureClassName,
LinkedHashMap<String,Object> featureParams) throws Exception {
// start with default parameters
final LinkedHashMap<String,Object> paramsA = new LinkedHashMap<String,Object>();
final Object defaultValue;
switch (random().nextInt(6)) {
case 0:
defaultValue = null;
break;
case 1:
defaultValue = "1.2";
break;
case 2:
defaultValue = Double.valueOf(3.4d);
break;
case 3:
defaultValue = Float.valueOf(0.5f);
break;
case 4:
defaultValue = Integer.valueOf(67);
break;
case 5:
defaultValue = Long.valueOf(89);
break;
default:
defaultValue = null;
fail("unexpected defaultValue choice");
break;
}
if (defaultValue != null) {
paramsA.put("defaultValue", defaultValue);
}
// then add specific parameters
paramsA.putAll(featureParams);
// next choose a random feature name
final String featureName = "randomFeatureName"+random().nextInt(10);
// create a feature from the parameters
final Feature featureA = Feature.getInstance(solrResourceLoader,
featureClassName, featureName, paramsA);
// turn the feature back into parameters
final LinkedHashMap<String,Object> paramsB = featureA.paramsToMap();
// create feature B from feature A's parameters
final Feature featureB = Feature.getInstance(solrResourceLoader,
featureClassName, featureName, paramsB);
// check that feature A and feature B are identical
assertEquals(featureA, featureB);
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.solr.ltr.feature;
import java.util.LinkedHashMap;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.ltr.TestRerankBase;
import org.apache.solr.ltr.model.LinearModel;
@ -149,6 +151,12 @@ public class TestFieldLengthFeature extends TestRerankBase {
assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='1'");
}
@Test
public void testParamsToMap() throws Exception {
final LinkedHashMap<String,Object> params = new LinkedHashMap<String,Object>();
params.put("field", "field"+random().nextInt(10));
doTestParamsToMap(FieldLengthFeature.class.getName(), params);
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.solr.ltr.feature;
import java.util.LinkedHashMap;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.ltr.FeatureLoggerTestUtils;
import org.apache.solr.ltr.TestRerankBase;
@ -204,5 +206,11 @@ public class TestFieldValueFeature extends TestRerankBase {
}
@Test
public void testParamsToMap() throws Exception {
final LinkedHashMap<String,Object> params = new LinkedHashMap<String,Object>();
params.put("field", "field"+random().nextInt(10));
doTestParamsToMap(FieldValueFeature.class.getName(), params);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.solr.ltr.feature;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
@ -152,4 +153,10 @@ public class TestOriginalScoreFeature extends TestRerankBase {
}
}
@Test
public void testParamsToMap() throws Exception {
final LinkedHashMap<String,Object> params = new LinkedHashMap<String,Object>();
doTestParamsToMap(OriginalScoreFeature.class.getName(), params);
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.solr.ltr.feature;
import java.util.LinkedHashMap;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.ltr.TestRerankBase;
import org.apache.solr.ltr.model.LinearModel;
@ -120,4 +122,11 @@ public class TestRankingFeature extends TestRerankBase {
}
@Test
public void testParamsToMap() throws Exception {
final LinkedHashMap<String,Object> params = new LinkedHashMap<String,Object>();
params.put("q", "{!func}pow(popularity,2)");
doTestParamsToMap(SolrFeature.class.getName(), params);
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.solr.ltr.feature;
import java.util.LinkedHashMap;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.ltr.TestRerankBase;
import org.apache.solr.ltr.model.LinearModel;
@ -162,4 +164,14 @@ public class TestValueFeature extends TestRerankBase {
assertJQ("/query" + query.toQueryString(), "/responseHeader/status==400");
}
@Test
public void testParamsToMap() throws Exception {
final LinkedHashMap<String,Object> params = new LinkedHashMap<String,Object>();
params.put("value", "${val"+random().nextInt(10)+"}");
if (random().nextBoolean()) {
params.put("required", random().nextBoolean());
}
doTestParamsToMap(ValueFeature.class.getName(), params);
}
}