Skip to content

Commit

Permalink
revise/reduce FeatureException use (in favour of ModelException and R…
Browse files Browse the repository at this point in the history
…untimeException) (apache#80)

(as preparation for relocating FeatureException.java from util to feature package)
  • Loading branch information
cpoerschke authored and Michael Nilsson committed Sep 26, 2016
1 parent 347ed36 commit 269998c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.apache.solr.ltr.log.FeatureLogger;
import org.apache.solr.ltr.ranking.Feature.FeatureWeight;
import org.apache.solr.ltr.ranking.Feature.FeatureWeight.FeatureScorer;
import org.apache.solr.ltr.util.FeatureException;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.ltr.ranking.LTRThreadModule;
import org.slf4j.Logger;
Expand Down Expand Up @@ -231,8 +230,8 @@ private void createWeights(IndexSearcher searcher, boolean needsScores, float bo
try{
FeatureWeight fw = f.createWeight(searcher, needsScores, req, originalQuery, efi);
featureWeights.add(fw);
}catch (final Exception e) {
throw new FeatureException("Exception from createWeight for " + f.toString() + " "
} catch (final Exception e) {
throw new RuntimeException("Exception from createWeight for " + f.toString() + " "
+ e.getMessage(), e);
}
}
Expand All @@ -255,9 +254,9 @@ public FeatureWeight call() throws Exception{
try {
FeatureWeight fw = f.createWeight(searcher, needsScores, req, originalQuery, efi);
return fw;
} catch (final IOException se) {
throw new FeatureException("Exception from createWeight for " + f.toString() + " "
+ se.getMessage(), se);
} catch (final Exception e) {
throw new RuntimeException("Exception from createWeight for " + f.toString() + " "
+ e.getMessage(), e);
} finally {
querySemaphore.release();
LTRThreadModule.ltrSemaphore.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.solr.ltr.feature.norm.impl.IdentityNormalizer;
import org.apache.solr.ltr.ranking.Feature;
import org.apache.solr.ltr.util.CommonLTRParams;
import org.apache.solr.ltr.util.FeatureException;
import org.apache.solr.ltr.util.ModelException;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.rest.BaseSolrResource;
Expand Down Expand Up @@ -108,40 +107,6 @@ public void loadStoredModels() {
}
}

@SuppressWarnings("unchecked")
private Normalizer parseNormalizer(Map<String,Object> featureMap) {
final Object normObj = featureMap.get(CommonLTRParams.FEATURE_NORM);
final Normalizer norm;
if (normObj != null) {
norm = fromNormalizerMap(solrResourceLoader,
(Map<String,Object>) normObj);
}
else {
norm = IdentityNormalizer.INSTANCE;
}

return norm;
}

@SuppressWarnings("unchecked")
private Feature parseFeature(Map<String,Object> featureMap,
FeatureStore featureStore) throws FeatureException,
CloneNotSupportedException {
// FIXME name shouldn't be be null, exception?
final String name = (String) featureMap.get(CommonLTRParams.FEATURE_NAME);

if (featureStores == null) {
throw new FeatureException("missing feature store");
}

Feature meta = featureStore.get(name);
if (meta == null) {
throw new FeatureException("feature " + name
+ " not found in store " + featureStore.getName());
}
return meta;
}

@SuppressWarnings("unchecked")
public LTRScoringAlgorithm makeLTRScoringAlgorithm(String json)
throws ModelException {
Expand Down Expand Up @@ -190,20 +155,22 @@ public LTRScoringAlgorithm makeLTRScoringAlgorithm(Map<String,Object> map)
final List<Feature> features = new ArrayList<>();
final List<Normalizer> norms = new ArrayList<>();
for (final Object modelFeature : featureList) {
try {
// check the declared features exist in the feature store
final Map<String,Object> modelFeatureMap =
(Map<String,Object>) modelFeature;
final Feature feature = parseFeature(modelFeatureMap,
fstore);
final Normalizer norm = parseNormalizer(modelFeatureMap);
norms.add(norm);
features.add(feature);
} catch (FeatureException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, e);
} catch (final CloneNotSupportedException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, e);
final Map<String,Object> modelFeatureMap =
(Map<String,Object>) modelFeature;

final String featureName = (String) modelFeatureMap.get(CommonLTRParams.FEATURE_NAME);
final Feature feature = (featureName == null ? null : fstore.get(featureName));
if (feature == null) {
throw new SolrException(ErrorCode.BAD_REQUEST,
"feature " + featureName + " not found in store " + fstore.getName());
}

final Object normObj = modelFeatureMap.get(CommonLTRParams.FEATURE_NORM);
final Normalizer norm = (normObj == null ? IdentityNormalizer.INSTANCE :
fromNormalizerMap(solrResourceLoader, (Map<String,Object>) normObj));

features.add(feature);
norms.add(norm);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -330,7 +297,7 @@ private static List<Object> modelAsManagedResources(ModelStore store) {
final List<Feature> featureList = modelmeta.getFeatures();
final List<Normalizer> normList = modelmeta.getNorms();
if (normList.size() != featureList.size()) {
throw new FeatureException("Every feature must have a normalizer");
throw new ModelException("Every feature must have a normalizer");
}
for (int idx = 0; idx < featureList.size(); ++idx) {
final Feature feature = featureList.get(idx);
Expand Down

0 comments on commit 269998c

Please sign in to comment.