Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-15493: Throw an error message if the feature store doesn't exist (#1487)

Ilaria Petreti69800fd88418
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index bd655230fef..ad82bb6e874 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -98,6 +98,8 @@ Improvements
* SOLR-16711: Extract SolrCLI tool implementations into their own classes and reorganize CLI related classes into new org.apache.solr.cli package. (Eric Pugh)
+* SOLR-15493: Throw an error message if the feature store doesn't exist. (Ilaria Petreti via Alessandro Benedetti)
+
Optimizations
---------------------
diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRFeatureLoggerTransformerFactory.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRFeatureLoggerTransformerFactory.java
index 8abb53a10c1..b3646097745 100644
--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRFeatureLoggerTransformerFactory.java
+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRFeatureLoggerTransformerFactory.java
@@ -230,7 +230,8 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {
Map<String, String[]> transformerExternalFeatureInfo =
LTRQParserPlugin.extractEFIParams(localparams);
- final LoggingModel loggingModel = createLoggingModel(transformerFeatureStore);
+ final LoggingModel loggingModel =
+ createLoggingModel(transformerFeatureStore, docsWereNotReranked);
setupRerankingQueriesForLogging(
transformerFeatureStore, transformerExternalFeatureInfo, loggingModel);
setupRerankingWeightsForLogging(context);
@@ -241,10 +242,31 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {
*
* @param transformerFeatureStore the explicit transformer feature store
*/
- private LoggingModel createLoggingModel(String transformerFeatureStore) {
- final ManagedFeatureStore fr = ManagedFeatureStore.getManagedFeatureStore(req.getCore());
+ private LoggingModel createLoggingModel(
+ String transformerFeatureStore, Boolean docsWereNotReranked) {
+ final ManagedFeatureStore featureStores =
+ ManagedFeatureStore.getManagedFeatureStore(req.getCore());
+
+ // check for non-existent feature store name
+ if (transformerFeatureStore != null) {
+ if (!featureStores.getStores().containsKey(transformerFeatureStore)) {
+ throw new SolrException(
+ SolrException.ErrorCode.BAD_REQUEST,
+ "Missing feature store: " + transformerFeatureStore);
+ }
+ }
+
+ final FeatureStore store = featureStores.getFeatureStore(transformerFeatureStore);
- final FeatureStore store = fr.getFeatureStore(transformerFeatureStore);
+ // check for empty feature store only if there is no reranking query, otherwise the model
+ // store will be used later for feature extraction
+ if (docsWereNotReranked) {
+ if (store.getFeatures().isEmpty()) {
+ throw new SolrException(
+ SolrException.ErrorCode.BAD_REQUEST,
+ "Empty feature store. If no ranking query (rq) is passed and DEFAULT feature store is empty, an existing feature store name must be passed for feature extraction.");
+ }
+ }
// if transformerFeatureStore was null before this gets actual name
transformerFeatureStore = store.getName();
diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedFeatureStore.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedFeatureStore.java
index ab2cb5b3592..787f6c6e77d 100644
--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedFeatureStore.java
+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedFeatureStore.java
@@ -82,6 +82,10 @@ public class ManagedFeatureStore extends ManagedResource
super(resourceId, loader, storageIO);
}
+ public Map<String, FeatureStore> getStores() {
+ return stores;
+ }
+
public synchronized FeatureStore getFeatureStore(String name) {
if (name == null) {
name = FeatureStore.DEFAULT_FEATURE_STORE_NAME;
@@ -160,11 +164,12 @@ public class ManagedFeatureStore extends ManagedResource
if (childId == null) {
response.add(FEATURE_STORE_JSON_FIELD, stores.keySet());
} else {
- final FeatureStore store = getFeatureStore(childId);
- if (store == null) {
+ // check for non-existent feature store name
+ if (!stores.containsKey(childId)) {
throw new SolrException(
- SolrException.ErrorCode.BAD_REQUEST, "missing feature store [" + childId + "]");
+ SolrException.ErrorCode.BAD_REQUEST, "Missing feature store: " + childId);
}
+ final FeatureStore store = getFeatureStore(childId);
response.add(FEATURES_JSON_FIELD, featuresAsManagedResources(store));
}
}
diff --git a/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestFeatureLogging.java b/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestFeatureLogging.java
index 38d833f1817..0c920d717ca 100644
--- a/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestFeatureLogging.java
+++ b/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestFeatureLogging.java
@@ -152,6 +152,74 @@ public class TestFeatureLogging extends TestRerankBase {
+ "'}");
}
+ @Test
+ public void featureExtraction_nonExistentFeatureStore_shouldThrowException() throws Exception {
+ loadFeature("store9f1", ValueFeature.class.getName(), "store9", "{\"value\":3.0}");
+ loadModel(
+ "store9m1",
+ LinearModel.class.getName(),
+ new String[] {"store9f1"},
+ "store9",
+ "{\"weights\":{\"store9f1\":1.0}}");
+
+ final SolrQuery query = new SolrQuery();
+ query.setQuery("id:7");
+ query.add("rows", "1");
+
+ // Non-existent store specified, should throw exception
+ query.add("fl", "fv:[fv store=nonExistentFeatureStore]");
+ assertJQ(
+ "/query" + query.toQueryString(),
+ "/error/msg=='Missing feature store: nonExistentFeatureStore'");
+ assertJQ("/query" + query.toQueryString(), "/error/code==400");
+ }
+
+ @Test
+ public void featureExtraction_nonExistentFeatureStoreReRanking_shouldThrowException()
+ throws Exception {
+ loadFeature("store9f1", ValueFeature.class.getName(), "store9", "{\"value\":3.0}");
+ loadModel(
+ "store9m1",
+ LinearModel.class.getName(),
+ new String[] {"store9f1"},
+ "store9",
+ "{\"weights\":{\"store9f1\":1.0}}");
+
+ final SolrQuery query = new SolrQuery();
+ query.setQuery("id:7");
+ query.add("rows", "1");
+
+ // Non-existent store specified + model specified, should throw exception
+ query.add("fl", "fv:[fv store=nonExistentFeatureStore]");
+ query.add("rq", "{!ltr reRankDocs=3 model=store9m1}");
+ assertJQ(
+ "/query" + query.toQueryString(),
+ "/error/msg=='Missing feature store: nonExistentFeatureStore'");
+ assertJQ("/query" + query.toQueryString(), "/error/code==400");
+ }
+
+ @Test
+ public void featureExtraction_noFeatureAndModelStore_shouldThrowException() throws Exception {
+ loadFeature("store9f1", ValueFeature.class.getName(), "store9", "{\"value\":3.0}");
+ loadModel(
+ "store9m1",
+ LinearModel.class.getName(),
+ new String[] {"store9f1"},
+ "store9",
+ "{\"weights\":{\"store9f1\":1.0}}");
+
+ final SolrQuery query = new SolrQuery();
+ query.setQuery("id:7");
+ query.add("rows", "1");
+
+ // No store specified + no model specified + empty default store, should throw exception
+ query.add("fl", "fv:[fv]");
+ assertJQ(
+ "/query" + query.toQueryString(),
+ "/error/msg=='Empty feature store. If no ranking query (rq) is passed and DEFAULT feature store is empty, an existing feature store name must be passed for feature extraction.'");
+ assertJQ("/query" + query.toQueryString(), "/error/code==400");
+ }
+
@Test
public void testGeneratedGroup() throws Exception {
loadFeature("c1", ValueFeature.class.getName(), "testgroup", "{\"value\":1.0}");
diff --git a/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestManagedFeatureStore.java b/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestManagedFeatureStore.java
index 137500279ae..b78709deacf 100644
--- a/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestManagedFeatureStore.java
+++ b/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestManagedFeatureStore.java
@@ -30,17 +30,17 @@ import org.junit.Test;
public class TestManagedFeatureStore extends SolrTestCaseJ4 {
- private ManagedFeatureStore fstore = null;
+ private ManagedFeatureStore featureStore = null;
@Before
public void setup() throws Exception {
initCore("solrconfig-ltr.xml", "schema.xml");
- fstore = ManagedFeatureStore.getManagedFeatureStore(h.getCore());
+ featureStore = ManagedFeatureStore.getManagedFeatureStore(h.getCore());
}
@After
public void cleanup() throws Exception {
- fstore = null;
+ featureStore = null;
deleteCore();
}
@@ -59,8 +59,8 @@ public class TestManagedFeatureStore extends SolrTestCaseJ4 {
public void testDefaultFeatureStoreName() {
assertEquals("_DEFAULT_", FeatureStore.DEFAULT_FEATURE_STORE_NAME);
final FeatureStore expectedFeatureStore =
- fstore.getFeatureStore(FeatureStore.DEFAULT_FEATURE_STORE_NAME);
- final FeatureStore actualFeatureStore = fstore.getFeatureStore(null);
+ featureStore.getFeatureStore(FeatureStore.DEFAULT_FEATURE_STORE_NAME);
+ final FeatureStore actualFeatureStore = featureStore.getFeatureStore(null);
assertEquals(
"getFeatureStore(null) should return the default feature store",
expectedFeatureStore,
@@ -69,12 +69,12 @@ public class TestManagedFeatureStore extends SolrTestCaseJ4 {
@Test
public void testFeatureStoreAdd() throws FeatureException {
- final FeatureStore fs = fstore.getFeatureStore("fstore-testFeature");
+ final FeatureStore fs = featureStore.getFeatureStore("featureStore-testFeature");
for (int i = 0; i < 5; i++) {
final String name = "c" + i;
- fstore.addFeature(
- createMap(name, OriginalScoreFeature.class.getName(), null), "fstore-testFeature");
+ featureStore.addFeature(
+ createMap(name, OriginalScoreFeature.class.getName(), null), "featureStore-testFeature");
final Feature f = fs.get(name);
assertNotNull(f);
@@ -84,14 +84,14 @@ public class TestManagedFeatureStore extends SolrTestCaseJ4 {
@Test
public void testFeatureStoreGet() throws FeatureException {
- final FeatureStore fs = fstore.getFeatureStore("fstore-testFeature2");
+ final FeatureStore fs = featureStore.getFeatureStore("featureStore-testFeature2");
for (int i = 0; i < 5; i++) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("value", i);
final String name = "c" + i;
- fstore.addFeature(
- createMap(name, ValueFeature.class.getName(), params), "fstore-testFeature2");
+ featureStore.addFeature(
+ createMap(name, ValueFeature.class.getName(), params), "featureStore-testFeature2");
}
for (int i = 0; i < 5; i++) {
@@ -105,21 +105,22 @@ public class TestManagedFeatureStore extends SolrTestCaseJ4 {
@Test
public void testMissingFeatureReturnsNull() {
- final FeatureStore fs = fstore.getFeatureStore("fstore-testFeature3");
+ final FeatureStore fs = featureStore.getFeatureStore("featureStore-testFeature3");
for (int i = 0; i < 5; i++) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("value", i);
final String name = "testc" + (float) i;
- fstore.addFeature(
- createMap(name, ValueFeature.class.getName(), params), "fstore-testFeature3");
+ featureStore.addFeature(
+ createMap(name, ValueFeature.class.getName(), params), "featureStore-testFeature3");
}
assertNull(fs.get("missing_feature_name"));
}
@Test
public void getInstanceTest() throws FeatureException {
- fstore.addFeature(createMap("test", OriginalScoreFeature.class.getName(), null), "testFstore");
- final Feature feature = fstore.getFeatureStore("testFstore").get("test");
+ featureStore.addFeature(
+ createMap("test", OriginalScoreFeature.class.getName(), null), "testFeatureStore");
+ final Feature feature = featureStore.getFeatureStore("testFeatureStore").get("test");
assertNotNull(feature);
assertEquals("test", feature.getName());
assertEquals(OriginalScoreFeature.class.getName(), feature.getClass().getName());
@@ -134,7 +135,8 @@ public class TestManagedFeatureStore extends SolrTestCaseJ4 {
expectThrows(
Exception.class,
() -> {
- fstore.addFeature(createMap("test", nonExistingClassName, null), "testFstore2");
+ featureStore.addFeature(
+ createMap("test", nonExistingClassName, null), "testFeatureStore2");
});
Throwable rootError = getRootCause(ex);
assertEquals(expectedException.toString(), rootError.toString());
diff --git a/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestModelManagerPersistence.java b/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestModelManagerPersistence.java
index 3b8f076a20a..d513cfd9dc7 100644
--- a/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestModelManagerPersistence.java
+++ b/solr/modules/ltr/src/test/org/apache/solr/ltr/store/rest/TestModelManagerPersistence.java
@@ -107,18 +107,33 @@ public class TestModelManagerPersistence extends TestRerankBase {
assertJDelete(ManagedFeatureStore.REST_END_POINT + "/test2", "/responseHeader/status==0");
assertJDelete(ManagedModelStore.REST_END_POINT + "/test-model2", "/responseHeader/status==0");
- assertJQ(ManagedFeatureStore.REST_END_POINT + "/test2", "/features/==[]");
+ // check for exception
+ assertJQ(
+ ManagedFeatureStore.REST_END_POINT + "/test2",
+ "/error/msg=='Missing feature store: test2'");
+ // check that the deleted feature store is not inappropriately added again to the feature store
+ // list
+ assertJQ(ManagedFeatureStore.REST_END_POINT, "/featureStores==['test', 'test1']");
assertJQ(ManagedModelStore.REST_END_POINT, "/models/[0]/name=='test-model'");
restTestHarness.reload();
- assertJQ(ManagedFeatureStore.REST_END_POINT + "/test2", "/features/==[]");
+ assertJQ(
+ ManagedFeatureStore.REST_END_POINT + "/test2",
+ "/error/msg=='Missing feature store: test2'");
+ assertJQ(ManagedFeatureStore.REST_END_POINT, "/featureStores==['test', 'test1']");
assertJQ(ManagedModelStore.REST_END_POINT, "/models/[0]/name=='test-model'");
assertJDelete(ManagedModelStore.REST_END_POINT + "/test-model", "/responseHeader/status==0");
assertJDelete(ManagedFeatureStore.REST_END_POINT + "/test1", "/responseHeader/status==0");
- assertJQ(ManagedFeatureStore.REST_END_POINT + "/test1", "/features/==[]");
+ assertJQ(
+ ManagedFeatureStore.REST_END_POINT + "/test1",
+ "/error/msg=='Missing feature store: test1'");
+ assertJQ(ManagedFeatureStore.REST_END_POINT, "/featureStores==['test']");
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
restTestHarness.reload();
- assertJQ(ManagedFeatureStore.REST_END_POINT + "/test1", "/features/==[]");
+ assertJQ(
+ ManagedFeatureStore.REST_END_POINT + "/test1",
+ "/error/msg=='Missing feature store: test1'");
+ assertJQ(ManagedFeatureStore.REST_END_POINT, "/featureStores==['test']");
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
}
@@ -128,7 +143,7 @@ public class TestModelManagerPersistence extends TestRerankBase {
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
assertJQ(
ManagedFeatureStore.REST_END_POINT + "/" + FeatureStore.DEFAULT_FEATURE_STORE_NAME,
- "/features/==[]");
+ "/error/msg=='Missing feature store: " + FeatureStore.DEFAULT_FEATURE_STORE_NAME + "'");
// load models and features from files
loadFeatures("features-linear.json");
@@ -172,14 +187,14 @@ public class TestModelManagerPersistence extends TestRerankBase {
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
assertJQ(
ManagedFeatureStore.REST_END_POINT + "/" + FeatureStore.DEFAULT_FEATURE_STORE_NAME,
- "/features/==[]");
+ "/error/msg=='Missing feature store: " + FeatureStore.DEFAULT_FEATURE_STORE_NAME + "'");
// check persistence after reload
restTestHarness.reload();
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
assertJQ(
ManagedFeatureStore.REST_END_POINT + "/" + FeatureStore.DEFAULT_FEATURE_STORE_NAME,
- "/features/==[]");
+ "/error/msg=='Missing feature store: " + FeatureStore.DEFAULT_FEATURE_STORE_NAME + "'");
// check persistence after restart
jetty.stop();
@@ -187,7 +202,7 @@ public class TestModelManagerPersistence extends TestRerankBase {
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
assertJQ(
ManagedFeatureStore.REST_END_POINT + "/" + FeatureStore.DEFAULT_FEATURE_STORE_NAME,
- "/features/==[]");
+ "/error/msg=='Missing feature store: " + FeatureStore.DEFAULT_FEATURE_STORE_NAME + "'");
}
private static void doWrapperModelPersistenceChecks(
@@ -214,7 +229,9 @@ public class TestModelManagerPersistence extends TestRerankBase {
// check whether models and features are empty
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
- assertJQ(ManagedFeatureStore.REST_END_POINT + "/" + FS_NAME, "/features/==[]");
+ assertJQ(
+ ManagedFeatureStore.REST_END_POINT + "/" + FS_NAME,
+ "/error/msg=='Missing feature store: " + FS_NAME + "'");
// setup features
loadFeature(
@@ -260,7 +277,9 @@ public class TestModelManagerPersistence extends TestRerankBase {
restTestHarness.delete(ManagedModelStore.REST_END_POINT + "/" + modelName);
restTestHarness.delete(ManagedFeatureStore.REST_END_POINT + "/" + FS_NAME);
assertJQ(ManagedModelStore.REST_END_POINT, "/models/==[]");
- assertJQ(ManagedFeatureStore.REST_END_POINT + "/" + FS_NAME, "/features/==[]");
+ assertJQ(
+ ManagedFeatureStore.REST_END_POINT + "/" + FS_NAME,
+ "/error/msg=='Missing feature store: " + FS_NAME + "'");
// NOTE: we don't test the persistence of the deletion here because it's tested in
// testFilePersistence
@@ -330,6 +349,8 @@ public class TestModelManagerPersistence extends TestRerankBase {
assertJDelete(
ManagedFeatureStore.REST_END_POINT + "/" + featureStoreName, "/responseHeader/status==0");
- assertJQ(ManagedFeatureStore.REST_END_POINT + "/" + featureStoreName, "/features/==[]");
+ assertJQ(
+ ManagedFeatureStore.REST_END_POINT + "/" + featureStoreName,
+ "/error/msg=='Missing feature store: " + featureStoreName + "'");
}
}