Apache Solr open-source search software
SOLR-10174: reduce makeFeatureWeights code duplication in tests
Christine Poerschkecf9b70b25365
diff --git a/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestLTRScoringQuery.java b/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestLTRScoringQuery.javaindex 0576c999771..e20f7e1684b 100644--- a/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestLTRScoringQuery.java+++ b/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestLTRScoringQuery.java@@ -89,16 +89,6 @@ public class TestLTRScoringQuery extends LuceneTestCase {return features;}- private static Map<String,Object> makeFeatureWeights(List<Feature> features) {- final Map<String,Object> nameParams = new HashMap<String,Object>();- final HashMap<String,Double> modelWeights = new HashMap<String,Double>();- for (final Feature feat : features) {- modelWeights.put(feat.getName(), 0.1);- }- nameParams.put("weights", modelWeights);- return nameParams;- }-private LTRScoringQuery.ModelWeight performQuery(TopDocs hits,IndexSearcher searcher, int docid, LTRScoringQuery model) throws IOException,ModelException {@@ -132,7 +122,7 @@ public class TestLTRScoringQuery extends LuceneTestCase {Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));final List<Feature> allFeatures = makeFeatures(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});- final Map<String,Object> modelParams = makeFeatureWeights(features);+ final Map<String,Object> modelParams = TestLinearModel.makeFeatureWeights(features);final LTRScoringModel algorithm1 = TestLinearModel.createLinearModel("testModelName",@@ -222,7 +212,7 @@ public class TestLTRScoringQuery extends LuceneTestCase {Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));LTRScoringModel ltrScoringModel = TestLinearModel.createLinearModel("test",features, norms, "test", allFeatures,- makeFeatureWeights(features));+ TestLinearModel.makeFeatureWeights(features));LTRScoringQuery.ModelWeight modelWeight = performQuery(hits, searcher,hits.scoreDocs[0].doc, new LTRScoringQuery(ltrScoringModel));@@ -248,7 +238,7 @@ public class TestLTRScoringQuery extends LuceneTestCase {new ArrayList<Normalizer>(Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));ltrScoringModel = TestLinearModel.createLinearModel("test",- features, norms, "test", allFeatures, makeFeatureWeights(features));+ features, norms, "test", allFeatures, TestLinearModel.makeFeatureWeights(features));modelWeight = performQuery(hits, searcher, hits.scoreDocs[0].doc,new LTRScoringQuery(ltrScoringModel));@@ -268,7 +258,7 @@ public class TestLTRScoringQuery extends LuceneTestCase {Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));try {ltrScoringModel = TestLinearModel.createLinearModel("test",- features, norms, "test", allFeatures, makeFeatureWeights(features));+ features, norms, "test", allFeatures, TestLinearModel.makeFeatureWeights(features));fail("unexpectedly got here instead of catching "+expectedModelException);modelWeight = performQuery(hits, searcher, hits.scoreDocs[0].doc,new LTRScoringQuery(ltrScoringModel));@@ -301,7 +291,7 @@ public class TestLTRScoringQuery extends LuceneTestCase {Collections.nCopies(features.size(),norm));final LTRScoringModel normMeta = TestLinearModel.createLinearModel("test",features, norms, "test", allFeatures,- makeFeatureWeights(features));+ TestLinearModel.makeFeatureWeights(features));modelWeight = performQuery(hits, searcher, hits.scoreDocs[0].doc,new LTRScoringQuery(normMeta));diff --git a/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestSelectiveWeightCreation.java b/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestSelectiveWeightCreation.javaindex cbd0e2389da..b2cbec907b0 100644--- a/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestSelectiveWeightCreation.java+++ b/solr/contrib/ltr/src/test/org/apache/solr/ltr/TestSelectiveWeightCreation.java@@ -70,16 +70,6 @@ public class TestSelectiveWeightCreation extends TestRerankBase {return features;}- private static Map<String,Object> makeFeatureWeights(List<Feature> features) {- final Map<String,Object> nameParams = new HashMap<String,Object>();- final HashMap<String,Double> modelWeights = new HashMap<String,Double>();- for (final Feature feat : features) {- modelWeights.put(feat.getName(), 0.1);- }- nameParams.put("weights", modelWeights);- return nameParams;- }-private LTRScoringQuery.ModelWeight performQuery(TopDocs hits,IndexSearcher searcher, int docid, LTRScoringQuery model) throws IOException,ModelException {@@ -168,7 +158,7 @@ public class TestSelectiveWeightCreation extends TestRerankBase {// when features are NOT requested in the response, only the modelFeature weights should be createdfinal LTRScoringModel ltrScoringModel1 = TestLinearModel.createLinearModel("test",features, norms, "test", allFeatures,- makeFeatureWeights(features));+ TestLinearModel.makeFeatureWeights(features));LTRScoringQuery.ModelWeight modelWeight = performQuery(hits, searcher,hits.scoreDocs[0].doc, new LTRScoringQuery(ltrScoringModel1, false)); // features not requested in responseLTRScoringQuery.FeatureInfo[] featuresInfo = modelWeight.getFeaturesInfo();@@ -185,7 +175,7 @@ public class TestSelectiveWeightCreation extends TestRerankBase {// when features are requested in the response, weights should be created for all featuresfinal LTRScoringModel ltrScoringModel2 = TestLinearModel.createLinearModel("test",features, norms, "test", allFeatures,- makeFeatureWeights(features));+ TestLinearModel.makeFeatureWeights(features));modelWeight = performQuery(hits, searcher,hits.scoreDocs[0].doc, new LTRScoringQuery(ltrScoringModel2, true)); // features requested in responsefeaturesInfo = modelWeight.getFeaturesInfo();diff --git a/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestLinearModel.java b/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestLinearModel.javaindex 067bd277de8..46496244db8 100644--- a/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestLinearModel.java+++ b/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestLinearModel.java@@ -45,6 +45,16 @@ public class TestLinearModel extends TestRerankBase {return model;}+ public static Map<String,Object> makeFeatureWeights(List<Feature> features) {+ final Map<String,Object> nameParams = new HashMap<String,Object>();+ final HashMap<String,Double> modelWeights = new HashMap<String,Double>();+ for (final Feature feat : features) {+ modelWeights.put(feat.getName(), 0.1);+ }+ nameParams.put("weights", modelWeights);+ return nameParams;+ }+static ManagedModelStore store = null;static FeatureStore fstore = null;