Apache Solr open-source search software
SOLR-14920, SOLR-16029: contrib/ltr/src/java: apply & enforce 'spotless' code formatting (#189)
Christine Poerschke1c73e456ddeb
diff --git a/gradle/validation/spotless.gradle b/gradle/validation/spotless.gradleindex bfb00bcf734..a6c1472b588 100644--- a/gradle/validation/spotless.gradle+++ b/gradle/validation/spotless.gradle@@ -52,7 +52,6 @@ configure(project(":solr").subprojects) { prj ->case ":solr:modules:hadoop-auth":case ":solr:modules:hdfs":case ":solr:modules:langid":- case ":solr:modules:ltr":case ":solr:modules:scripting":case ":solr:core":case ":solr:solrj":@@ -60,6 +59,9 @@ configure(project(":solr").subprojects) { prj ->case ":solr:test-framework":targetExclude "src/**/*.java"break+ case ":solr:modules:ltr":+ targetExclude "src/test/**/*.java"+ break}}}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/CSVFeatureLogger.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/CSVFeatureLogger.javaindex 2da754e64ad..244b1b76d21 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/CSVFeatureLogger.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/CSVFeatureLogger.java@@ -16,9 +16,7 @@*/package org.apache.solr.ltr;-/**- * A feature logger that logs in csv format.- */+/** A feature logger that logs in csv format. */public class CSVFeatureLogger extends FeatureLogger {public static final char DEFAULT_KEY_VALUE_SEPARATOR = '=';public static final char DEFAULT_FEATURE_SEPARATOR = ',';@@ -39,24 +37,22 @@ public class CSVFeatureLogger extends FeatureLogger {@Overridepublic String makeFeatureVector(LTRScoringQuery.FeatureInfo[] featuresInfo) {- // Allocate the buffer to a size based on the number of features instead of the- // default 16. You need space for the name, value, and two separators per feature,- // but not all the features are expected to fire, so this is just a naive estimate.+ // Allocate the buffer to a size based on the number of features instead of the+ // default 16. You need space for the name, value, and two separators per feature,+ // but not all the features are expected to fire, so this is just a naive estimate.StringBuilder sb = new StringBuilder(featuresInfo.length * 3);boolean isDense = featureFormat.equals(FeatureFormat.DENSE);- for (LTRScoringQuery.FeatureInfo featInfo:featuresInfo) {- if (featInfo.isUsed() || isDense){+ for (LTRScoringQuery.FeatureInfo featInfo : featuresInfo) {+ if (featInfo.isUsed() || isDense) {sb.append(featInfo.getName())- .append(keyValueSep)- .append(featInfo.getValue())- .append(featureSep);+ .append(keyValueSep)+ .append(featInfo.getValue())+ .append(featureSep);}}- final String features = (sb.length() > 0 ?- sb.substring(0, sb.length() - 1) : "");+ final String features = (sb.length() > 0 ? sb.substring(0, sb.length() - 1) : "");return features;}--}\ No newline at end of file+}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/DocInfo.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/DocInfo.javaindex b3dfb9eee4b..e454d90acc2 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/DocInfo.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/DocInfo.java@@ -18,7 +18,7 @@ package org.apache.solr.ltr;import java.util.HashMap;-public class DocInfo extends HashMap<String,Object> {+public class DocInfo extends HashMap<String, Object> {// Name of key used to store the original score of a docprivate static final String ORIGINAL_DOC_SCORE = "ORIGINAL_DOC_SCORE";@@ -32,11 +32,10 @@ public class DocInfo extends HashMap<String,Object> {}public Float getOriginalDocScore() {- return (Float)get(ORIGINAL_DOC_SCORE);+ return (Float) get(ORIGINAL_DOC_SCORE);}public boolean hasOriginalDocScore() {return containsKey(ORIGINAL_DOC_SCORE);}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/FeatureLogger.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/FeatureLogger.javaindex ec14ab97734..1b43511f39c 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/FeatureLogger.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/FeatureLogger.java@@ -19,15 +19,18 @@ package org.apache.solr.ltr;import org.apache.solr.search.SolrIndexSearcher;/**- * FeatureLogger can be registered in a model and provide a strategy for logging- * the feature values.+ * FeatureLogger can be registered in a model and provide a strategy for logging the feature values.*/public abstract class FeatureLogger {- /** the name of the cache using for storing the feature value **/+ /** the name of the cache using for storing the feature value */private final String fvCacheName;- public enum FeatureFormat {DENSE, SPARSE};+ public enum FeatureFormat {+ DENSE,+ SPARSE+ };+protected final FeatureFormat featureFormat;protected FeatureLogger(String fvCacheName, FeatureFormat f) {@@ -36,46 +39,45 @@ public abstract class FeatureLogger {}/**- * Log will be called every time that the model generates the feature values- * for a document and a query.+ * Log will be called every time that the model generates the feature values for a document and a+ * query.*- * @param docid- * Solr document id whose features we are saving- * @param featuresInfo- * List of all the {@link LTRScoringQuery.FeatureInfo} objects which contain name and value- * for all the features triggered by the result set- * @return true if the logger successfully logged the features, false- * otherwise.+ * @param docid Solr document id whose features we are saving+ * @param featuresInfo List of all the {@link LTRScoringQuery.FeatureInfo} objects which contain+ * name and value for all the features triggered by the result set+ * @return true if the logger successfully logged the features, false otherwise.*/-- public boolean log(int docid, LTRScoringQuery scoringQuery,- SolrIndexSearcher searcher, LTRScoringQuery.FeatureInfo[] featuresInfo) {+ public boolean log(+ int docid,+ LTRScoringQuery scoringQuery,+ SolrIndexSearcher searcher,+ LTRScoringQuery.FeatureInfo[] featuresInfo) {final String featureVector = makeFeatureVector(featuresInfo);if (featureVector == null) {return false;}- return searcher.cacheInsert(fvCacheName,- fvCacheKey(scoringQuery, docid), featureVector) != null;+ if (null == searcher.cacheInsert(fvCacheName, fvCacheKey(scoringQuery, docid), featureVector)) {+ return false;+ }++ return true;}public abstract String makeFeatureVector(LTRScoringQuery.FeatureInfo[] featuresInfo);private static int fvCacheKey(LTRScoringQuery scoringQuery, int docid) {- return scoringQuery.hashCode() + (31 * docid);+ return scoringQuery.hashCode() + (31 * docid);}/*** populate the document with its feature vector*- * @param docid- * Solr document id+ * @param docid Solr document id* @return String representation of the list of features calculated for docid*/-- public String getFeatureVector(int docid, LTRScoringQuery scoringQuery,- SolrIndexSearcher searcher) {+ public String getFeatureVector(+ int docid, LTRScoringQuery scoringQuery, SolrIndexSearcher searcher) {return (String) searcher.cacheLookup(fvCacheName, fvCacheKey(scoringQuery, docid));}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRRescorer.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRRescorer.javaindex 4909a6f8a96..64070349bfa 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRRescorer.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRRescorer.java@@ -20,7 +20,6 @@ import java.io.IOException;import java.util.Arrays;import java.util.Comparator;import java.util.List;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.ReaderUtil;import org.apache.lucene.search.Explanation;@@ -34,16 +33,14 @@ import org.apache.lucene.search.Weight;import org.apache.solr.ltr.interleaving.OriginalRankingLTRScoringQuery;import org.apache.solr.search.SolrIndexSearcher;-/**- * Implements the rescoring logic. The top documents returned by solr with their- * original scores, will be processed by a {@link LTRScoringQuery} that will assign a- * new score to each document. The top documents will be resorted based on the- * new score.- * */+ * Implements the rescoring logic. The top documents returned by solr with their original scores,+ * will be processed by a {@link LTRScoringQuery} that will assign a new score to each document. The+ * top documents will be resorted based on the new score.+ */public class LTRRescorer extends Rescorer {- final private LTRScoringQuery scoringQuery;+ private final LTRScoringQuery scoringQuery;public LTRRescorer() {this.scoringQuery = null;@@ -53,20 +50,21 @@ public class LTRRescorer extends Rescorer {this.scoringQuery = scoringQuery;}- final private static Comparator<ScoreDoc> docComparator = Comparator.comparingInt(a -> a.doc);+ private static final Comparator<ScoreDoc> docComparator = Comparator.comparingInt(a -> a.doc);- final protected static Comparator<ScoreDoc> scoreComparator = (a, b) -> {- // Sort by score descending, then docID ascending:- if (a.score > b.score) {- return -1;- } else if (a.score < b.score) {- return 1;- } else {- // This subtraction can't overflow int- // because docIDs are >= 0:- return a.doc - b.doc;- }- };+ protected static final Comparator<ScoreDoc> scoreComparator =+ (a, b) -> {+ // Sort by score descending, then docID ascending:+ if (a.score > b.score) {+ return -1;+ } else if (a.score < b.score) {+ return 1;+ } else {+ // This subtraction can't overflow int+ // because docIDs are >= 0:+ return a.doc - b.doc;+ }+ };protected static void heapAdjust(ScoreDoc[] hits, int size, int root) {final ScoreDoc doc = hits[root];@@ -112,16 +110,13 @@ public class LTRRescorer extends Rescorer {/*** rescores the documents:*- * @param searcher- * current IndexSearcher- * @param firstPassTopDocs- * documents to rerank;- * @param topN- * documents to return;+ * @param searcher current IndexSearcher+ * @param firstPassTopDocs documents to rerank;+ * @param topN documents to return;*/@Override- public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs,- int topN) throws IOException {+ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int topN)+ throws IOException {if ((topN == 0) || (firstPassTopDocs.scoreDocs.length == 0)) {return firstPassTopDocs;}@@ -133,13 +128,15 @@ public class LTRRescorer extends Rescorer {return new TopDocs(firstPassTopDocs.totalHits, reranked);}- private ScoreDoc[] rerank(IndexSearcher searcher, int topN, ScoreDoc[] firstPassResults) throws IOException {+ private ScoreDoc[] rerank(IndexSearcher searcher, int topN, ScoreDoc[] firstPassResults)+ throws IOException {final ScoreDoc[] reranked = new ScoreDoc[topN];final List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();- final LTRScoringQuery.ModelWeight modelWeight = (LTRScoringQuery.ModelWeight) searcher- .createWeight(searcher.rewrite(scoringQuery), ScoreMode.COMPLETE, 1);+ final LTRScoringQuery.ModelWeight modelWeight =+ (LTRScoringQuery.ModelWeight)+ searcher.createWeight(searcher.rewrite(scoringQuery), ScoreMode.COMPLETE, 1);- scoreFeatures(searcher,topN, modelWeight, firstPassResults, leaves, reranked);+ scoreFeatures(searcher, topN, modelWeight, firstPassResults, leaves, reranked);// Must sort all documents that we reranked, and then select the topArrays.sort(reranked, scoreComparator);return reranked;@@ -153,9 +150,14 @@ public class LTRRescorer extends Rescorer {return hits;}- public void scoreFeatures(IndexSearcher indexSearcher,- int topN, LTRScoringQuery.ModelWeight modelWeight, ScoreDoc[] hits, List<LeafReaderContext> leaves,- ScoreDoc[] reranked) throws IOException {+ public void scoreFeatures(+ IndexSearcher indexSearcher,+ int topN,+ LTRScoringQuery.ModelWeight modelWeight,+ ScoreDoc[] hits,+ List<LeafReaderContext> leaves,+ ScoreDoc[] reranked)+ throws IOException {int readerUpto = -1;int endDoc = 0;@@ -186,30 +188,43 @@ public class LTRRescorer extends Rescorer {}/**- * Call this method if the {@link #scoreSingleHit(int, int, int, ScoreDoc, int, org.apache.solr.ltr.LTRScoringQuery.ModelWeight.ModelScorer, ScoreDoc[])}- * method indicated that the document's feature info should be logged.+ * Call this method if the {@link #scoreSingleHit(int, int, int, ScoreDoc, int,+ * org.apache.solr.ltr.LTRScoringQuery.ModelWeight.ModelScorer, ScoreDoc[])} method indicated that+ * the document's feature info should be logged.*/- protected static void logSingleHit(IndexSearcher indexSearcher, LTRScoringQuery.ModelWeight modelWeight, int docid, LTRScoringQuery scoringQuery) {+ protected static void logSingleHit(+ IndexSearcher indexSearcher,+ LTRScoringQuery.ModelWeight modelWeight,+ int docid,+ LTRScoringQuery scoringQuery) {final FeatureLogger featureLogger = scoringQuery.getFeatureLogger();if (featureLogger != null && indexSearcher instanceof SolrIndexSearcher) {- featureLogger.log(docid, scoringQuery, (SolrIndexSearcher)indexSearcher, modelWeight.getFeaturesInfo());+ featureLogger.log(+ docid, scoringQuery, (SolrIndexSearcher) indexSearcher, modelWeight.getFeaturesInfo());}}/**- * Scores a single document and returns true if the document's feature info should be logged via the- * {@link #logSingleHit(IndexSearcher, org.apache.solr.ltr.LTRScoringQuery.ModelWeight, int, LTRScoringQuery)}- * method. Feature info logging is only necessary for the topN documents.+ * Scores a single document and returns true if the document's feature info should be logged via+ * the {@link #logSingleHit(IndexSearcher, org.apache.solr.ltr.LTRScoringQuery.ModelWeight, int,+ * LTRScoringQuery)} method. Feature info logging is only necessary for the topN documents.*/- protected static boolean scoreSingleHit(int topN, int docBase, int hitUpto, ScoreDoc hit, int docID, LTRScoringQuery.ModelWeight.ModelScorer scorer, ScoreDoc[] reranked) throws IOException {- // Scorer for a LTRScoringQuery.ModelWeight should never be null since we always have to- // call score- // even if no feature scorers match, since a model might use that info to- // return a- // non-zero score. Same applies for the case of advancing a LTRScoringQuery.ModelWeight.ModelScorer- // past the target- // doc since the model algorithm still needs to compute a potentially- // non-zero score from blank features.+ protected static boolean scoreSingleHit(+ int topN,+ int docBase,+ int hitUpto,+ ScoreDoc hit,+ int docID,+ LTRScoringQuery.ModelWeight.ModelScorer scorer,+ ScoreDoc[] reranked)+ throws IOException {+ /**+ * Scorer for a LTRScoringQuery.ModelWeight should never be null since we always have to call+ * score even if no feature scorers match, since a model might use that info to return a+ * non-zero score. Same applies for the case of advancing a+ * LTRScoringQuery.ModelWeight.ModelScorer past the target doc since the model algorithm still+ * needs to compute a potentially non-zero score from blank features.+ */assert (scorer != null);final int targetDoc = docID - docBase;scorer.docID();@@ -244,37 +259,39 @@ public class LTRRescorer extends Rescorer {}@Override- public Explanation explain(IndexSearcher searcher,- Explanation firstPassExplanation, int docID) throws IOException {+ public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)+ throws IOException {return getExplanation(searcher, docID, scoringQuery);}- protected static Explanation getExplanation(IndexSearcher searcher, int docID, LTRScoringQuery rerankingQuery) throws IOException {- final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext()- .leaves();+ protected static Explanation getExplanation(+ IndexSearcher searcher, int docID, LTRScoringQuery rerankingQuery) throws IOException {+ final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext().leaves();final int n = ReaderUtil.subIndex(docID, leafContexts);final LeafReaderContext context = leafContexts.get(n);final int deBasedDoc = docID - context.docBase;final Weight rankingWeight;if (rerankingQuery instanceof OriginalRankingLTRScoringQuery) {- rankingWeight = rerankingQuery.getOriginalQuery().createWeight(searcher, ScoreMode.COMPLETE, 1);+ rankingWeight =+ rerankingQuery.getOriginalQuery().createWeight(searcher, ScoreMode.COMPLETE, 1);} else {- rankingWeight = searcher.createWeight(searcher.rewrite(rerankingQuery),- ScoreMode.COMPLETE, 1);+ rankingWeight =+ searcher.createWeight(searcher.rewrite(rerankingQuery), ScoreMode.COMPLETE, 1);}return rankingWeight.explain(context, deBasedDoc);}- public static LTRScoringQuery.FeatureInfo[] extractFeaturesInfo(LTRScoringQuery.ModelWeight modelWeight,+ public static LTRScoringQuery.FeatureInfo[] extractFeaturesInfo(+ LTRScoringQuery.ModelWeight modelWeight,int docid,Float originalDocScore,List<LeafReaderContext> leafContexts)- throws IOException {+ throws IOException {final int n = ReaderUtil.subIndex(docid, leafContexts);final LeafReaderContext atomicContext = leafContexts.get(n);final int deBasedDoc = docid - atomicContext.docBase;final LTRScoringQuery.ModelWeight.ModelScorer r = modelWeight.scorer(atomicContext);- if ( (r == null) || (r.iterator().advance(deBasedDoc) != deBasedDoc) ) {+ if ((r == null) || (r.iterator().advance(deBasedDoc) != deBasedDoc)) {return new LTRScoringQuery.FeatureInfo[0];} else {if (originalDocScore != null) {@@ -286,5 +303,4 @@ public class LTRRescorer extends Rescorer {return modelWeight.getFeaturesInfo();}}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRScoringQuery.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRScoringQuery.javaindex dcffbd6b26b..a7b3d7bd7f3 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRScoringQuery.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRScoringQuery.java@@ -29,7 +29,6 @@ import java.util.concurrent.Future;import java.util.concurrent.FutureTask;import java.util.concurrent.RunnableFuture;import java.util.concurrent.Semaphore;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.DisiPriorityQueue;import org.apache.lucene.search.DisiWrapper;@@ -50,50 +49,53 @@ import org.apache.solr.request.SolrQueryRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-/**- * The ranking query that is run, reranking results using the- * LTRScoringModel algorithm- */+/** The ranking query that is run, reranking results using the LTRScoringModel algorithm */public class LTRScoringQuery extends Query implements Accountable {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- private static final long BASE_RAM_BYTES = RamUsageEstimator.shallowSizeOfInstance(LTRScoringQuery.class);+ private static final long BASE_RAM_BYTES =+ RamUsageEstimator.shallowSizeOfInstance(LTRScoringQuery.class);// contains a description of the model- final private LTRScoringModel ltrScoringModel;- final private boolean extractAllFeatures;- final private LTRThreadModule ltrThreadMgr;- final private Semaphore querySemaphore; // limits the number of threads per query, so that multiple requests can be serviced simultaneously+ private final LTRScoringModel ltrScoringModel;+ private final boolean extractAllFeatures;+ private final LTRThreadModule ltrThreadMgr;++ // limits the number of threads per query, so that multiple requests can be serviced+ // simultaneously+ private final Semaphore querySemaphore;// feature logger to output the features.private FeatureLogger fl;// Map of external parameters, such as query intent, that can be used by// features- final private Map<String,String[]> efi;+ private final Map<String, String[]> efi;// Original solr query used to fetch matching documentsprivate Query originalQuery;// Original solr requestprivate SolrQueryRequest request;public LTRScoringQuery(LTRScoringModel ltrScoringModel) {- this(ltrScoringModel, Collections.<String,String[]>emptyMap(), false, null);+ this(ltrScoringModel, Collections.<String, String[]>emptyMap(), false, null);}public LTRScoringQuery(LTRScoringModel ltrScoringModel, boolean extractAllFeatures) {this(ltrScoringModel, Collections.<String, String[]>emptyMap(), extractAllFeatures, null);}- public LTRScoringQuery(LTRScoringModel ltrScoringModel,+ public LTRScoringQuery(+ LTRScoringModel ltrScoringModel,Map<String, String[]> externalFeatureInfo,- boolean extractAllFeatures, LTRThreadModule ltrThreadMgr) {+ boolean extractAllFeatures,+ LTRThreadModule ltrThreadMgr) {this.ltrScoringModel = ltrScoringModel;this.efi = externalFeatureInfo;this.extractAllFeatures = extractAllFeatures;this.ltrThreadMgr = ltrThreadMgr;if (this.ltrThreadMgr != null) {this.querySemaphore = this.ltrThreadMgr.createQuerySemaphore();- } else{+ } else {this.querySemaphore = null;}}@@ -122,7 +124,7 @@ public class LTRScoringQuery extends Query implements Accountable {return originalQuery;}- public Map<String,String[]> getExternalFeatureInfo() {+ public Map<String, String[]> getExternalFeatureInfo() {return efi;}@@ -139,13 +141,11 @@ public class LTRScoringQuery extends Query implements Accountable {final int prime = 31;int result = classHash();result = (prime * result) + ((ltrScoringModel == null) ? 0 : ltrScoringModel.hashCode());- result = (prime * result)- + ((originalQuery == null) ? 0 : originalQuery.hashCode());+ result = (prime * result) + ((originalQuery == null) ? 0 : originalQuery.hashCode());if (efi == null) {result = (prime * result) + 0;- }- else {- for (final Map.Entry<String,String[]> entry : efi.entrySet()) {+ } else {+ for (final Map.Entry<String, String[]> entry : efi.entrySet()) {final String key = entry.getKey();final String[] values = entry.getValue();result = (prime * result) + key.hashCode();@@ -155,9 +155,10 @@ public class LTRScoringQuery extends Query implements Accountable {result = (prime * result) + this.toString().hashCode();return result;}+@Overridepublic boolean equals(Object o) {- return sameClassAs(o) && equalsTo(getClass().cast(o));+ return sameClassAs(o) && equalsTo(getClass().cast(o));}@Override@@ -188,10 +189,10 @@ public class LTRScoringQuery extends Query implements Accountable {if (other.efi == null || efi.size() != other.efi.size()) {return false;}- for(final Map.Entry<String,String[]> entry : efi.entrySet()) {+ for (final Map.Entry<String, String[]> entry : efi.entrySet()) {final String key = entry.getKey();final String[] otherValues = other.efi.get(key);- if (otherValues == null || !Arrays.equals(otherValues,entry.getValue())) {+ if (otherValues == null || !Arrays.equals(otherValues, entry.getValue())) {return false;}}@@ -209,31 +210,31 @@ public class LTRScoringQuery extends Query implements Accountable {Collection<Feature> features = null;if (this.extractAllFeatures) {features = allFeatures;+ } else {+ features = modelFeatures;}- else{- features = modelFeatures;- }- final Feature.FeatureWeight[] extractedFeatureWeights = new Feature.FeatureWeight[features.size()];+ final Feature.FeatureWeight[] extractedFeatureWeights =+ new Feature.FeatureWeight[features.size()];final Feature.FeatureWeight[] modelFeaturesWeights = new Feature.FeatureWeight[modelFeatSize];- List<Feature.FeatureWeight > featureWeights = new ArrayList<>(features.size());+ List<Feature.FeatureWeight> featureWeights = new ArrayList<>(features.size());if (querySemaphore == null) {createWeights(searcher, scoreMode.needsScores(), featureWeights, features);- }- else{+ } else {createWeightsParallel(searcher, scoreMode.needsScores(), featureWeights, features);}- int i=0, j = 0;+ int i = 0, j = 0;if (this.extractAllFeatures) {for (final Feature.FeatureWeight fw : featureWeights) {extractedFeatureWeights[i++] = fw;}- for (final Feature f : modelFeatures){- modelFeaturesWeights[j++] = extractedFeatureWeights[f.getIndex()]; // we can lookup by featureid because all features will be extracted when this.extractAllFeatures is set+ for (final Feature f : modelFeatures) {+ // we can lookup by featureid because all features will be+ // extracted when this.extractAllFeatures is set+ modelFeaturesWeights[j++] = extractedFeatureWeights[f.getIndex()];}- }- else{- for (final Feature.FeatureWeight fw: featureWeights){+ } else {+ for (final Feature.FeatureWeight fw : featureWeights) {extractedFeatureWeights[i++] = fw;modelFeaturesWeights[j++] = fw;}@@ -241,28 +242,33 @@ public class LTRScoringQuery extends Query implements Accountable {return new ModelWeight(modelFeaturesWeights, extractedFeatureWeights, allFeatures.size());}- private void createWeights(IndexSearcher searcher, boolean needsScores,- List<Feature.FeatureWeight > featureWeights, Collection<Feature> features) throws IOException {+ private void createWeights(+ IndexSearcher searcher,+ boolean needsScores,+ List<Feature.FeatureWeight> featureWeights,+ Collection<Feature> features)+ throws IOException {final SolrQueryRequest req = getRequest();// since the feature store is a linkedhashmap order is preservedfor (final Feature f : features) {- try{+ try {Feature.FeatureWeight fw = f.createWeight(searcher, needsScores, req, originalQuery, efi);featureWeights.add(fw);} catch (final Exception e) {- throw new RuntimeException("Exception from createWeight for " + f.toString() + " "- + e.getMessage(), e);+ throw new RuntimeException(+ "Exception from createWeight for " + f.toString() + " " + e.getMessage(), e);}}}- private class CreateWeightCallable implements Callable<Feature.FeatureWeight>{- final private Feature f;- final private IndexSearcher searcher;- final private boolean needsScores;- final private SolrQueryRequest req;+ private class CreateWeightCallable implements Callable<Feature.FeatureWeight> {+ private final Feature f;+ private final IndexSearcher searcher;+ private final boolean needsScores;+ private final SolrQueryRequest req;- public CreateWeightCallable(Feature f, IndexSearcher searcher, boolean needsScores, SolrQueryRequest req){+ public CreateWeightCallable(+ Feature f, IndexSearcher searcher, boolean needsScores, SolrQueryRequest req) {this.f = f;this.searcher = searcher;this.needsScores = needsScores;@@ -270,13 +276,13 @@ public class LTRScoringQuery extends Query implements Accountable {}@Override- public Feature.FeatureWeight call() throws Exception{+ public Feature.FeatureWeight call() throws Exception {try {- Feature.FeatureWeight fw = f.createWeight(searcher, needsScores, req, originalQuery, efi);+ Feature.FeatureWeight fw = f.createWeight(searcher, needsScores, req, originalQuery, efi);return fw;} catch (final Exception e) {- throw new RuntimeException("Exception from createWeight for " + f.toString() + " "- + e.getMessage(), e);+ throw new RuntimeException(+ "Exception from createWeight for " + f.toString() + " " + e.getMessage(), e);} finally {querySemaphore.release();ltrThreadMgr.releaseLTRSemaphore();@@ -284,21 +290,29 @@ public class LTRScoringQuery extends Query implements Accountable {}} // end of call CreateWeightCallable- private void createWeightsParallel(IndexSearcher searcher, boolean needsScores,- List<Feature.FeatureWeight > featureWeights, Collection<Feature> features) throws RuntimeException {+ private void createWeightsParallel(+ IndexSearcher searcher,+ boolean needsScores,+ List<Feature.FeatureWeight> featureWeights,+ Collection<Feature> features)+ throws RuntimeException {final SolrQueryRequest req = getRequest();- List<Future<Feature.FeatureWeight> > futures = new ArrayList<>(features.size());- try{+ List<Future<Feature.FeatureWeight>> futures = new ArrayList<>(features.size());+ try {for (final Feature f : features) {CreateWeightCallable callable = new CreateWeightCallable(f, searcher, needsScores, req);RunnableFuture<Feature.FeatureWeight> runnableFuture = new FutureTask<>(callable);- querySemaphore.acquire(); // always acquire before the ltrSemaphore is acquired, to guarantee a that the current query is within the limit for max. threads- ltrThreadMgr.acquireLTRSemaphore();//may block and/or interrupt- ltrThreadMgr.execute(runnableFuture);//releases semaphore when done++ // always acquire before the ltrSemaphore is acquired, to guarantee a that+ // the current query is within the limit for max. threads+ querySemaphore.acquire();++ ltrThreadMgr.acquireLTRSemaphore(); // may block and/or interrupt+ ltrThreadMgr.execute(runnableFuture); // releases semaphore when donefutures.add(runnableFuture);}- //Loop over futures to get the feature weight objects+ // Loop over futures to get the feature weight objectsfor (final Future<Feature.FeatureWeight> future : futures) {featureWeights.add(future.get()); // future.get() will block if the job is still running}@@ -315,38 +329,41 @@ public class LTRScoringQuery extends Query implements Accountable {@Overridepublic long ramBytesUsed() {- return BASE_RAM_BYTES +- RamUsageEstimator.sizeOfObject(efi) +- RamUsageEstimator.sizeOfObject(ltrScoringModel) +- RamUsageEstimator.sizeOfObject(originalQuery, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED);+ return BASE_RAM_BYTES+ + RamUsageEstimator.sizeOfObject(efi)+ + RamUsageEstimator.sizeOfObject(ltrScoringModel)+ + RamUsageEstimator.sizeOfObject(+ originalQuery, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED);}public static class FeatureInfo {- final private String name;+ private final String name;private float value;private boolean used;- FeatureInfo(String n, float v, boolean u){- name = n; value = v; used = u;+ FeatureInfo(String n, float v, boolean u) {+ name = n;+ value = v;+ used = u;}- public void setValue(float value){+ public void setValue(float value) {this.value = value;}- public String getName(){+ public String getName() {return name;}- public float getValue(){+ public float getValue() {return value;}- public boolean isUsed(){+ public boolean isUsed() {return used;}- public void setUsed(boolean used){+ public void setUsed(boolean used) {this.used = used;}}@@ -355,9 +372,9 @@ public class LTRScoringQuery extends Query implements Accountable {// List of the model's features used for scoring. This is a subset of the// features used for logging.- final private Feature.FeatureWeight[] modelFeatureWeights;- final private float[] modelFeatureValuesNormalized;- final private Feature.FeatureWeight[] extractedFeatureWeights;+ private final Feature.FeatureWeight[] modelFeatureWeights;+ private final float[] modelFeatureValuesNormalized;+ private final Feature.FeatureWeight[] extractedFeatureWeights;// List of all the feature names, values - used for both scoring and logging/*@@ -370,7 +387,7 @@ public class LTRScoringQuery extends Query implements Accountable {* we need a map which holds just the features that were triggered by the documents in the result set.**/- final private FeatureInfo[] featuresInfo;+ private final FeatureInfo[] featuresInfo;/** @param modelFeatureWeights* - should be the same size as the number of features used by the model@@ -381,8 +398,10 @@ public class LTRScoringQuery extends Query implements Accountable {* @param allFeaturesSize* - total number of feature in the feature store used by this model*/- public ModelWeight(Feature.FeatureWeight[] modelFeatureWeights,- Feature.FeatureWeight[] extractedFeatureWeights, int allFeaturesSize) {+ public ModelWeight(+ Feature.FeatureWeight[] modelFeatureWeights,+ Feature.FeatureWeight[] extractedFeatureWeights,+ int allFeaturesSize) {super(LTRScoringQuery.this);this.extractedFeatureWeights = extractedFeatureWeights;this.modelFeatureWeights = modelFeatureWeights;@@ -391,16 +410,16 @@ public class LTRScoringQuery extends Query implements Accountable {setFeaturesInfo();}- private void setFeaturesInfo(){- for (int i = 0; i < extractedFeatureWeights.length;++i){+ private void setFeaturesInfo() {+ for (int i = 0; i < extractedFeatureWeights.length; ++i) {String featName = extractedFeatureWeights[i].getName();int featId = extractedFeatureWeights[i].getIndex();float value = extractedFeatureWeights[i].getDefaultValue();- featuresInfo[featId] = new FeatureInfo(featName,value,false);+ featuresInfo[featId] = new FeatureInfo(featName, value, false);}}- public FeatureInfo[] getFeaturesInfo(){+ public FeatureInfo[] getFeaturesInfo() {return featuresInfo;}@@ -420,16 +439,16 @@ public class LTRScoringQuery extends Query implements Accountable {}/**- * Goes through all the stored feature values, and calculates the normalized- * values for all the features that will be used for scoring.- * Then calculate and return the model's score.+ * Goes through all the stored feature values, and calculates the normalized values for all the+ * features that will be used for scoring. Then calculate and return the model's score.*/private float makeNormalizedFeaturesAndScore() {int pos = 0;for (final Feature.FeatureWeight feature : modelFeatureWeights) {final int featureId = feature.getIndex();FeatureInfo fInfo = featuresInfo[featureId];- if (fInfo.isUsed()) { // not checking for finfo == null as that would be a bug we should catch+ // not checking for finfo == null as that would be a bug we should catch+ if (fInfo.isUsed()) {modelFeatureValuesNormalized[pos] = fInfo.getValue();} else {modelFeatureValuesNormalized[pos] = feature.getDefaultValue();@@ -441,15 +460,14 @@ public class LTRScoringQuery extends Query implements Accountable {}@Override- public Explanation explain(LeafReaderContext context, int doc)- throws IOException {+ public Explanation explain(LeafReaderContext context, int doc) throws IOException {final Explanation[] explanations = new Explanation[this.featuresInfo.length];for (final Feature.FeatureWeight feature : extractedFeatureWeights) {explanations[feature.getIndex()] = feature.explain(context, doc);}final List<Explanation> featureExplanations = new ArrayList<>();- for (int idx = 0 ;idx < modelFeatureWeights.length; ++idx) {+ for (int idx = 0; idx < modelFeatureWeights.length; ++idx) {final Feature.FeatureWeight f = modelFeatureWeights[idx];Explanation e = ltrScoringModel.getNormalizerExplanation(explanations[f.getIndex()], idx);featureExplanations.add(e);@@ -460,14 +478,15 @@ public class LTRScoringQuery extends Query implements Accountable {final float finalScore = bs.score();return ltrScoringModel.explain(context, doc, finalScore, featureExplanations);-}protected void reset() {- for (int i = 0; i < extractedFeatureWeights.length;++i){+ for (int i = 0; i < extractedFeatureWeights.length; ++i) {int featId = extractedFeatureWeights[i].getIndex();float value = extractedFeatureWeights[i].getDefaultValue();- featuresInfo[featId].setValue(value); // need to set default value everytime as the default value is used in 'dense' mode even if used=false+ // need to set default value everytime as the default value is used in 'dense'+ // mode even if used=false+ featuresInfo[featId].setValue(value);featuresInfo[featId].setUsed(false);}}@@ -475,8 +494,8 @@ public class LTRScoringQuery extends Query implements Accountable {@Overridepublic ModelScorer scorer(LeafReaderContext context) throws IOException {- final List<Feature.FeatureWeight.FeatureScorer> featureScorers = new ArrayList<Feature.FeatureWeight.FeatureScorer>(- extractedFeatureWeights.length);+ final List<Feature.FeatureWeight.FeatureScorer> featureScorers =+ new ArrayList<Feature.FeatureWeight.FeatureScorer>(extractedFeatureWeights.length);for (final Feature.FeatureWeight featureWeight : extractedFeatureWeights) {final Feature.FeatureWeight.FeatureScorer scorer = featureWeight.scorer(context);if (scorer != null) {@@ -490,7 +509,6 @@ public class LTRScoringQuery extends Query implements Accountable {// non 0 score for a given model.ModelScorer mscorer = new ModelScorer(this, featureScorers);return mscorer;-}@Override@@ -499,8 +517,8 @@ public class LTRScoringQuery extends Query implements Accountable {}public class ModelScorer extends Scorer {- final private DocInfo docInfo;- final private Scorer featureTraversalScorer;+ private final DocInfo docInfo;+ private final Scorer featureTraversalScorer;public DocInfo getDocInfo() {return docInfo;@@ -546,18 +564,17 @@ public class LTRScoringQuery extends Query implements Accountable {}private class SparseModelScorer extends Scorer {- final private DisiPriorityQueue subScorers;- final private ScoringQuerySparseIterator itr;+ private final DisiPriorityQueue subScorers;+ private final ScoringQuerySparseIterator itr;private int targetDoc = -1;private int activeDoc = -1;- private SparseModelScorer(Weight weight,- List<Feature.FeatureWeight.FeatureScorer> featureScorers) {+ private SparseModelScorer(+ Weight weight, List<Feature.FeatureWeight.FeatureScorer> featureScorers) {super(weight);if (featureScorers.size() <= 1) {- throw new IllegalArgumentException(- "There must be at least 2 subScorers");+ throw new IllegalArgumentException("There must be at least 2 subScorers");}subScorers = new DisiPriorityQueue(featureScorers.size());for (final Scorer scorer : featureScorers) {@@ -643,17 +660,16 @@ public class LTRScoringQuery extends Query implements Accountable {return targetDoc;}}-}private class DenseModelScorer extends Scorer {private int activeDoc = -1; // The doc that our scorer's are actually atprivate int targetDoc = -1; // The doc we were most recently told to go toprivate int freq = -1;- final private List<Feature.FeatureWeight.FeatureScorer> featureScorers;+ private final List<Feature.FeatureWeight.FeatureScorer> featureScorers;- private DenseModelScorer(Weight weight,- List<Feature.FeatureWeight.FeatureScorer> featureScorers) {+ private DenseModelScorer(+ Weight weight, List<Feature.FeatureWeight.FeatureScorer> featureScorers) {super(weight);this.featureScorers = featureScorers;}@@ -685,7 +701,7 @@ public class LTRScoringQuery extends Query implements Accountable {public float getMaxScore(int upTo) throws IOException {return Float.POSITIVE_INFINITY;}-+@Overridepublic final Collection<ChildScorable> getChildren() {final ArrayList<ChildScorable> children = new ArrayList<>();@@ -726,8 +742,7 @@ public class LTRScoringQuery extends Query implements Accountable {activeDoc = NO_MORE_DOCS;for (final Scorer scorer : featureScorers) {if (scorer.docID() != NO_MORE_DOCS) {- activeDoc = Math.min(activeDoc,- scorer.iterator().advance(target));+ activeDoc = Math.min(activeDoc, scorer.iterator().advance(target));}}}@@ -743,10 +758,8 @@ public class LTRScoringQuery extends Query implements Accountable {}return sum;}-}}}}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRThreadModule.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRThreadModule.javaindex 2f5c337d271..dccae8bb319 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRThreadModule.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/LTRThreadModule.java@@ -19,41 +19,43 @@ package org.apache.solr.ltr;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Semaphore;-import org.apache.solr.common.util.NamedList;import org.apache.solr.util.SolrPluginUtils;import org.apache.solr.util.plugin.NamedListInitializedPlugin;/**- * The LTRThreadModule is optionally used by the {@link org.apache.solr.ltr.search.LTRQParserPlugin} and- * {@link org.apache.solr.ltr.response.transform.LTRFeatureLoggerTransformerFactory LTRFeatureLoggerTransformerFactory}- * classes to parallelize the creation of {@link org.apache.solr.ltr.feature.Feature.FeatureWeight Feature.FeatureWeight}- * objects.- * <p>- * Example configuration:+ * The LTRThreadModule is optionally used by the {@link org.apache.solr.ltr.search.LTRQParserPlugin}+ * and {@link org.apache.solr.ltr.response.transform.LTRFeatureLoggerTransformerFactory+ * LTRFeatureLoggerTransformerFactory} classes to parallelize the creation of {@link+ * org.apache.solr.ltr.feature.Feature.FeatureWeight Feature.FeatureWeight} objects.+ *+ * <p>Example configuration:+ ** <pre>- <queryParser name="ltr" class="org.apache.solr.ltr.search.LTRQParserPlugin">- <int name="threadModule.totalPoolThreads">10</int>- <int name="threadModule.numThreadsPerRequest">5</int>- </queryParser>-- <transformer name="features" class="org.apache.solr.ltr.response.transform.LTRFeatureLoggerTransformerFactory">- <int name="threadModule.totalPoolThreads">10</int>- <int name="threadModule.numThreadsPerRequest">5</int>- </transformer>-</pre>- * If an individual solr instance is expected to receive no more than one query at a time, it is best- * to set <code>totalPoolThreads</code> and <code>numThreadsPerRequest</code> to the same value.+ * <queryParser name="ltr" class="org.apache.solr.ltr.search.LTRQParserPlugin">+ * <int name="threadModule.totalPoolThreads">10</int>+ * <int name="threadModule.numThreadsPerRequest">5</int>+ * </queryParser>+ *+ * <transformer name="features" class="org.apache.solr.ltr.response.transform.LTRFeatureLoggerTransformerFactory">+ * <int name="threadModule.totalPoolThreads">10</int>+ * <int name="threadModule.numThreadsPerRequest">5</int>+ * </transformer>+ * </pre>*- * If multiple queries need to be serviced simultaneously then <code>totalPoolThreads</code> and+ * If an individual solr instance is expected to receive no more than one query at a time, it is+ * best to set <code>totalPoolThreads</code> and <code>numThreadsPerRequest</code> to the same+ * value.+ *+ * <p>If multiple queries need to be serviced simultaneously then <code>totalPoolThreads</code> and* <code>numThreadsPerRequest</code> can be adjusted based on the expected response times.*- * If the value of <code>numThreadsPerRequest</code> is higher, the response time for a single query- * will be improved up to a point. If multiple queries are serviced simultaneously, the value of- * <code>totalPoolThreads</code> imposes a contention between the queries if- * <code>(totalPoolThreads < numThreadsPerRequest * total parallel queries)</code>.+ * <p>If the value of <code>numThreadsPerRequest</code> is higher, the response time for a single+ * query will be improved up to a point. If multiple queries are serviced simultaneously, the value+ * of <code>totalPoolThreads</code> imposes a contention between the queries if <code>+ * (totalPoolThreads < numThreadsPerRequest * total parallel queries)</code>.*/-public final class LTRThreadModule implements NamedListInitializedPlugin {+public final class LTRThreadModule implements NamedListInitializedPlugin {public static LTRThreadModule getInstance(NamedList<?> args) {@@ -87,7 +89,7 @@ public final class LTRThreadModule implements NamedListInitializedPlugin {// remove consumed keys only once iteration is complete// since NamedList iterator does not support 'remove'for (Object key : extractedArgs.asShallowMap().keySet()) {- args.remove(CONFIG_PREFIX+key);+ args.remove(CONFIG_PREFIX + key);}return extractedArgs;@@ -101,8 +103,7 @@ public final class LTRThreadModule implements NamedListInitializedPlugin {private Semaphore ltrSemaphore;private volatile ExecutorService createWeightScoreExecutor;- public LTRThreadModule() {- }+ public LTRThreadModule() {}// For test use only.LTRThreadModule(int totalPoolThreads, int numThreadsPerRequest) {@@ -117,7 +118,7 @@ public final class LTRThreadModule implements NamedListInitializedPlugin {SolrPluginUtils.invokeSetters(this, args);}validate();- if (this.totalPoolThreads > 1 ){+ if (this.totalPoolThreads > 1) {ltrSemaphore = new Semaphore(totalPoolThreads);} else {ltrSemaphore = null;@@ -125,14 +126,15 @@ public final class LTRThreadModule implements NamedListInitializedPlugin {}private void validate() {- if (totalPoolThreads <= 0){+ if (totalPoolThreads <= 0) {throw new IllegalArgumentException("totalPoolThreads cannot be less than 1");}- if (numThreadsPerRequest <= 0){+ if (numThreadsPerRequest <= 0) {throw new IllegalArgumentException("numThreadsPerRequest cannot be less than 1");}- if (totalPoolThreads < numThreadsPerRequest){- throw new IllegalArgumentException("numThreadsPerRequest cannot be greater than totalPoolThreads");+ if (totalPoolThreads < numThreadsPerRequest) {+ throw new IllegalArgumentException(+ "numThreadsPerRequest cannot be greater than totalPoolThreads");}}@@ -163,5 +165,4 @@ public final class LTRThreadModule implements NamedListInitializedPlugin {public void setExecutor(ExecutorService sharedExecutor) {this.createWeightScoreExecutor = sharedExecutor;}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/SolrQueryRequestContextUtils.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/SolrQueryRequestContextUtils.javaindex 295597b1bc4..57e790bac68 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/SolrQueryRequestContextUtils.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/SolrQueryRequestContextUtils.java@@ -20,23 +20,22 @@ import org.apache.solr.request.SolrQueryRequest;public class SolrQueryRequestContextUtils {- /** key prefix to reduce possibility of clash with other code's key choices **/+ /** key prefix to reduce possibility of clash with other code's key choices */private static final String LTR_PREFIX = "ltr.";- /** key of the feature logger in the request context **/+ /** key of the feature logger in the request context */private static final String FEATURE_LOGGER = LTR_PREFIX + "feature_logger";- /** key of the scoring queries in the request context **/+ /** key of the scoring queries in the request context */private static final String SCORING_QUERIES = LTR_PREFIX + "scoring_queries";- /** key of the isExtractingFeatures flag in the request context **/+ /** key of the isExtractingFeatures flag in the request context */private static final String IS_EXTRACTING_FEATURES = LTR_PREFIX + "isExtractingFeatures";- /** key of the feature vector store name in the request context **/+ /** key of the feature vector store name in the request context */private static final String STORE = LTR_PREFIX + "store";- /** feature logger accessors **/-+ /** feature logger accessors */public static void setFeatureLogger(SolrQueryRequest req, FeatureLogger featureLogger) {req.getContext().put(FEATURE_LOGGER, featureLogger);}@@ -45,8 +44,7 @@ public class SolrQueryRequestContextUtils {return (FeatureLogger) req.getContext().get(FEATURE_LOGGER);}- /** scoring query accessors **/-+ /** scoring query accessors */public static void setScoringQueries(SolrQueryRequest req, LTRScoringQuery[] scoringQueries) {req.getContext().put(SCORING_QUERIES, scoringQueries);}@@ -55,8 +53,7 @@ public class SolrQueryRequestContextUtils {return (LTRScoringQuery[]) req.getContext().get(SCORING_QUERIES);}- /** isExtractingFeatures flag accessors **/-+ /** isExtractingFeatures flag accessors */public static void setIsExtractingFeatures(SolrQueryRequest req) {req.getContext().put(IS_EXTRACTING_FEATURES, Boolean.TRUE);}@@ -69,8 +66,7 @@ public class SolrQueryRequestContextUtils {return Boolean.TRUE.equals(req.getContext().get(IS_EXTRACTING_FEATURES));}- /** feature vector store name accessors **/-+ /** feature vector store name accessors */public static void setFvStoreName(SolrQueryRequest req, String fvStoreName) {req.getContext().put(STORE, fvStoreName);}@@ -78,6 +74,4 @@ public class SolrQueryRequestContextUtils {public static String getFvStoreName(SolrQueryRequest req) {return (String) req.getContext().get(STORE);}-}-diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/Feature.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/Feature.javaindex df6b60c1a82..da6b1ad8035 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/Feature.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/Feature.java@@ -19,7 +19,6 @@ package org.apache.solr.ltr.feature;import java.io.IOException;import java.util.LinkedHashMap;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.search.Explanation;@@ -37,45 +36,52 @@ import org.apache.solr.request.macro.MacroExpander;import org.apache.solr.util.SolrPluginUtils;/**- * A recipe for computing a feature. Subclass this for specialized feature calculations.- * <p>- * A feature consists of+ * A recipe for computing a feature. Subclass this for specialized feature calculations.+ *+ * <p>A feature consists of+ ** <ul>- * <li> a name as the identifier- * <li> parameters to represent the specific feature+ * <li>a name as the identifier+ * <li>parameters to represent the specific feature* </ul>- * <p>- * Example configuration (snippet):- * <pre>{- "class" : "...",- "name" : "myFeature",- "params" : {- ...- }-}</pre>- * <p>- * {@link Feature} is an abstract class and concrete classes should implement- * the {@link #validate()} function, and must implement the {@link #paramsToMap()}- * and createWeight() methods.+ *+ * <p>Example configuration (snippet):+ *+ * <pre>+ * {+ * "class" : "...",+ * "name" : "myFeature",+ * "params" : {+ * ...+ * }+ * }+ * </pre>+ *+ * <p>{@link Feature} is an abstract class and concrete classes should implement the {@link+ * #validate()} function, and must implement the {@link #paramsToMap()} and createWeight() methods.*/public abstract class Feature extends Query implements Accountable {private static final long BASE_RAM_BYTES = RamUsageEstimator.shallowSizeOfInstance(Feature.class);- final protected String name;+ protected final String name;private int index = -1;private float defaultValue = 0.0f;private Object defaultValueObject = null;- final private Map<String,Object> params;-- public static Feature getInstance(SolrResourceLoader solrResourceLoader,- String className, String name, Map<String,Object> params) {- final Feature f = solrResourceLoader.newInstance(- className,- Feature.class,- new String[0], // no sub packages- new Class<?>[] { String.class, Map.class },- new Object[] { name, params });+ private final Map<String, Object> params;++ public static Feature getInstance(+ SolrResourceLoader solrResourceLoader,+ String className,+ String name,+ Map<String, Object> params) {+ final Feature f =+ solrResourceLoader.newInstance(+ className,+ Feature.class,+ new String[0], // no sub packages+ new Class<?>[] {String.class, Map.class},+ new Object[] {name, params});if (params != null) {SolrPluginUtils.invokeSetters(f, params.entrySet());}@@ -83,17 +89,16 @@ public abstract class Feature extends Query implements Accountable {return f;}- public Feature(String name, Map<String,Object> params) {+ public Feature(String name, Map<String, Object> params) {this.name = name;this.params = params;}/**- * As part of creation of a feature instance, this function confirms- * that the feature parameters are valid.+ * As part of creation of a feature instance, this function confirms that the feature parameters+ * are valid.*- * @throws FeatureException- * Feature Exception+ * @throws FeatureException Feature Exception*/protected abstract void validate() throws FeatureException;@@ -102,7 +107,7 @@ public abstract class Feature extends Query implements Accountable {final StringBuilder sb = new StringBuilder(64); // default initialCapacity of 16 won't be enoughsb.append(getClass().getSimpleName());sb.append(" [name=").append(name);- final LinkedHashMap<String,Object> params = paramsToMap();+ final LinkedHashMap<String, Object> params = paramsToMap();if (params != null) {sb.append(", params=").append(params);}@@ -110,17 +115,22 @@ public abstract class Feature extends Query implements Accountable {return sb.toString();}- public abstract FeatureWeight createWeight(IndexSearcher searcher,- boolean needsScores, SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi) throws IOException;+ public abstract FeatureWeight createWeight(+ IndexSearcher searcher,+ boolean needsScores,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException;public float getDefaultValue() {return defaultValue;}- public void setDefaultValue(Object obj){+ public void setDefaultValue(Object obj) {this.defaultValueObject = obj;if (obj instanceof String) {- defaultValue = Float.parseFloat((String)obj);+ defaultValue = Float.parseFloat((String) obj);} else if (obj instanceof Double) {defaultValue = ((Double) obj).floatValue();} else if (obj instanceof Float) {@@ -134,7 +144,6 @@ public abstract class Feature extends Query implements Accountable {}}-@Overridepublic int hashCode() {final int prime = 31;@@ -147,14 +156,14 @@ public abstract class Feature extends Query implements Accountable {@Overridepublic boolean equals(Object o) {- return sameClassAs(o) && equalsTo(getClass().cast(o));+ return sameClassAs(o) && equalsTo(getClass().cast(o));}@Overridepublic long ramBytesUsed() {- return BASE_RAM_BYTES +- RamUsageEstimator.sizeOfObject(name) +- RamUsageEstimator.sizeOfObject(params);+ return BASE_RAM_BYTES+ + RamUsageEstimator.sizeOfObject(name)+ + RamUsageEstimator.sizeOfObject(params);}@Override@@ -183,70 +192,63 @@ public abstract class Feature extends Query implements Accountable {return true;}- /**- * @return the name- */+ /** @return the name */public String getName() {return name;}- /**- * @return the id- */+ /** @return the id */public int getIndex() {return index;}/**- * @param index- * Unique ID for this feature. Similar to feature name, except it can- * be used to directly access the feature in the global list of- * features.+ * @param index Unique ID for this feature. Similar to feature name, except it can be used to+ * directly access the feature in the global list of features.*/public void setIndex(int index) {this.index = index;}- public abstract LinkedHashMap<String,Object> paramsToMap();+ public abstract LinkedHashMap<String, Object> paramsToMap();- protected LinkedHashMap<String,Object> defaultParamsToMap() {- final LinkedHashMap<String,Object> params = new LinkedHashMap<>();+ 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- **/+ /** Weight for a feature */public abstract class FeatureWeight extends Weight {- final protected IndexSearcher searcher;- final protected SolrQueryRequest request;- final protected Map<String,String[]> efi;- final protected MacroExpander macroExpander;- final protected Query originalQuery;+ protected final IndexSearcher searcher;+ protected final SolrQueryRequest request;+ protected final Map<String, String[]> efi;+ protected final MacroExpander macroExpander;+ protected final Query originalQuery;/**- * Initialize a feature without the normalizer from the feature file. This is- * called on initial construction since multiple models share the same- * features, but have different normalizers. A concrete model's feature is- * copied through featForNewModel().+ * Initialize a feature without the normalizer from the feature file. This is called on initial+ * construction since multiple models share the same features, but have different normalizers. A+ * concrete model's feature is copied through featForNewModel().*- * @param q- * Solr query associated with this FeatureWeight- * @param searcher- * Solr searcher available for features if they need them+ * @param q Solr query associated with this FeatureWeight+ * @param searcher Solr searcher available for features if they need them*/- public FeatureWeight(Query q, IndexSearcher searcher,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi) {+ public FeatureWeight(+ Query q,+ IndexSearcher searcher,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi) {super(q);this.searcher = searcher;this.request = request;this.originalQuery = originalQuery;this.efi = efi;- macroExpander = new MacroExpander(efi,true);+ macroExpander = new MacroExpander(efi, true);}public String getName() {@@ -262,8 +264,7 @@ public abstract class Feature extends Query implements Accountable {}@Override- public abstract FeatureScorer scorer(LeafReaderContext context)- throws IOException;+ public abstract FeatureScorer scorer(LeafReaderContext context) throws IOException;@Overridepublic boolean isCacheable(LeafReaderContext ctx) {@@ -271,8 +272,7 @@ public abstract class Feature extends Query implements Accountable {}@Override- public Explanation explain(LeafReaderContext context, int doc)- throws IOException {+ public Explanation explain(LeafReaderContext context, int doc) throws IOException {final FeatureScorer r = scorer(context);float score = getDefaultValue();if (r != null) {@@ -281,34 +281,30 @@ public abstract class Feature extends Query implements Accountable {score = r.score();}return Explanation.match(score, toString());- }else{+ } else {return Explanation.match(score, "The feature has no value");}}/**- * Used in the FeatureWeight's explain. Each feature should implement this- * returning properties of the specific scorer useful for an explain. For- * example "MyCustomClassFeature [name=" + name + "myVariable:" + myVariable +- * "]"; If not provided, a default implementation will return basic feature- * properties, which might not include query time specific values.+ * Used in the FeatureWeight's explain. Each feature should implement this returning properties+ * of the specific scorer useful for an explain. For example "MyCustomClassFeature [name=" ++ * name + "myVariable:" + myVariable + "]"; If not provided, a default implementation will+ * return basic feature properties, which might not include query time specific values.*/@Overridepublic String toString() {return Feature.this.toString();}- /**- * A 'recipe' for computing a feature- */+ /** A 'recipe' for computing a feature */public abstract class FeatureScorer extends Scorer {- final protected String name;+ protected final String name;private DocInfo docInfo;- final protected DocIdSetIterator itr;+ protected final DocIdSetIterator itr;- public FeatureScorer(Feature.FeatureWeight weight,- DocIdSetIterator itr) {+ public FeatureScorer(Feature.FeatureWeight weight, DocIdSetIterator itr) {super(weight);this.itr = itr;name = weight.getName();@@ -318,9 +314,7 @@ public abstract class Feature extends Query implements Accountable {@Overridepublic abstract float score() throws IOException;- /**- * Used to provide context from initial score steps to later reranking steps.- */+ /** Used to provide context from initial score steps to later reranking steps. */public void setDocInfo(DocInfo docInfo) {this.docInfo = docInfo;}@@ -341,12 +335,12 @@ public abstract class Feature extends Query implements Accountable {}/**- * A <code>FeatureScorer</code> that contains a <code>Scorer</code>,- * which it delegates to where appropriate.+ * A <code>FeatureScorer</code> that contains a <code>Scorer</code>, which it delegates to where+ * appropriate.*/public abstract class FilterFeatureScorer extends FeatureScorer {- final protected Scorer in;+ protected final Scorer in;public FilterFeatureScorer(Feature.FeatureWeight weight, Scorer scorer) {super(weight, null);@@ -376,20 +370,17 @@ public abstract class Feature extends Query implements Accountable {public float getMaxScore(int upTo) throws IOException {return in.getMaxScore(upTo);}-}/**- * Default FeatureScorer class that returns the score passed in. Can be used- * as a simple ValueFeature, or to return a default scorer in case an- * underlying feature's scorer is null.+ * Default FeatureScorer class that returns the score passed in. Can be used as a simple+ * ValueFeature, or to return a default scorer in case an underlying feature's scorer is null.*/public class ValueFeatureScorer extends FeatureScorer {float constScore;- public ValueFeatureScorer(FeatureWeight weight, float constScore,- DocIdSetIterator itr) {- super(weight,itr);+ public ValueFeatureScorer(FeatureWeight weight, float constScore, DocIdSetIterator itr) {+ super(weight, itr);this.constScore = constScore;}@@ -403,7 +394,5 @@ public abstract class Feature extends Query implements Accountable {return constScore;}}-}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FeatureException.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FeatureException.javaindex 6c8f82762f0..86c3192d1cc 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FeatureException.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FeatureException.java@@ -27,5 +27,4 @@ public class FeatureException extends RuntimeException {public FeatureException(String message, Exception cause) {super(message, cause);}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldLengthFeature.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldLengthFeature.javaindex 7bbfdb813af..f4a454f8dfb 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldLengthFeature.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldLengthFeature.java@@ -19,7 +19,6 @@ package org.apache.solr.ltr.feature;import java.io.IOException;import java.util.LinkedHashMap;import java.util.Map;-import org.apache.lucene.index.IndexableField;import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.NumericDocValues;@@ -28,20 +27,25 @@ import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.util.SmallFloat;import org.apache.solr.request.SolrQueryRequest;+/**- * This feature returns the length of a field (in terms) for the current document.- * Example configuration:- * <pre>{- "name": "titleLength",- "class": "org.apache.solr.ltr.feature.FieldLengthFeature",- "params": {- "field": "title"- }-}</pre>- * Note: since this feature relies on norms values that are stored in a single byte- * the value of the feature could have a lightly different value.- * (see also {@link org.apache.lucene.search.similarities.ClassicSimilarity})- **/+ * This feature returns the length of a field (in terms) for the current document. Example+ * configuration:+ *+ * <pre>+ * {+ * "name": "titleLength",+ * "class": "org.apache.solr.ltr.feature.FieldLengthFeature",+ * "params": {+ * "field": "title"+ * }+ * }+ * </pre>+ *+ * Note: since this feature relies on norms values that are stored in a single byte the value of the+ * feature could have a lightly different value. (see also {@link+ * org.apache.lucene.search.similarities.ClassicSimilarity})+ */public class FieldLengthFeature extends Feature {private String field;@@ -55,8 +59,8 @@ public class FieldLengthFeature extends Feature {}@Override- public LinkedHashMap<String,Object> paramsToMap() {- final LinkedHashMap<String,Object> params = defaultParamsToMap();+ public LinkedHashMap<String, Object> paramsToMap() {+ final LinkedHashMap<String, Object> params = defaultParamsToMap();params.put("field", field);return params;}@@ -64,13 +68,11 @@ public class FieldLengthFeature extends Feature {@Overrideprotected void validate() throws FeatureException {if (field == null || field.isEmpty()) {- throw new FeatureException(getClass().getSimpleName()+- ": field must be provided");+ throw new FeatureException(getClass().getSimpleName() + ": field must be provided");}}/** Cache of decoded bytes. */-private static final float[] NORM_TABLE = new float[256];static {@@ -80,42 +82,44 @@ public class FieldLengthFeature extends Feature {}}- /**- * Decodes the norm value, assuming it is a single byte.- *- */-+ /** Decodes the norm value, assuming it is a single byte. */private final float decodeNorm(long norm) {return NORM_TABLE[(int) (norm & 0xFF)]; // & 0xFF maps negative bytes to// positive above 127}- public FieldLengthFeature(String name, Map<String,Object> params) {+ public FieldLengthFeature(String name, Map<String, Object> params) {super(name, params);}@Override- public FeatureWeight createWeight(IndexSearcher searcher, boolean needsScores,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi)- throws IOException {+ public FeatureWeight createWeight(+ IndexSearcher searcher,+ boolean needsScores,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException {return new FieldLengthFeatureWeight(searcher, request, originalQuery, efi);}-public class FieldLengthFeatureWeight extends FeatureWeight {- public FieldLengthFeatureWeight(IndexSearcher searcher,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi) {+ public FieldLengthFeatureWeight(+ IndexSearcher searcher,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi) {super(FieldLengthFeature.this, searcher, request, originalQuery, efi);}@Overridepublic FeatureScorer scorer(LeafReaderContext context) throws IOException {NumericDocValues norms = context.reader().getNormValues(field);- if (norms == null){- return new ValueFeatureScorer(this, 0f,- DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));+ if (norms == null) {+ return new ValueFeatureScorer(+ this, 0f, DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));}return new FieldLengthFeatureScorer(this, norms);}@@ -124,8 +128,8 @@ public class FieldLengthFeature extends Feature {NumericDocValues norms = null;- public FieldLengthFeatureScorer(FeatureWeight weight,- NumericDocValues norms) throws IOException {+ public FieldLengthFeatureScorer(FeatureWeight weight, NumericDocValues norms)+ throws IOException {super(weight, norms);this.norms = norms;@@ -133,8 +137,7 @@ public class FieldLengthFeature extends Feature {final IndexableField idxF = searcher.doc(0).getField(field);if (idxF.fieldType().omitNorms()) {throw new IOException(- "FieldLengthFeatures can't be used if omitNorms is enabled (field="- + field + ")");+ "FieldLengthFeatures can't be used if omitNorms is enabled (field=" + field + ")");}}@@ -152,5 +155,4 @@ public class FieldLengthFeature extends Feature {}}}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldValueFeature.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldValueFeature.javaindex 563a01d14df..87c47be73b8 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldValueFeature.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/FieldValueFeature.java@@ -21,7 +21,6 @@ import java.util.Collections;import java.util.LinkedHashMap;import java.util.Map;import java.util.Set;-import org.apache.lucene.document.Document;import org.apache.lucene.index.DocValues;import org.apache.lucene.index.DocValuesType;@@ -41,27 +40,31 @@ import org.apache.solr.schema.SchemaField;import org.apache.solr.search.SolrIndexSearcher;/**- * This feature returns the value of a field in the current document.- * The field must have stored="true" or docValues="true" properties.- * Example configuration:- * <pre>{- "name": "rawHits",- "class": "org.apache.solr.ltr.feature.FieldValueFeature",- "params": {- "field": "hits"- }-}</pre>+ * This feature returns the value of a field in the current document. The field must have+ * stored="true" or docValues="true" properties. Example configuration:+ *+ * <pre>+ * {+ * "name": "rawHits",+ * "class": "org.apache.solr.ltr.feature.FieldValueFeature",+ * "params": {+ * "field": "hits"+ * }+ * }+ * </pre>*- * <p>There are 4 different types of FeatureScorers that a FieldValueFeatureWeight may use.- * The chosen scorer depends on the field attributes.</p>+ * <p>There are 4 different types of FeatureScorers that a FieldValueFeatureWeight may use. The+ * chosen scorer depends on the field attributes.*- * <p>FieldValueFeatureScorer (FVFS): used for stored=true, no matter if docValues=true or docValues=false</p>+ * <p>FieldValueFeatureScorer (FVFS): used for stored=true, no matter if docValues=true or+ * docValues=false+ *+ * <p>NumericDocValuesFVFS: used for stored=false and docValues=true, if docValueType == NUMERIC*- * <p>NumericDocValuesFVFS: used for stored=false and docValues=true, if docValueType == NUMERIC</p>* <p>SortedDocValuesFVFS: used for stored=false and docValues=true, if docValueType == SORTED*- * <p>DefaultValueFVFS: used for stored=false and docValues=true, a fallback scorer that is used on segments- * where no document has a value set in the field of this feature</p>+ * <p>DefaultValueFVFS: used for stored=false and docValues=true, a fallback scorer that is used on+ * segments where no document has a value set in the field of this feature*/public class FieldValueFeature extends Feature {@@ -78,8 +81,8 @@ public class FieldValueFeature extends Feature {}@Override- public LinkedHashMap<String,Object> paramsToMap() {- final LinkedHashMap<String,Object> params = defaultParamsToMap();+ public LinkedHashMap<String, Object> paramsToMap() {+ final LinkedHashMap<String, Object> params = defaultParamsToMap();params.put("field", field);return params;}@@ -87,27 +90,33 @@ public class FieldValueFeature extends Feature {@Overrideprotected void validate() throws FeatureException {if (field == null || field.isEmpty()) {- throw new FeatureException(getClass().getSimpleName()+- ": field must be provided");+ throw new FeatureException(getClass().getSimpleName() + ": field must be provided");}}- public FieldValueFeature(String name, Map<String,Object> params) {+ public FieldValueFeature(String name, Map<String, Object> params) {super(name, params);}@Override- public FeatureWeight createWeight(IndexSearcher searcher, boolean needsScores,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi)- throws IOException {+ public FeatureWeight createWeight(+ IndexSearcher searcher,+ boolean needsScores,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException {return new FieldValueFeatureWeight(searcher, request, originalQuery, efi);}public class FieldValueFeatureWeight extends FeatureWeight {private final SchemaField schemaField;- public FieldValueFeatureWeight(IndexSearcher searcher,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi) {+ public FieldValueFeatureWeight(+ IndexSearcher searcher,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi) {super(FieldValueFeature.this, searcher, request, originalQuery, efi);if (searcher instanceof SolrIndexSearcher) {schemaField = ((SolrIndexSearcher) searcher).getSchema().getFieldOrNull(field);@@ -128,34 +137,38 @@ public class FieldValueFeature extends Feature {if (schemaField != null && !schemaField.stored() && schemaField.hasDocValues()) {final FieldInfo fieldInfo = context.reader().getFieldInfos().fieldInfo(field);- final DocValuesType docValuesType = fieldInfo != null ? fieldInfo.getDocValuesType() : DocValuesType.NONE;+ final DocValuesType docValuesType =+ fieldInfo != null ? fieldInfo.getDocValuesType() : DocValuesType.NONE;if (DocValuesType.NUMERIC.equals(docValuesType)) {- return new NumericDocValuesFieldValueFeatureScorer(this, context,- DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS), schemaField.getType().getNumberType());+ return new NumericDocValuesFieldValueFeatureScorer(+ this,+ context,+ DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS),+ schemaField.getType().getNumberType());} else if (DocValuesType.SORTED.equals(docValuesType)) {- return new SortedDocValuesFieldValueFeatureScorer(this, context,- DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));+ return new SortedDocValuesFieldValueFeatureScorer(+ this, context, DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));} else if (DocValuesType.NONE.equals(docValuesType)) {- // Using a fallback feature scorer because this segment has no documents with a doc value for the current field- return new DefaultValueFieldValueFeatureScorer(this, DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));+ // Using a fallback feature scorer because this segment has no documents with a doc value+ // for the current field+ return new DefaultValueFieldValueFeatureScorer(+ this, DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));}- throw new IllegalArgumentException("Doc values type " + docValuesType.name() + " of field " + field- + " is not supported");+ throw new IllegalArgumentException(+ "Doc values type " + docValuesType.name() + " of field " + field + " is not supported");}- return new FieldValueFeatureScorer(this, context,- DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));+ return new FieldValueFeatureScorer(+ this, context, DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));}- /**- * A FeatureScorer that reads the stored value for a field- */+ /** A FeatureScorer that reads the stored value for a field */public class FieldValueFeatureScorer extends FeatureScorer {LeafReaderContext context = null;- public FieldValueFeatureScorer(FeatureWeight weight,- LeafReaderContext context, DocIdSetIterator itr) {+ public FieldValueFeatureScorer(+ FeatureWeight weight, LeafReaderContext context, DocIdSetIterator itr) {super(weight, itr);this.context = context;}@@ -164,8 +177,7 @@ public class FieldValueFeature extends Feature {public float score() throws IOException {try {- final Document document = context.reader().document(itr.docID(),- fieldAsSet);+ final Document document = context.reader().document(itr.docID(), fieldAsSet);final IndexableField indexableField = document.getField(field);if (indexableField == null) {return getDefaultValue();@@ -189,9 +201,7 @@ public class FieldValueFeature extends Feature {}} catch (final IOException e) {throw new FeatureException(- e.toString() + ": " +- "Unable to extract feature for "- + name, e);+ e.toString() + ": " + "Unable to extract feature for " + name, e);}return getDefaultValue();}@@ -202,15 +212,16 @@ public class FieldValueFeature extends Feature {}}- /**- * A FeatureScorer that reads the numeric docValues for a field- */+ /** A FeatureScorer that reads the numeric docValues for a field */public final class NumericDocValuesFieldValueFeatureScorer extends FeatureScorer {private final NumericDocValues docValues;private final NumberType numberType;- public NumericDocValuesFieldValueFeatureScorer(final FeatureWeight weight, final LeafReaderContext context,- final DocIdSetIterator itr, final NumberType numberType) {+ public NumericDocValuesFieldValueFeatureScorer(+ final FeatureWeight weight,+ final LeafReaderContext context,+ final DocIdSetIterator itr,+ final NumberType numberType) {super(weight, itr);this.numberType = numberType;@@ -255,14 +266,12 @@ public class FieldValueFeature extends Feature {}}- /**- * A FeatureScorer that reads the sorted docValues for a field- */+ /** A FeatureScorer that reads the sorted docValues for a field */public final class SortedDocValuesFieldValueFeatureScorer extends FeatureScorer {private final SortedDocValues docValues;- public SortedDocValuesFieldValueFeatureScorer(final FeatureWeight weight, final LeafReaderContext context,- final DocIdSetIterator itr) {+ public SortedDocValuesFieldValueFeatureScorer(+ final FeatureWeight weight, final LeafReaderContext context, final DocIdSetIterator itr) {super(weight, itr);SortedDocValues docValues;@@ -314,12 +323,14 @@ public class FieldValueFeature extends Feature {/*** A FeatureScorer that always returns the default value.*- * It is used as a fallback for cases when a segment does not have any documents that contain doc values for a field.- * By doing so, we prevent a fallback to the FieldValueFeatureScorer, which would also return the default value but- * in a less performant way because it would first try to read the stored fields for the doc (which aren't present).+ * <p>It is used as a fallback for cases when a segment does not have any documents that contain+ * doc values for a field. By doing so, we prevent a fallback to the FieldValueFeatureScorer,+ * which would also return the default value but in a less performant way because it would first+ * try to read the stored fields for the doc (which aren't present).*/public final class DefaultValueFieldValueFeatureScorer extends FeatureScorer {- public DefaultValueFieldValueFeatureScorer(final FeatureWeight weight, final DocIdSetIterator itr) {+ public DefaultValueFieldValueFeatureScorer(+ final FeatureWeight weight, final DocIdSetIterator itr) {super(weight, itr);}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/OriginalScoreFeature.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/OriginalScoreFeature.javaindex 68ceca73a06..a74e930641b 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/OriginalScoreFeature.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/OriginalScoreFeature.java@@ -19,7 +19,6 @@ package org.apache.solr.ltr.feature;import java.io.IOException;import java.util.LinkedHashMap;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;@@ -28,48 +27,57 @@ import org.apache.lucene.search.Scorer;import org.apache.lucene.search.Weight;import org.apache.solr.ltr.DocInfo;import org.apache.solr.request.SolrQueryRequest;+/**- * This feature returns the original score that the document had before performing- * the reranking.+ * This feature returns the original score that the document had before performing the reranking.* Example configuration:- * <pre>{- "name": "originalScore",- "class": "org.apache.solr.ltr.feature.OriginalScoreFeature",- "params": { }-}</pre>- **/+ *+ * <pre>+ * {+ * "name": "originalScore",+ * "class": "org.apache.solr.ltr.feature.OriginalScoreFeature",+ * "params": { }+ * }+ * </pre>+ */public class OriginalScoreFeature extends Feature {- public OriginalScoreFeature(String name, Map<String,Object> params) {+ public OriginalScoreFeature(String name, Map<String, Object> params) {super(name, params);}@Override- public LinkedHashMap<String,Object> paramsToMap() {+ public LinkedHashMap<String, Object> paramsToMap() {return defaultParamsToMap();}@Override- protected void validate() throws FeatureException {- }+ protected void validate() throws FeatureException {}@Override- public OriginalScoreWeight createWeight(IndexSearcher searcher,- boolean needsScores, SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi) throws IOException {+ public OriginalScoreWeight createWeight(+ IndexSearcher searcher,+ boolean needsScores,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException {return new OriginalScoreWeight(searcher, request, originalQuery, efi);-}public class OriginalScoreWeight extends FeatureWeight {final Weight w;- public OriginalScoreWeight(IndexSearcher searcher,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi) throws IOException {+ public OriginalScoreWeight(+ IndexSearcher searcher,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException {super(OriginalScoreFeature.this, searcher, request, originalQuery, efi);w = searcher.createWeight(searcher.rewrite(originalQuery), ScoreMode.COMPLETE, 1);- };-+ }@Overridepublic String toString() {@@ -95,11 +103,10 @@ public class OriginalScoreFeature extends Feature {// was already scored in step 1// we shouldn't need to calc original score again.final DocInfo docInfo = getDocInfo();- return (docInfo != null && docInfo.hasOriginalDocScore() ? docInfo.getOriginalDocScore() : in.score());+ return (docInfo != null && docInfo.hasOriginalDocScore()+ ? docInfo.getOriginalDocScore()+ : in.score());}-}-}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/SolrFeature.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/SolrFeature.javaindex 83b8b115bb7..145e757c7d5 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/SolrFeature.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/SolrFeature.java@@ -21,7 +21,6 @@ import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.MatchNoDocsQuery;@@ -41,22 +40,28 @@ import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.search.SyntaxError;/**- * This feature allows you to reuse any Solr query as a feature. The value- * of the feature will be the score of the given query for the current document.- * See <a href="https://solr.apache.org/guide/other-parsers.html">Solr documentation of other parsers</a> you can use as a feature.- * Example configurations:- * <pre>[{ "name": "isBook",- "class": "org.apache.solr.ltr.feature.SolrFeature",- "params":{ "fq": ["{!terms f=category}book"] }-},-{- "name": "documentRecency",- "class": "org.apache.solr.ltr.feature.SolrFeature",- "params": {- "q": "{!func}recip( ms(NOW,publish_date), 3.16e-11, 1, 1)"- }-}]</pre>- **/+ * This feature allows you to reuse any Solr query as a feature. The value of the feature will be+ * the score of the given query for the current document. See <a+ * href="https://solr.apache.org/solr/guide/other-parsers.html">Solr documentation of other+ * parsers</a> you can use as a feature. Example configurations:+ *+ * <pre>+ * [+ * {+ * "name": "isBook",+ * "class": "org.apache.solr.ltr.feature.SolrFeature",+ * "params":{ "fq": ["{!terms f=category}book"] }+ * },+ * {+ * "name": "documentRecency",+ * "class": "org.apache.solr.ltr.feature.SolrFeature",+ * "params": {+ * "q": "{!func}recip( ms(NOW,publish_date), 3.16e-11, 1, 1)"+ * }+ * }+ * ]+ * </pre>+ */public class SolrFeature extends Feature {private String df;@@ -89,13 +94,13 @@ public class SolrFeature extends Feature {this.fq = fq;}- public SolrFeature(String name, Map<String,Object> params) {+ public SolrFeature(String name, Map<String, Object> params) {super(name, params);}@Override- public LinkedHashMap<String,Object> paramsToMap() {- final LinkedHashMap<String,Object> params = defaultParamsToMap();+ public LinkedHashMap<String, Object> paramsToMap() {+ final LinkedHashMap<String, Object> params = defaultParamsToMap();if (df != null) {params.put("df", df);}@@ -109,29 +114,33 @@ public class SolrFeature extends Feature {}@Override- public FeatureWeight createWeight(IndexSearcher searcher, boolean needsScores,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi)- throws IOException {+ public FeatureWeight createWeight(+ IndexSearcher searcher,+ boolean needsScores,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException {return new SolrFeatureWeight((SolrIndexSearcher) searcher, request, originalQuery, efi);}@Overrideprotected void validate() throws FeatureException {- if ((q == null || q.isEmpty()) &&- ((fq == null) || fq.isEmpty())) {- throw new FeatureException(getClass().getSimpleName()+- ": Q or FQ must be provided");+ if ((q == null || q.isEmpty()) && ((fq == null) || fq.isEmpty())) {+ throw new FeatureException(getClass().getSimpleName() + ": Q or FQ must be provided");}}- /**- * Weight for a SolrFeature- **/+ /** Weight for a SolrFeature */public class SolrFeatureWeight extends FeatureWeight {private final Weight solrQueryWeight;- public SolrFeatureWeight(SolrIndexSearcher searcher,- SolrQueryRequest request, Query originalQuery, Map<String, String[]> efi) throws IOException {+ public SolrFeatureWeight(+ SolrIndexSearcher searcher,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException {super(SolrFeature.this, searcher, request, originalQuery, efi);try {final SolrQueryRequest req = makeRequest(request.getCore(), q, fq, df);@@ -147,10 +156,13 @@ public class SolrFeature extends Feature {} else {qStr = macroExpander.expand(qStr);if (qStr == null) {- throw new FeatureException(this.getClass().getSimpleName() + " requires efi parameter that was not passed in request.");+ throw new FeatureException(+ this.getClass().getSimpleName()+ + " requires efi parameter that was not passed in request.");}scoreQuery = QParser.getParser(qStr, req).getQuery();- // note: QParser can return a null Query sometimes, such as if the query is a stopword or just symbols+ // note: QParser can return a null Query sometimes, such as if the query is a stopword or+ // just symbolsif (scoreQuery == null) {scoreQuery = new MatchNoDocsQuery(); // debatable; all or none?}@@ -159,12 +171,15 @@ public class SolrFeature extends Feature {// Build the filter queriesQuery filterDocSetQuery = null;if (fq != null) {- List<Query> filterQueries = new ArrayList<>(); // If there are no fqs we just want an empty list+ List<Query> filterQueries =+ new ArrayList<>(); // If there are no fqs we just want an empty listfor (String fqStr : fq) {if (fqStr != null) {fqStr = macroExpander.expand(fqStr);if (fqStr == null) {- throw new FeatureException(this.getClass().getSimpleName() + " requires efi parameter that was not passed in request.");+ throw new FeatureException(+ this.getClass().getSimpleName()+ + " requires efi parameter that was not passed in request.");}final Query filterQuery = QParser.getParser(fqStr, req).getQuery();if (filterQuery != null) {@@ -188,8 +203,8 @@ public class SolrFeature extends Feature {}}- private LocalSolrQueryRequest makeRequest(SolrCore core, String solrQuery,- List<String> fqs, String df) {+ private LocalSolrQueryRequest makeRequest(+ SolrCore core, String solrQuery, List<String> fqs, String df) {final NamedList<String> returnList = new NamedList<String>();if ((solrQuery != null) && !solrQuery.isEmpty()) {returnList.add(CommonParams.Q, solrQuery);@@ -218,9 +233,7 @@ public class SolrFeature extends Feature {return new SolrFeatureScorer(this, solrScorer);}- /**- * Scorer for a SolrFeature- */+ /** Scorer for a SolrFeature */public class SolrFeatureScorer extends FilterFeatureScorer {public SolrFeatureScorer(FeatureWeight weight, Scorer solrScorer) {@@ -233,12 +246,9 @@ public class SolrFeature extends Feature {return in.score();} catch (UnsupportedOperationException e) {throw new FeatureException(- e.toString() + ": " +- "Unable to extract feature for "- + name, e);+ e.toString() + ": " + "Unable to extract feature for " + name, e);}}-}}-}\ No newline at end of file+}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/ValueFeature.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/ValueFeature.javaindex 80e7d8cb57a..2c13262c84d 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/ValueFeature.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/ValueFeature.java@@ -19,31 +19,33 @@ package org.apache.solr.ltr.feature;import java.io.IOException;import java.util.LinkedHashMap;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.solr.request.SolrQueryRequest;+/*** This feature allows to return a constant given value for the current document.*- * Example configuration:- * <pre>{- "name" : "userFromMobile",- "class" : "org.apache.solr.ltr.feature.ValueFeature",- "params" : { "value" : "${userFromMobile}", "required":true }- }</pre>+ * <p>Example configuration:*- *You can place a constant value like "1.3f" in the value params, but many times you- *would want to pass in external information to use per request. For instance, maybe- *you want to rank things differently if the search came from a mobile device, or maybe- *you want to use your external query intent system as a feature.- *In the rerank request you can pass in rq={... efi.userFromMobile=1}, and the above- *feature will return 1 for all the docs for that request. If required is set to true,- *the request will return an error since you failed to pass in the efi, otherwise if will- *just skip the feature and use a default value of 0 instead.- **/+ * <pre>+ * {+ * "name" : "userFromMobile",+ * "class" : "org.apache.solr.ltr.feature.ValueFeature",+ * "params" : { "value" : "${userFromMobile}", "required":true }+ * }+ * </pre>+ *+ * You can place a constant value like "1.3f" in the value params, but many times you would want to+ * pass in external information to use per request. For instance, maybe you want to rank things+ * differently if the search came from a mobile device, or maybe you want to use your external query+ * intent system as a feature. In the rerank request you can pass in rq={... efi.userFromMobile=1},+ * and the above feature will return 1 for all the docs for that request. If required is set to+ * true, the request will return an error since you failed to pass in the efi, otherwise if will+ * just skip the feature and use a default value of 0 instead.+ */public class ValueFeature extends Feature {private float configValue = -1f;private String configValueStr = null;@@ -81,8 +83,8 @@ public class ValueFeature extends Feature {}@Override- public LinkedHashMap<String,Object> paramsToMap() {- final LinkedHashMap<String,Object> params = defaultParamsToMap();+ public LinkedHashMap<String, Object> paramsToMap() {+ final LinkedHashMap<String, Object> params = defaultParamsToMap();params.put("value", value);if (required != null) {params.put("required", required);@@ -97,32 +99,41 @@ public class ValueFeature extends Feature {}}- public ValueFeature(String name, Map<String,Object> params) {+ public ValueFeature(String name, Map<String, Object> params) {super(name, params);}@Override- public FeatureWeight createWeight(IndexSearcher searcher, boolean needsScores,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi)- throws IOException {+ public FeatureWeight createWeight(+ IndexSearcher searcher,+ boolean needsScores,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi)+ throws IOException {return new ValueFeatureWeight(searcher, request, originalQuery, efi);}public class ValueFeatureWeight extends FeatureWeight {- final protected Float featureValue;+ protected final Float featureValue;- public ValueFeatureWeight(IndexSearcher searcher,- SolrQueryRequest request, Query originalQuery, Map<String,String[]> efi) {+ public ValueFeatureWeight(+ IndexSearcher searcher,+ SolrQueryRequest request,+ Query originalQuery,+ Map<String, String[]> efi) {super(ValueFeature.this, searcher, request, originalQuery, efi);if (configValueStr != null) {final String expandedValue = macroExpander.expand(configValueStr);if (expandedValue != null) {featureValue = Float.parseFloat(expandedValue);} else if (isRequired()) {- throw new FeatureException(this.getClass().getSimpleName() + " requires efi parameter that was not passed in request.");+ throw new FeatureException(+ this.getClass().getSimpleName()+ + " requires efi parameter that was not passed in request.");} else {- featureValue=null;+ featureValue = null;}} else {featureValue = configValue;@@ -131,15 +142,12 @@ public class ValueFeature extends Feature {@Overridepublic FeatureScorer scorer(LeafReaderContext context) throws IOException {- if(featureValue!=null) {- return new ValueFeatureScorer(this, featureValue,- DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));+ if (featureValue != null) {+ return new ValueFeatureScorer(+ this, featureValue, DocIdSetIterator.all(DocIdSetIterator.NO_MORE_DOCS));} else {return null;}}--}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/package-info.javaindex 456fffcffa3..052eae3808c 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/feature/package-info.java@@ -15,7 +15,5 @@* limitations under the License.*/-/**- * Contains Feature related classes- */+/** Contains Feature related classes */package org.apache.solr.ltr.feature;diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/Interleaving.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/Interleaving.javaindex db044439ce0..bb5992c973b 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/Interleaving.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/Interleaving.java@@ -22,25 +22,27 @@ import org.apache.solr.common.SolrException;import org.apache.solr.ltr.interleaving.algorithms.TeamDraftInterleaving;/**- * Interleaving considers two ranking models: modelA and modelB.- * For a given query, each model returns its ranked list of documents La = (a1,a2,...) and Lb = (b1, b2, ...).- * An Interleaving algorithm creates a unique ranked list I = (i1, i2, ...).- * This list is created by interleaving elements from the two lists la and lb as described by the implementation algorithm.+ * Interleaving considers two ranking models: modelA and modelB.<br>+ * For a given query, each model returns its ranked list of documents La = (a1,a2,...) and Lb = (b1,+ * b2, ...).<br>+ * An Interleaving algorithm creates a unique ranked list I = (i1, i2, ...).<br>+ * This list is created by interleaving elements from the two lists la and lb as described by the+ * implementation algorithm.<br>* Each element Ij is labelled TeamA if it is selected from La and TeamB if it is selected from Lb.*/public interface Interleaving {- String TEAM_DRAFT = "TeamDraft";+ String TEAM_DRAFT = "TeamDraft";- InterleavingResult interleave(ScoreDoc[] rerankedA, ScoreDoc[] rerankedB);+ InterleavingResult interleave(ScoreDoc[] rerankedA, ScoreDoc[] rerankedB);- static Interleaving getImplementation(String algorithm) {- switch(algorithm) {- case TEAM_DRAFT:- return new TeamDraftInterleaving();- default:- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "Unknown Interleaving algorithm: " + algorithm);- }- }-}\ No newline at end of file+ static Interleaving getImplementation(String algorithm) {+ switch (algorithm) {+ case TEAM_DRAFT:+ return new TeamDraftInterleaving();+ default:+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST, "Unknown Interleaving algorithm: " + algorithm);+ }+ }+}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/InterleavingResult.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/InterleavingResult.javaindex aeaac2997a6..7cc45b0f44a 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/InterleavingResult.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/InterleavingResult.java@@ -19,14 +19,14 @@ package org.apache.solr.ltr.interleaving;import java.util.ArrayList;import java.util.Set;-import org.apache.lucene.search.ScoreDoc;public class InterleavingResult {- final private ScoreDoc[] interleavedResults;- final private ArrayList<Set<Integer>> interleavingPicks;+ private final ScoreDoc[] interleavedResults;+ private final ArrayList<Set<Integer>> interleavingPicks;- public InterleavingResult(ScoreDoc[] interleavedResults, ArrayList<Set<Integer>> interleavingPicks) {+ public InterleavingResult(+ ScoreDoc[] interleavedResults, ArrayList<Set<Integer>> interleavingPicks) {this.interleavedResults = interleavedResults;this.interleavingPicks = interleavingPicks;}@@ -38,5 +38,4 @@ public class InterleavingResult {public ArrayList<Set<Integer>> getInterleavingPicks() {return interleavingPicks;}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingQuery.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingQuery.javaindex 7c00195ceb6..f34ee1663b7 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingQuery.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingQuery.java@@ -19,20 +19,22 @@ package org.apache.solr.ltr.interleaving;import java.io.IOException;import java.util.Arrays;-import org.apache.lucene.search.Query;import org.apache.solr.ltr.search.LTRQuery;import org.apache.solr.search.RankQuery;/**- * A learning to rank Query with Interleaving, will incapsulate two models, and delegate to it the rescoring- * of the documents.- **/+ * A learning to rank Query with Interleaving, will incapsulate two models, and delegate to it the+ * rescoring of the documents.+ */public class LTRInterleavingQuery extends LTRQuery {private final LTRInterleavingScoringQuery[] rerankingQueries;private final Interleaving interlavingAlgorithm;- public LTRInterleavingQuery(Interleaving interleavingAlgorithm, LTRInterleavingScoringQuery[] rerankingQueries, int rerankDocs) {+ public LTRInterleavingQuery(+ Interleaving interleavingAlgorithm,+ LTRInterleavingScoringQuery[] rerankingQueries,+ int rerankDocs) {super(null, rerankDocs, new LTRInterleavingRescorer(interleavingAlgorithm, rerankingQueries));this.rerankingQueries = rerankingQueries;this.interlavingAlgorithm = interleavingAlgorithm;@@ -50,13 +52,14 @@ public class LTRInterleavingQuery extends LTRQuery {private boolean equalsTo(LTRInterleavingQuery other) {return (mainQuery.equals(other.mainQuery)- && rerankingQueries.equals(other.rerankingQueries) && (reRankDocs == other.reRankDocs));+ && rerankingQueries.equals(other.rerankingQueries)+ && (reRankDocs == other.reRankDocs));}@Overridepublic RankQuery wrap(Query _mainQuery) {super.wrap(_mainQuery);- for(LTRInterleavingScoringQuery rerankingQuery: rerankingQueries){+ for (LTRInterleavingScoringQuery rerankingQuery : rerankingQueries) {rerankingQuery.setOriginalQuery(_mainQuery);}return this;@@ -64,12 +67,18 @@ public class LTRInterleavingQuery extends LTRQuery {@Overridepublic String toString(String field) {- return "{!ltr mainQuery='" + mainQuery.toString() + "' rerankingQueries='"- + Arrays.toString(rerankingQueries) + "' reRankDocs=" + reRankDocs + "}";+ return "{!ltr mainQuery='"+ + mainQuery.toString()+ + "' rerankingQueries='"+ + Arrays.toString(rerankingQueries)+ + "' reRankDocs="+ + reRankDocs+ + "}";}@Overrideprotected Query rewrite(Query rewrittenMainQuery) throws IOException {- return new LTRInterleavingQuery(interlavingAlgorithm, rerankingQueries, reRankDocs).wrap(rewrittenMainQuery);+ return new LTRInterleavingQuery(interlavingAlgorithm, rerankingQueries, reRankDocs)+ .wrap(rewrittenMainQuery);}}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingRescorer.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingRescorer.javaindex 799f4d9e36a..f0c90558eeb 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingRescorer.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingRescorer.java@@ -21,7 +21,6 @@ import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Set;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.Explanation;import org.apache.lucene.search.IndexSearcher;@@ -32,22 +31,22 @@ import org.apache.solr.ltr.LTRRescorer;import org.apache.solr.ltr.LTRScoringQuery;/**- * Implements the rescoring logic. The top documents returned by solr with their- * original scores, will be processed by a {@link LTRScoringQuery} that will assign a- * new score to each document. The top documents will be resorted based on the- * new score.- * */+ * Implements the rescoring logic. The top documents returned by solr with their original scores,+ * will be processed by a {@link LTRScoringQuery} that will assign a new score to each document. The+ * top documents will be resorted based on the new score.+ */public class LTRInterleavingRescorer extends LTRRescorer {-- final private LTRInterleavingScoringQuery[] rerankingQueries;++ private final LTRInterleavingScoringQuery[] rerankingQueries;private Integer originalRankingIndex = null;- final private Interleaving interleavingAlgorithm;-- public LTRInterleavingRescorer( Interleaving interleavingAlgorithm, LTRInterleavingScoringQuery[] rerankingQueries) {+ private final Interleaving interleavingAlgorithm;++ public LTRInterleavingRescorer(+ Interleaving interleavingAlgorithm, LTRInterleavingScoringQuery[] rerankingQueries) {this.rerankingQueries = rerankingQueries;this.interleavingAlgorithm = interleavingAlgorithm;- for(int i=0;i<this.rerankingQueries.length;i++){- if(this.rerankingQueries[i] instanceof OriginalRankingLTRScoringQuery){+ for (int i = 0; i < this.rerankingQueries.length; i++) {+ if (this.rerankingQueries[i] instanceof OriginalRankingLTRScoringQuery) {this.originalRankingIndex = i;}}@@ -56,34 +55,34 @@ public class LTRInterleavingRescorer extends LTRRescorer {/*** rescores the documents:*- * @param searcher- * current IndexSearcher- * @param firstPassTopDocs- * documents to rerank;- * @param topN- * documents to return;+ * @param searcher current IndexSearcher+ * @param firstPassTopDocs documents to rerank;+ * @param topN documents to return;*/@Override- public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs,- int topN) throws IOException {+ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int topN)+ throws IOException {if ((topN == 0) || (firstPassTopDocs.scoreDocs.length == 0)) {return firstPassTopDocs;}ScoreDoc[] firstPassResults = null;- if(originalRankingIndex != null) {+ if (originalRankingIndex != null) {firstPassResults = new ScoreDoc[firstPassTopDocs.scoreDocs.length];- System.arraycopy(firstPassTopDocs.scoreDocs, 0, firstPassResults, 0, firstPassTopDocs.scoreDocs.length);+ System.arraycopy(+ firstPassTopDocs.scoreDocs, 0, firstPassResults, 0, firstPassTopDocs.scoreDocs.length);}topN = Math.toIntExact(Math.min(topN, firstPassTopDocs.totalHits.value));- ScoreDoc[][] reRankedPerModel = rerank(searcher,topN,getFirstPassDocsRanked(firstPassTopDocs));+ ScoreDoc[][] reRankedPerModel =+ rerank(searcher, topN, getFirstPassDocsRanked(firstPassTopDocs));if (originalRankingIndex != null) {reRankedPerModel[originalRankingIndex] = firstPassResults;}- InterleavingResult interleaved = interleavingAlgorithm.interleave(reRankedPerModel[0], reRankedPerModel[1]);+ InterleavingResult interleaved =+ interleavingAlgorithm.interleave(reRankedPerModel[0], reRankedPerModel[1]);ScoreDoc[] interleavedResults = interleaved.getInterleavedResults();-+ArrayList<Set<Integer>> interleavingPicks = interleaved.getInterleavingPicks();rerankingQueries[0].setPickedInterleavingDocIds(interleavingPicks.get(0));rerankingQueries[1].setPickedInterleavingDocIds(interleavingPicks.get(1));@@ -91,14 +90,17 @@ public class LTRInterleavingRescorer extends LTRRescorer {return new TopDocs(firstPassTopDocs.totalHits, interleavedResults);}- private ScoreDoc[][] rerank(IndexSearcher searcher, int topN, ScoreDoc[] firstPassResults) throws IOException {+ private ScoreDoc[][] rerank(IndexSearcher searcher, int topN, ScoreDoc[] firstPassResults)+ throws IOException {ScoreDoc[][] reRankedPerModel = new ScoreDoc[rerankingQueries.length][topN];final List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();- LTRScoringQuery.ModelWeight[] modelWeights = new LTRScoringQuery.ModelWeight[rerankingQueries.length];+ LTRScoringQuery.ModelWeight[] modelWeights =+ new LTRScoringQuery.ModelWeight[rerankingQueries.length];for (int i = 0; i < rerankingQueries.length; i++) {if (originalRankingIndex == null || originalRankingIndex != i) {- modelWeights[i] = (LTRScoringQuery.ModelWeight) searcher- .createWeight(searcher.rewrite(rerankingQueries[i]), ScoreMode.COMPLETE, 1);+ modelWeights[i] =+ (LTRScoringQuery.ModelWeight)+ searcher.createWeight(searcher.rewrite(rerankingQueries[i]), ScoreMode.COMPLETE, 1);}}scoreFeatures(searcher, topN, modelWeights, firstPassResults, leaves, reRankedPerModel);@@ -112,15 +114,21 @@ public class LTRInterleavingRescorer extends LTRRescorer {return reRankedPerModel;}- public void scoreFeatures(IndexSearcher indexSearcher,- int topN, LTRScoringQuery.ModelWeight[] modelWeights, ScoreDoc[] hits, List<LeafReaderContext> leaves,- ScoreDoc[][] rerankedPerModel) throws IOException {+ public void scoreFeatures(+ IndexSearcher indexSearcher,+ int topN,+ LTRScoringQuery.ModelWeight[] modelWeights,+ ScoreDoc[] hits,+ List<LeafReaderContext> leaves,+ ScoreDoc[][] rerankedPerModel)+ throws IOException {int readerUpto = -1;int endDoc = 0;int docBase = 0;int hitUpto = 0;- LTRScoringQuery.ModelWeight.ModelScorer[] scorers = new LTRScoringQuery.ModelWeight.ModelScorer[rerankingQueries.length];+ LTRScoringQuery.ModelWeight.ModelScorer[] scorers =+ new LTRScoringQuery.ModelWeight.ModelScorer[rerankingQueries.length];while (hitUpto < hits.length) {final ScoreDoc hit = hits[hitUpto];final int docID = hit.doc;@@ -143,24 +151,23 @@ public class LTRInterleavingRescorer extends LTRRescorer {for (int i = 0; i < rerankingQueries.length; i++) {if (modelWeights[i] != null) {final ScoreDoc hit_i = new ScoreDoc(hit.doc, hit.score, hit.shardIndex);- if (scoreSingleHit(topN, docBase, hitUpto, hit_i, docID, scorers[i], rerankedPerModel[i])) {+ if (scoreSingleHit(+ topN, docBase, hitUpto, hit_i, docID, scorers[i], rerankedPerModel[i])) {logSingleHit(indexSearcher, modelWeights[i], hit_i.doc, rerankingQueries[i]);}}}hitUpto++;}-}-+@Override- public Explanation explain(IndexSearcher searcher,- Explanation firstPassExplanation, int docID) throws IOException {+ public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)+ throws IOException {LTRScoringQuery pickedRerankModel = rerankingQueries[0];if (rerankingQueries[1].getPickedInterleavingDocIds().contains(docID)) {pickedRerankModel = rerankingQueries[1];}return getExplanation(searcher, docID, pickedRerankModel);}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingScoringQuery.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingScoringQuery.javaindex cb2da2c45d1..be1559299de 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingScoringQuery.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/LTRInterleavingScoringQuery.java@@ -18,7 +18,6 @@ package org.apache.solr.ltr.interleaving;import java.util.Map;import java.util.Set;-import org.apache.solr.ltr.LTRScoringQuery;import org.apache.solr.ltr.LTRThreadModule;import org.apache.solr.ltr.model.LTRScoringModel;@@ -36,9 +35,11 @@ public class LTRInterleavingScoringQuery extends LTRScoringQuery {super(ltrScoringModel, extractAllFeatures);}- public LTRInterleavingScoringQuery(LTRScoringModel ltrScoringModel,+ public LTRInterleavingScoringQuery(+ LTRScoringModel ltrScoringModel,Map<String, String[]> externalFeatureInfo,- boolean extractAllFeatures, LTRThreadModule ltrThreadMgr) {+ boolean extractAllFeatures,+ LTRThreadModule ltrThreadMgr) {super(ltrScoringModel, externalFeatureInfo, extractAllFeatures, ltrThreadMgr);}@@ -49,5 +50,4 @@ public class LTRInterleavingScoringQuery extends LTRScoringQuery {public void setPickedInterleavingDocIds(Set<Integer> pickedInterleavingDocIds) {this.pickedInterleavingDocIds = pickedInterleavingDocIds;}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/OriginalRankingLTRScoringQuery.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/OriginalRankingLTRScoringQuery.javaindex 9c4db056e8b..aa7f8d5d222 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/OriginalRankingLTRScoringQuery.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/OriginalRankingLTRScoringQuery.java@@ -29,5 +29,4 @@ public final class OriginalRankingLTRScoringQuery extends LTRInterleavingScoringpublic String getScoringModelName() {return this.originalRankingModelName;}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/TeamDraftInterleaving.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/TeamDraftInterleaving.javaindex a9cd38ad8db..eab416087db 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/TeamDraftInterleaving.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/TeamDraftInterleaving.java@@ -22,32 +22,34 @@ import java.util.HashSet;import java.util.LinkedList;import java.util.Random;import java.util.Set;-import org.apache.lucene.search.ScoreDoc;import org.apache.solr.ltr.interleaving.Interleaving;import org.apache.solr.ltr.interleaving.InterleavingResult;/**- * Interleaving was introduced the first time by Joachims in [1, 2].- * Team Draft Interleaving is among the most successful and used interleaving approaches[3].- * Team Draft Interleaving implements a method similar to the way in which captains select their players in team-matches.- * Team Draft Interleaving produces a fair distribution of ranking models’ elements in the final interleaved list.- * "Team draft interleaving" has also proved to overcome an issue of the "Balanced interleaving" approach, in determining the winning model[4].- * <p>- * [1] T. Joachims. Optimizing search engines using clickthrough data. KDD (2002)- * [2] T.Joachims.Evaluatingretrievalperformanceusingclickthroughdata.InJ.Franke, G. Nakhaeizadeh, and I. Renz, editors,- * Text Mining, pages 79–96. Physica/Springer (2003)- * [3] F. Radlinski, M. Kurup, and T. Joachims. How does clickthrough data reflect re-- * trieval quality? In CIKM, pages 43–52. ACM Press (2008)- * [4] O. Chapelle, T. Joachims, F. Radlinski, and Y. Yue.- * Large-scale validation and analysis of interleaved search evaluation. ACM TOIS, 30(1):1–41, Feb. (2012)+ * Interleaving was introduced the first time by Joachims in [1, 2].<br>+ * Team Draft Interleaving is among the most successful and used interleaving approaches[3].<br>+ * Team Draft Interleaving implements a method similar to the way in which captains select their+ * players in team-matches.<br>+ * Team Draft Interleaving produces a fair distribution of ranking models’ elements in the final+ * interleaved list.<br>+ * "Team draft interleaving" has also proved to overcome an issue of the "Balanced interleaving"+ * approach, in determining the winning model[4].+ *+ * <p>[1] T. Joachims. Optimizing search engines using clickthrough data. KDD (2002) <br>+ * [2] T.Joachims.Evaluatingretrievalperformanceusingclickthroughdata.InJ.Franke, G. Nakhaeizadeh,+ * and I. Renz, editors, Text Mining, pages 79–96. Physica/Springer (2003) <br>+ * [3] F. Radlinski, M. Kurup, and T. Joachims. How does clickthrough data reflect re- trieval+ * quality? In CIKM, pages 43–52. ACM Press (2008) <br>+ * [4] O. Chapelle, T. Joachims, F. Radlinski, and Y. Yue. Large-scale validation and analysis of+ * interleaved search evaluation. ACM TOIS, 30(1):1–41, Feb. (2012)*/public class TeamDraftInterleaving implements Interleaving {public static Random RANDOM;static {- // We try to make things reproducible in the context of our tests by initializing the random instance- // based on the current seed+ // We try to make things reproducible in the context of our tests by initializing the random+ // instance based on the current seedString seed = System.getProperty("tests.seed");if (seed == null) {RANDOM = new Random();@@ -57,20 +59,20 @@ public class TeamDraftInterleaving implements Interleaving {}/**- * Team Draft Interleaving considers two ranking models: modelA and modelB.- * For a given query, each model returns its ranked list of documents La = (a1,a2,...) and Lb = (b1, b2, ...).- * The algorithm creates a unique ranked list I = (i1, i2, ...).- * This list is created by interleaving elements from the two lists la and lb as described by Chapelle et al.[1].- * Each element Ij is labelled TeamA if it is selected from La and TeamB if it is selected from Lb.- * <p>- * [1] O. Chapelle, T. Joachims, F. Radlinski, and Y. Yue.- * Large-scale validation and analysis of interleaved search evaluation. ACM TOIS, 30(1):1–41, Feb. (2012)- * <p>- * Assumptions:- * - rerankedA and rerankedB has the same length.- * They contains the same search results, ranked differently by two ranking models- * - each reranked list can not contain the same search result more than once.- * - results are all from the same shard+ * Team Draft Interleaving considers two ranking models: modelA and modelB. For a given query,+ * each model returns its ranked list of documents La = (a1,a2,...) and Lb = (b1, b2, ...). The+ * algorithm creates a unique ranked list I = (i1, i2, ...). This list is created by interleaving+ * elements from the two lists la and lb as described by Chapelle et al.[1]. Each element Ij is+ * labelled TeamA if it is selected from La and TeamB if it is selected from Lb.+ *+ * <p>[1] O. Chapelle, T. Joachims, F. Radlinski, and Y. Yue. Large-scale validation and analysis+ * of interleaved search evaluation. ACM TOIS, 30(1):1–41, Feb. (2012)+ *+ * <p>Assumptions:<br>+ * - rerankedA and rerankedB has the same length. They contains the same search results, ranked+ * differently by two ranking models<br>+ * - each reranked list can not contain the same search result more than once.<br>+ * - results are all from the same shard<br>** @param rerankedA a ranked list of search results produced by a ranking model A* @param rerankedB a ranked list of search results produced by a ranking model B@@ -86,15 +88,17 @@ public class TeamDraftInterleaving implements Interleaving {int topN = rerankedA.length;int indexA = 0, indexB = 0;- while (interleavedResults.size() < topN && indexA < rerankedA.length && indexB < rerankedB.length) {- if(teamA.size()<teamB.size() || (teamA.size()==teamB.size() && !RANDOM.nextBoolean())){+ while (interleavedResults.size() < topN+ && indexA < rerankedA.length+ && indexB < rerankedB.length) {+ if (teamA.size() < teamB.size() || (teamA.size() == teamB.size() && !RANDOM.nextBoolean())) {indexA = updateIndex(alreadyAdded, indexA, rerankedA);interleavedResults.add(rerankedA[indexA]);alreadyAdded.add(rerankedA[indexA].doc);teamA.add(rerankedA[indexA].doc);indexA++;- } else{- indexB = updateIndex(alreadyAdded,indexB,rerankedB);+ } else {+ indexB = updateIndex(alreadyAdded, indexB, rerankedB);interleavedResults.add(rerankedB[indexB]);alreadyAdded.add(rerankedB[indexB].doc);teamB.add(rerankedB[indexB].doc);@@ -105,7 +109,7 @@ public class TeamDraftInterleaving implements Interleaving {interleavingPicks.add(teamB);interleavedResultArray = interleavedResults.toArray(interleavedResultArray);- return new InterleavingResult(interleavedResultArray,interleavingPicks);+ return new InterleavingResult(interleavedResultArray, interleavingPicks);}private int updateIndex(HashSet<Integer> alreadyAdded, int index, ScoreDoc[] reranked) {diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/package-info.javaindex d66eedf6841..32c417eed7c 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/algorithms/package-info.java@@ -15,7 +15,5 @@* limitations under the License.*/-/**- * Contains Various Interleaving Algorithms- */+/** Contains Various Interleaving Algorithms */package org.apache.solr.ltr.interleaving.algorithms;diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/package-info.javaindex 82f59758717..301ca0571c8 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/interleaving/package-info.java@@ -15,7 +15,5 @@* limitations under the License.*/-/**- * Contains Various Interleaving auxiliary classes- */+/** Contains Various Interleaving auxiliary classes */package org.apache.solr.ltr.interleaving;diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/AdapterModel.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/AdapterModel.javaindex 262a47f99f3..61e0d4c1ff4 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/AdapterModel.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/AdapterModel.java@@ -18,14 +18,13 @@ package org.apache.solr.ltr.model;import java.util.List;import java.util.Map;-import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.ltr.feature.Feature;import org.apache.solr.ltr.norm.Normalizer;/**- * A scoring model whose initialization is completed via its- * {@link #init(SolrResourceLoader)} method.+ * A scoring model whose initialization is completed via its {@link #init(SolrResourceLoader)}+ * method.** @since 7.3*/@@ -33,13 +32,17 @@ public abstract class AdapterModel extends LTRScoringModel {protected SolrResourceLoader solrResourceLoader;- public AdapterModel(String name, List<Feature> features, List<Normalizer> norms, String featureStoreName,- List<Feature> allFeatures, Map<String,Object> params) {+ public AdapterModel(+ String name,+ List<Feature> features,+ List<Normalizer> norms,+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {super(name, features, norms, featureStoreName, allFeatures, params);}public void init(SolrResourceLoader solrResourceLoader) throws ModelException {this.solrResourceLoader = solrResourceLoader;}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.javaindex a56fe82d323..b18251e13c8 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java@@ -25,7 +25,6 @@ import java.io.Reader;import java.nio.charset.StandardCharsets;import java.util.List;import java.util.Map;-import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.ltr.feature.Feature;import org.apache.solr.ltr.norm.Normalizer;@@ -35,32 +34,40 @@ import org.noggit.ObjectBuilder;/*** A scoring model that fetches the wrapped model from {@link SolrResourceLoader}.*- * <p>This model uses {@link SolrResourceLoader#openResource(String)} for fetching the wrapped model.- * If you give a relative path for {@code params/resource}, this model will try to load the wrapped model from- * the instance directory (i.e. ${solr.solr.home}). Otherwise, seek through classpaths.+ * <p>This model uses {@link SolrResourceLoader#openResource(String)} for fetching the wrapped+ * model. If you give a relative path for {@code params/resource}, this model will try to load the+ * wrapped model from the instance directory (i.e. ${solr.solr.home}). Otherwise, seek through+ * classpaths.** <p>Example configuration:- * <pre>{- "class": "org.apache.solr.ltr.model.DefaultWrapperModel",- "name": "myWrapperModelName",- "params": {- "resource": "models/myModel.json"- }-}</pre>+ *+ * <pre>+ * {+ * "class": "org.apache.solr.ltr.model.DefaultWrapperModel",+ * "name": "myWrapperModelName",+ * "params": {+ * "resource": "models/myModel.json"+ * }+ * }+ * </pre>** @see SolrResourceLoader#openResource(String)*/public class DefaultWrapperModel extends WrapperModel {/**- * resource is part of the LTRScoringModel params map- * and therefore here it does not individually+ * resource is part of the LTRScoringModel params map and therefore here it does not individually* influence the class hashCode, equals, etc.*/private String resource;- public DefaultWrapperModel(String name, List<Feature> features, List<Normalizer> norms, String featureStoreName,- List<Feature> allFeatures, Map<String, Object> params) {+ public DefaultWrapperModel(+ String name,+ List<Feature> features,+ List<Normalizer> norms,+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {super(name, features, norms, featureStoreName, allFeatures, params);}@@ -72,7 +79,7 @@ public class DefaultWrapperModel extends WrapperModel {protected void validate() throws ModelException {super.validate();if (resource == null) {- throw new ModelException("no resource configured for model "+name);+ throw new ModelException("no resource configured for model " + name);}}@@ -82,7 +89,8 @@ public class DefaultWrapperModel extends WrapperModel {try (InputStream in = openInputStream()) {modelMapObj = parseInputStream(in);} catch (IOException e) {- throw new ModelException("Failed to fetch the wrapper model from given resource (" + resource + ")", e);+ throw new ModelException(+ "Failed to fetch the wrapper model from given resource (" + resource + ")", e);}return modelMapObj;}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.javaindex b1fb21f0a9b..2f57bf50bd2 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java@@ -24,7 +24,6 @@ import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Objects;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.Explanation;import org.apache.lucene.util.Accountable;@@ -38,71 +37,83 @@ import org.apache.solr.util.SolrPluginUtils;/*** A scoring model computes scores that can be used to rerank documents.- * <p>- * A scoring model consists of+ *+ * <p>A scoring model consists of+ ** <ul>- * <li> a list of features ({@link Feature}) and- * <li> a list of normalizers ({@link Normalizer}) plus- * <li> parameters or configuration to represent the scoring algorithm.+ * <li>a list of features ({@link Feature}) and+ * <li>a list of normalizers ({@link Normalizer}) plus+ * <li>parameters or configuration to represent the scoring algorithm.* </ul>- * <p>- * Example configuration (snippet):- * <pre>{- "class" : "...",- "name" : "myModelName",- "features" : [- {- "name" : "isBook"- },- {- "name" : "originalScore",- "norm": {- "class" : "org.apache.solr.ltr.norm.StandardNormalizer",- "params" : { "avg":"100", "std":"10" }- }- },- {- "name" : "price",- "norm": {- "class" : "org.apache.solr.ltr.norm.MinMaxNormalizer",- "params" : { "min":"0", "max":"1000" }- }- }- ],- "params" : {- ...- }-}</pre>- * <p>- * {@link LTRScoringModel} is an abstract class and concrete classes must- * implement the {@link #score(float[])} and- * {@link #explain(LeafReaderContext, int, float, List)} methods.+ *+ * <p>Example configuration (snippet):+ *+ * <pre>+ * {+ * "class" : "...",+ * "name" : "myModelName",+ * "features" : [+ * {+ * "name" : "isBook"+ * },+ * {+ * "name" : "originalScore",+ * "norm": {+ * "class" : "org.apache.solr.ltr.norm.StandardNormalizer",+ * "params" : { "avg":"100", "std":"10" }+ * }+ * },+ * {+ * "name" : "price",+ * "norm": {+ * "class" : "org.apache.solr.ltr.norm.MinMaxNormalizer",+ * "params" : { "min":"0", "max":"1000" }+ * }+ * }+ * ],+ * "params" : {+ * ...+ * }+ * }+ * </pre>+ *+ * <p>{@link LTRScoringModel} is an abstract class and concrete classes must implement the {@link+ * #score(float[])} and {@link #explain(LeafReaderContext, int, float, List)} methods.*/public abstract class LTRScoringModel implements Accountable {- private static final long BASE_RAM_BYTES = RamUsageEstimator.shallowSizeOfInstance(LTRScoringModel.class);+ private static final long BASE_RAM_BYTES =+ RamUsageEstimator.shallowSizeOfInstance(LTRScoringModel.class);protected final String name;private final String featureStoreName;protected final List<Feature> features;private final List<Feature> allFeatures;- private final Map<String,Object> params;+ private final Map<String, Object> params;protected final List<Normalizer> norms;private Integer hashCode; // cached since it shouldn't actually change after construction- public static LTRScoringModel getInstance(SolrResourceLoader solrResourceLoader,- String className, String name, List<Feature> features,+ public static LTRScoringModel getInstance(+ SolrResourceLoader solrResourceLoader,+ String className,+ String name,+ List<Feature> features,List<Normalizer> norms,- String featureStoreName, List<Feature> allFeatures,- Map<String,Object> params) throws ModelException {+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params)+ throws ModelException {final LTRScoringModel model;try {// create an instance of the model- model = solrResourceLoader.newInstance(- className,- LTRScoringModel.class,- new String[0], // no sub packages- new Class<?>[] { String.class, List.class, List.class, String.class, List.class, Map.class },- new Object[] { name, features, norms, featureStoreName, allFeatures, params });+ model =+ solrResourceLoader.newInstance(+ className,+ LTRScoringModel.class,+ new String[0], // no sub packages+ new Class<?>[] {+ String.class, List.class, List.class, String.class, List.class, Map.class+ },+ new Object[] {name, features, norms, featureStoreName, allFeatures, params});if (params != null) {SolrPluginUtils.invokeSetters(model, params.entrySet());}@@ -113,74 +124,78 @@ public abstract class LTRScoringModel implements Accountable {return model;}- public LTRScoringModel(String name, List<Feature> features,+ public LTRScoringModel(+ String name,+ List<Feature> features,List<Normalizer> norms,- String featureStoreName, List<Feature> allFeatures,- Map<String,Object> params) {+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {this.name = name;- this.features = features != null ? Collections.unmodifiableList(new ArrayList<>(features)) : null;+ this.features =+ features != null ? Collections.unmodifiableList(new ArrayList<>(features)) : null;this.featureStoreName = featureStoreName;- this.allFeatures = allFeatures != null ? Collections.unmodifiableList(new ArrayList<>(allFeatures)) : null;+ this.allFeatures =+ allFeatures != null ? Collections.unmodifiableList(new ArrayList<>(allFeatures)) : null;this.params = params != null ? Collections.unmodifiableMap(new LinkedHashMap<>(params)) : null;this.norms = norms != null ? Collections.unmodifiableList(new ArrayList<>(norms)) : null;}/**- * Validate that settings make sense and throws- * {@link ModelException} if they do not make sense.+ * Validate that settings make sense and throws {@link ModelException} if they do not make sense.*/protected void validate() throws ModelException {final List<Feature> features = getFeatures();final List<Normalizer> norms = getNorms();if (features.isEmpty()) {- throw new ModelException("no features declared for model "+name);+ throw new ModelException("no features declared for model " + name);}final HashSet<String> featureNames = new HashSet<>();for (final Feature feature : features) {final String featureName = feature.getName();if (!featureNames.add(featureName)) {- throw new ModelException("duplicated feature "+featureName+" in model "+name);+ throw new ModelException("duplicated feature " + featureName + " in model " + name);}}if (features.size() != norms.size()) {- throw new ModelException("counted "+features.size()+" features and "+norms.size()+" norms in model "+name);+ throw new ModelException(+ "counted "+ + features.size()+ + " features and "+ + norms.size()+ + " norms in model "+ + name);}}- /**- * @return the norms- */+ /** @return the norms */public List<Normalizer> getNorms() {return norms;}- /**- * @return the name- */+ /** @return the name */public String getName() {return name;}- /**- * @return the features- */+ /** @return the features */public List<Feature> getFeatures() {return features;}- public Map<String,Object> getParams() {+ public Map<String, Object> getParams() {return params;}@Overridepublic int hashCode() {- if(hashCode == null) {+ if (hashCode == null) {hashCode = calculateHashCode();}return hashCode;}- final private int calculateHashCode() {+ private final int calculateHashCode() {final int prime = 31;int result = 1;result = (prime * result) + Objects.hashCode(features);@@ -239,19 +254,19 @@ public abstract class LTRScoringModel implements Accountable {return false;}-return true;}@Overridepublic long ramBytesUsed() {- return BASE_RAM_BYTES +- RamUsageEstimator.sizeOfObject(allFeatures, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED) +- RamUsageEstimator.sizeOfObject(features, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED) +- RamUsageEstimator.sizeOfObject(featureStoreName) +- RamUsageEstimator.sizeOfObject(name) +- RamUsageEstimator.sizeOfObject(norms) +- RamUsageEstimator.sizeOfObject(params);+ return BASE_RAM_BYTES+ + RamUsageEstimator.sizeOfObject(+ allFeatures, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED)+ + RamUsageEstimator.sizeOfObject(features, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED)+ + RamUsageEstimator.sizeOfObject(featureStoreName)+ + RamUsageEstimator.sizeOfObject(name)+ + RamUsageEstimator.sizeOfObject(norms)+ + RamUsageEstimator.sizeOfObject(params);}public Collection<Feature> getAllFeatures() {@@ -263,48 +278,43 @@ public abstract class LTRScoringModel implements Accountable {}/**- * Given a list of normalized values for all features a scoring algorithm- * cares about, calculate and return a score.+ * Given a list of normalized values for all features a scoring algorithm cares about, calculate+ * and return a score.*- * @param modelFeatureValuesNormalized- * List of normalized feature values. Each feature is identified by- * its id, which is the index in the array+ * @param modelFeatureValuesNormalized List of normalized feature values. Each feature is+ * identified by its id, which is the index in the array* @return The final score for a document*/public abstract float score(float[] modelFeatureValuesNormalized);/**- * Similar to the score() function, except it returns an explanation of how- * the features were used to calculate the score.+ * Similar to the score() function, except it returns an explanation of how the features were used+ * to calculate the score.*- * @param context- * Context the document is in- * @param doc- * Document to explain- * @param finalScore- * Original score- * @param featureExplanations- * Explanations for each feature calculation+ * @param context Context the document is in+ * @param doc Document to explain+ * @param finalScore Original score+ * @param featureExplanations Explanations for each feature calculation* @return Explanation for the scoring of a document*/- public abstract Explanation explain(LeafReaderContext context, int doc,- float finalScore, List<Explanation> featureExplanations);+ public abstract Explanation explain(+ LeafReaderContext context, int doc, float finalScore, List<Explanation> featureExplanations);@Overridepublic String toString() {- return getClass().getSimpleName() + "(name="+getName()+")";+ return getClass().getSimpleName() + "(name=" + getName() + ")";}/**- * Goes through all the stored feature values, and calculates the normalized- * values for all the features that will be used for scoring.+ * Goes through all the stored feature values, and calculates the normalized values for all the+ * features that will be used for scoring.*/public void normalizeFeaturesInPlace(float[] modelFeatureValues) {float[] modelFeatureValuesNormalized = modelFeatureValues;if (modelFeatureValues.length != norms.size()) {throw new FeatureException("Must have normalizer for every feature");}- for(int idx = 0; idx < modelFeatureValuesNormalized.length; ++idx) {+ for (int idx = 0; idx < modelFeatureValuesNormalized.length; ++idx) {modelFeatureValuesNormalized[idx] =norms.get(idx).normalize(modelFeatureValuesNormalized[idx]);}@@ -317,5 +327,4 @@ public abstract class LTRScoringModel implements Accountable {}return e;}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LinearModel.javaindex 965c0cb550b..6b030f2d71a 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java@@ -19,68 +19,73 @@ package org.apache.solr.ltr.model;import java.util.ArrayList;import java.util.List;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.Explanation;import org.apache.solr.ltr.feature.Feature;import org.apache.solr.ltr.norm.Normalizer;/**- * A scoring model that computes scores using a dot product.- * Example models are RankSVM and Pranking.- * <p>- * Example configuration:- * <pre>{- "class" : "org.apache.solr.ltr.model.LinearModel",- "name" : "myModelName",- "features" : [- { "name" : "userTextTitleMatch" },- { "name" : "originalScore" },- { "name" : "isBook" }- ],- "params" : {- "weights" : {- "userTextTitleMatch" : 1.0,- "originalScore" : 0.5,- "isBook" : 0.1- }- }-}</pre>- * <p>- * Training libraries:+ * A scoring model that computes scores using a dot product. Example models are RankSVM and+ * Pranking.+ *+ * <p>Example configuration:+ *+ * <pre>+ * {+ * "class" : "org.apache.solr.ltr.model.LinearModel",+ * "name" : "myModelName",+ * "features" : [+ * { "name" : "userTextTitleMatch" },+ * { "name" : "originalScore" },+ * { "name" : "isBook" }+ * ],+ * "params" : {+ * "weights" : {+ * "userTextTitleMatch" : 1.0,+ * "originalScore" : 0.5,+ * "isBook" : 0.1+ * }+ * }+ * }+ * </pre>+ *+ * <p>Training libraries:+ ** <ul>- * <li> <a href="https://www.csie.ntu.edu.tw/~cjlin/liblinear/">- * LIBLINEAR -- A Library for Large Linear Classification</a>+ * <li><a href="https://www.csie.ntu.edu.tw/~cjlin/liblinear/">LIBLINEAR -- A Library for Large+ * Linear Classification</a>* </ul>+ ** <ul>- * <li> <a href="https://www.csie.ntu.edu.tw/~cjlin/libsvm/">- * LIBSVM -- A Library for Support Vector Machines</a>+ * <li><a href="https://www.csie.ntu.edu.tw/~cjlin/libsvm/">LIBSVM -- A Library for Support Vector+ * Machines</a>* </ul>- * <p>- * Background reading:+ *+ * <p>Background reading:+ ** <ul>- * <li> <a href="http://www.cs.cornell.edu/people/tj/publications/joachims_02c.pdf">- * Thorsten Joachims. Optimizing Search Engines Using Clickthrough Data.- * Proceedings of the ACM Conference on Knowledge Discovery and Data Mining (KDD), ACM, 2002.</a>+ * <li><a href="http://www.cs.cornell.edu/people/tj/publications/joachims_02c.pdf">Thorsten+ * Joachims. Optimizing Search Engines Using Clickthrough Data. Proceedings of the ACM+ * Conference on Knowledge Discovery and Data Mining (KDD), ACM, 2002.</a>* </ul>+ ** <ul>- * <li> <a href="https://papers.nips.cc/paper/2023-pranking-with-ranking.pdf">- * Koby Crammer and Yoram Singer. Pranking with Ranking.- * Advances in Neural Information Processing Systems (NIPS), 2001.</a>+ * <li><a href="https://papers.nips.cc/paper/2023-pranking-with-ranking.pdf">Koby Crammer and+ * Yoram Singer. Pranking with Ranking. Advances in Neural Information Processing Systems+ * (NIPS), 2001.</a>* </ul>*/public class LinearModel extends LTRScoringModel {/**- * featureToWeight is part of the LTRScoringModel params map- * and therefore here it does not individually- * influence the class hashCode, equals, etc.+ * featureToWeight is part of the LTRScoringModel params map and therefore here it does not+ * individually influence the class hashCode, equals, etc.*/protected Float[] featureToWeight;public void setWeights(Object weights) {@SuppressWarnings({"unchecked"})- final Map<String,Number> modelWeights = (Map<String, Number>) weights;+ final Map<String, Number> modelWeights = (Map<String, Number>) weights;for (int ii = 0; ii < features.size(); ++ii) {final String key = features.get(ii).getName();final Number val = modelWeights.get(key);@@ -88,10 +93,13 @@ public class LinearModel extends LTRScoringModel {}}- public LinearModel(String name, List<Feature> features,+ public LinearModel(+ String name,+ List<Feature> features,List<Normalizer> norms,- String featureStoreName, List<Feature> allFeatures,- Map<String,Object> params) {+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {super(name, features, norms, featureStoreName, allFeatures, params);featureToWeight = new Float[features.size()];}@@ -110,7 +118,8 @@ public class LinearModel extends LTRScoringModel {throw new ModelException("Model " + name + " doesn't contain any weights");}if (!missingWeightFeatureNames.isEmpty()) {- throw new ModelException("Model " + name + " lacks weight(s) for "+missingWeightFeatureNames);+ throw new ModelException(+ "Model " + name + " lacks weight(s) for " + missingWeightFeatureNames);}}@@ -124,24 +133,26 @@ public class LinearModel extends LTRScoringModel {}@Override- public Explanation explain(LeafReaderContext context, int doc,- float finalScore, List<Explanation> featureExplanations) {+ public Explanation explain(+ LeafReaderContext context, int doc, float finalScore, List<Explanation> featureExplanations) {final List<Explanation> details = new ArrayList<>();int index = 0;for (final Explanation featureExplain : featureExplanations) {final List<Explanation> featureDetails = new ArrayList<>();- featureDetails.add(Explanation.match(featureToWeight[index],- "weight on feature"));+ featureDetails.add(Explanation.match(featureToWeight[index], "weight on feature"));featureDetails.add(featureExplain);- details.add(Explanation.match(featureExplain.getValue().floatValue()- * featureToWeight[index], "prod of:", featureDetails));+ details.add(+ Explanation.match(+ featureExplain.getValue().floatValue() * featureToWeight[index],+ "prod of:",+ featureDetails));index++;}- return Explanation.match(finalScore, toString()- + " model applied to features, sum of:", details);+ return Explanation.match(+ finalScore, toString() + " model applied to features, sum of:", details);}@Override@@ -150,7 +161,7 @@ public class LinearModel extends LTRScoringModel {sb.append("(name=").append(getName());sb.append(",featureWeights=[");for (int ii = 0; ii < features.size(); ++ii) {- if (ii>0) {+ if (ii > 0) {sb.append(',');}final String key = features.get(ii).getName();@@ -159,5 +170,4 @@ public class LinearModel extends LTRScoringModel {sb.append("])");return sb.toString();}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/ModelException.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/ModelException.javaindex de8786d81c0..a4e57afb535 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/ModelException.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/ModelException.java@@ -27,5 +27,4 @@ public class ModelException extends RuntimeException {public ModelException(String message, Exception cause) {super(message, cause);}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.javaindex d79db8382d7..b66e2ff3b31 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java@@ -21,7 +21,6 @@ import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Stack;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.Explanation;import org.apache.solr.ltr.feature.Feature;@@ -29,83 +28,89 @@ import org.apache.solr.ltr.norm.Normalizer;import org.apache.solr.util.SolrPluginUtils;/**- * A scoring model that computes scores based on the summation of multiple weighted trees.- * Example models are LambdaMART and Gradient Boosted Regression Trees (GBRT) .- * <p>- * Example configuration:-<pre>{- "class" : "org.apache.solr.ltr.model.MultipleAdditiveTreesModel",- "name" : "multipleadditivetreesmodel",- "features":[- { "name" : "userTextTitleMatch"},- { "name" : "originalScore"}- ],- "params" : {- "trees" : [- {- "weight" : "1",- "root": {- "feature" : "userTextTitleMatch",- "threshold" : "0.5",- "left" : {- "value" : "-100"- },- "right" : {- "feature" : "originalScore",- "threshold" : "10.0",- "left" : {- "value" : "50"- },- "right" : {- "value" : "75"- }- }- }- },- {- "weight" : "2",- "root" : {- "value" : "-10"- }- }- ]- }-}</pre>- * <p>- * Training libraries:+ * A scoring model that computes scores based on the summation of multiple weighted trees. Example+ * models are LambdaMART and Gradient Boosted Regression Trees (GBRT) .+ *+ * <p>Example configuration:+ *+ * <pre>+ * {+ * "class" : "org.apache.solr.ltr.model.MultipleAdditiveTreesModel",+ * "name" : "multipleadditivetreesmodel",+ * "features":[+ * { "name" : "userTextTitleMatch"},+ * { "name" : "originalScore"}+ * ],+ * "params" : {+ * "trees" : [+ * {+ * "weight" : "1",+ * "root": {+ * "feature" : "userTextTitleMatch",+ * "threshold" : "0.5",+ * "left" : {+ * "value" : "-100"+ * },+ * "right" : {+ * "feature" : "originalScore",+ * "threshold" : "10.0",+ * "left" : {+ * "value" : "50"+ * },+ * "right" : {+ * "value" : "75"+ * }+ * }+ * }+ * },+ * {+ * "weight" : "2",+ * "root" : {+ * "value" : "-10"+ * }+ * }+ * ]+ * }+ * }+ * </pre>+ *+ * <p>Training libraries:+ ** <ul>- * <li> <a href="http://sourceforge.net/p/lemur/wiki/RankLib/">RankLib</a>+ * <li><a href="http://sourceforge.net/p/lemur/wiki/RankLib/">RankLib</a>* </ul>- * <p>- * Background reading:+ *+ * <p>Background reading:+ ** <ul>- * <li> <a href="http://research.microsoft.com/pubs/132652/MSR-TR-2010-82.pdf">- * Christopher J.C. Burges. From RankNet to LambdaRank to LambdaMART: An Overview.- * Microsoft Research Technical Report MSR-TR-2010-82.</a>+ * <li><a href="http://research.microsoft.com/pubs/132652/MSR-TR-2010-82.pdf">Christopher J.C.+ * Burges. From RankNet to LambdaRank to LambdaMART: An Overview. Microsoft Research Technical+ * Report MSR-TR-2010-82.</a>* </ul>+ ** <ul>- * <li> <a href="https://papers.nips.cc/paper/3305-a-general-boosting-method-and-its-application-to-learning-ranking-functions-for-web-search.pdf">- * Z. Zheng, H. Zha, T. Zhang, O. Chapelle, K. Chen, and G. Sun. A General Boosting Method and its Application to Learning Ranking Functions for Web Search.- * Advances in Neural Information Processing Systems (NIPS), 2007.</a>+ * <li><a+ * href="https://papers.nips.cc/paper/3305-a-general-boosting-method-and-its-application-to-learning-ranking-functions-for-web-search.pdf">+ * Z. Zheng, H. Zha, T. Zhang, O. Chapelle, K. Chen, and G. Sun. A General Boosting Method and+ * its Application to Learning Ranking Functions for Web Search. Advances in Neural+ * Information Processing Systems (NIPS), 2007.</a>* </ul>*/public class MultipleAdditiveTreesModel extends LTRScoringModel {/**- * fname2index is filled from constructor arguments- * (that are already part of the base class hashCode)- * and therefore here it does not individually- * influence the class hashCode, equals, etc.+ * fname2index is filled from constructor arguments (that are already part of the base class+ * hashCode) and therefore here it does not individually influence the class hashCode, equals,+ * etc.*/- private final HashMap<String,Integer> fname2index;+ private final HashMap<String, Integer> fname2index;/**- * trees is part of the LTRScoringModel params map- * and therefore here it does not individually+ * trees is part of the LTRScoringModel params map and therefore here it does not individually* influence the class hashCode, equals, etc.*/private List<RegressionTree> trees;- private RegressionTree createRegressionTree(Map<String,Object> map) {+ private RegressionTree createRegressionTree(Map<String, Object> map) {final RegressionTree rt = new RegressionTree();if (map != null) {SolrPluginUtils.invokeSetters(rt, map.entrySet());@@ -113,7 +118,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {return rt;}- private RegressionTreeNode createRegressionTreeNode(Map<String,Object> map) {+ private RegressionTreeNode createRegressionTreeNode(Map<String, Object> map) {final RegressionTreeNode rtn = new RegressionTreeNode();if (map != null) {SolrPluginUtils.invokeSetters(rtn, map.entrySet());@@ -158,12 +163,12 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {@SuppressWarnings({"unchecked"})public void setLeft(Object left) {- this.left = createRegressionTreeNode((Map<String,Object>) left);+ this.left = createRegressionTreeNode((Map<String, Object>) left);}@SuppressWarnings({"unchecked"})public void setRight(Object right) {- this.right = createRegressionTreeNode((Map<String,Object>) right);+ this.right = createRegressionTreeNode((Map<String, Object>) right);}public boolean isLeaf() {@@ -177,7 +182,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {sb.append(value);} else {sb.append("(feature=").append(feature);- sb.append(",threshold=").append(threshold.floatValue()-NODE_SPLIT_SLACK);+ sb.append(",threshold=").append(threshold.floatValue() - NODE_SPLIT_SLACK);sb.append(",left=").append(left);sb.append(",right=").append(right);sb.append(')');@@ -185,9 +190,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {return sb.toString();}- public RegressionTreeNode() {- }-+ public RegressionTreeNode() {}}public class RegressionTree {@@ -205,7 +208,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {@SuppressWarnings({"unchecked"})public void setRoot(Object root) {- this.root = createRegressionTreeNode((Map<String,Object>)root);+ this.root = createRegressionTreeNode((Map<String, Object>) root);}public float score(float[] featureVector) {@@ -225,8 +228,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {return sb.toString();}- public RegressionTree() {- }+ public RegressionTree() {}public void validate() throws ModelException {if (weight == null) {@@ -244,18 +246,21 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {public void setTrees(Object trees) {this.trees = new ArrayList<RegressionTree>();for (final Object o : (List<Object>) trees) {- final RegressionTree rt = createRegressionTree((Map<String,Object>) o);+ final RegressionTree rt = createRegressionTree((Map<String, Object>) o);this.trees.add(rt);}}- public MultipleAdditiveTreesModel(String name, List<Feature> features,+ public MultipleAdditiveTreesModel(+ String name,+ List<Feature> features,List<Normalizer> norms,- String featureStoreName, List<Feature> allFeatures,- Map<String,Object> params) {+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {super(name, features, norms, featureStoreName, allFeatures, params);- fname2index = new HashMap<String,Integer>();+ fname2index = new HashMap<String, Integer>();for (int i = 0; i < features.size(); ++i) {final String key = features.get(i).getName();fname2index.put(key, i);@@ -266,7 +271,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {protected void validate() throws ModelException {super.validate();if (trees == null) {- throw new ModelException("no trees declared for model "+name);+ throw new ModelException("no trees declared for model " + name);}for (RegressionTree tree : trees) {tree.validate();@@ -288,7 +293,8 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {return regressionTreeNode.value;}// unsupported feature (tree is looking for a feature that does not exist)- if ((regressionTreeNode.featureIndex < 0) || (regressionTreeNode.featureIndex >= featureVector.length)) {+ if ((regressionTreeNode.featureIndex < 0)+ || (regressionTreeNode.featureIndex >= featureVector.length)) {return 0f;}@@ -301,7 +307,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {}private static void validateNode(RegressionTreeNode regressionTreeNode) throws ModelException {-+// Create an empty stack and push root to itStack<RegressionTreeNode> stack = new Stack<RegressionTreeNode>();stack.push(regressionTreeNode);@@ -311,7 +317,11 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {if (topStackNode.isLeaf()) {if (topStackNode.left != null || topStackNode.right != null) {- throw new ModelException("MultipleAdditiveTreesModel tree node is leaf with left=" + topStackNode.left + " and right=" + topStackNode.right);+ throw new ModelException(+ "MultipleAdditiveTreesModel tree node is leaf with left="+ + topStackNode.left+ + " and right="+ + topStackNode.right);}continue;}@@ -340,8 +350,10 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {}// unsupported feature (tree is looking for a feature that does not exist)- if ((regressionTreeNode.featureIndex < 0) || (regressionTreeNode.featureIndex >= featureVector.length)) {- returnValueBuilder.append("'" + regressionTreeNode.feature + "' does not exist in FV, Return Zero");+ if ((regressionTreeNode.featureIndex < 0)+ || (regressionTreeNode.featureIndex >= featureVector.length)) {+ returnValueBuilder.append(+ "'" + regressionTreeNode.feature + "' does not exist in FV, Return Zero");return returnValueBuilder.toString();}@@ -350,19 +362,29 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {// that hereif (featureVector[regressionTreeNode.featureIndex] <= regressionTreeNode.threshold) {- returnValueBuilder.append("'" + regressionTreeNode.feature + "':" + featureVector[regressionTreeNode.featureIndex] + " <= "- + regressionTreeNode.threshold + ", Go Left | ");+ returnValueBuilder.append(+ "'"+ + regressionTreeNode.feature+ + "':"+ + featureVector[regressionTreeNode.featureIndex]+ + " <= "+ + regressionTreeNode.threshold+ + ", Go Left | ");regressionTreeNode = regressionTreeNode.left;} else {- returnValueBuilder.append("'" + regressionTreeNode.feature + "':" + featureVector[regressionTreeNode.featureIndex] + " > "- + regressionTreeNode.threshold + ", Go Right | ");+ returnValueBuilder.append(+ "'"+ + regressionTreeNode.feature+ + "':"+ + featureVector[regressionTreeNode.featureIndex]+ + " > "+ + regressionTreeNode.threshold+ + ", Go Right | ");regressionTreeNode = regressionTreeNode.right;}}-}-// /////////////////////////////////////////// produces a string that looks like:// 40.0 = multipleadditivetreesmodel [ org.apache.solr.ltr.model.MultipleAdditiveTreesModel ]@@ -373,8 +395,8 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {// exist in FV, Go Left | val: 50.0// -10.0 = tree 1 | val: -10.0@Override- public Explanation explain(LeafReaderContext context, int doc,- float finalScore, List<Explanation> featureExplanations) {+ public Explanation explain(+ LeafReaderContext context, int doc, float finalScore, List<Explanation> featureExplanations) {final float[] fv = new float[featureExplanations.size()];int index = 0;for (final Explanation featureExplain : featureExplanations) {@@ -387,14 +409,13 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {for (final RegressionTree t : trees) {final float score = t.score(fv);- final Explanation p = Explanation.match(score, "tree " + index + " | "- + t.explain(fv));+ final Explanation p = Explanation.match(score, "tree " + index + " | " + t.explain(fv));details.add(p);index++;}- return Explanation.match(finalScore, toString()- + " model applied to features, sum of:", details);+ return Explanation.match(+ finalScore, toString() + " model applied to features, sum of:", details);}@Override@@ -403,7 +424,7 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {sb.append("(name=").append(getName());sb.append(",trees=[");for (int ii = 0; ii < trees.size(); ++ii) {- if (ii>0) {+ if (ii > 0) {sb.append(',');}sb.append(trees.get(ii));@@ -411,5 +432,4 @@ public class MultipleAdditiveTreesModel extends LTRScoringModel {sb.append("])");return sb.toString();}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.javaindex d136eead4c6..de769586c67 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java@@ -19,7 +19,6 @@ package org.apache.solr.ltr.model;import java.util.ArrayList;import java.util.List;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.Explanation;import org.apache.solr.ltr.feature.Feature;@@ -28,75 +27,81 @@ import org.apache.solr.util.SolrPluginUtils;/*** A scoring model that computes document scores using a neural network.- * <p>- * Supported <a href="https://en.wikipedia.org/wiki/Activation_function">activation functions</a> are:- * <code>identity</code>, <code>relu</code>, <code>sigmoid</code>, <code>tanh</code>, <code>leakyrelu</code> and- * contributions to support additional activation functions are welcome.- * <p>- * Example configuration:-<pre>{- "class" : "org.apache.solr.ltr.model.NeuralNetworkModel",- "name" : "rankNetModel",- "features" : [- { "name" : "documentRecency" },- { "name" : "isBook" },- { "name" : "originalScore" }- ],- "params" : {- "layers" : [- {- "matrix" : [ [ 1.0, 2.0, 3.0 ],- [ 4.0, 5.0, 6.0 ],- [ 7.0, 8.0, 9.0 ],- [ 10.0, 11.0, 12.0 ] ],- "bias" : [ 13.0, 14.0, 15.0, 16.0 ],- "activation" : "sigmoid"- },- {- "matrix" : [ [ 17.0, 18.0, 19.0, 20.0 ],- [ 21.0, 22.0, 23.0, 24.0 ] ],- "bias" : [ 25.0, 26.0 ],- "activation" : "relu"- },- {- "matrix" : [ [ 27.0, 28.0 ],- [ 29.0, 30.0 ] ],- "bias" : [ 31.0, 32.0 ],- "activation" : "leakyrelu"- },- {- "matrix" : [ [ 33.0, 34.0 ],- [ 35.0, 36.0 ] ],- "bias" : [ 37.0, 38.0 ],- "activation" : "tanh"- },- {- "matrix" : [ [ 39.0, 40.0 ] ],- "bias" : [ 41.0 ],- "activation" : "identity"- }- ]- }-}</pre>- * <p>- * Training libraries:+ *+ * <p>Supported <a href="https://en.wikipedia.org/wiki/Activation_function">activation functions</a>+ * are: <code>identity</code>, <code>relu</code>, <code>sigmoid</code>, <code>tanh</code>, <code>+ * leakyrelu</code> and contributions to support additional activation functions are welcome.+ *+ * <p>Example configuration:+ *+ * <pre>+ * {+ * "class" : "org.apache.solr.ltr.model.NeuralNetworkModel",+ * "name" : "rankNetModel",+ * "features" : [+ * { "name" : "documentRecency" },+ * { "name" : "isBook" },+ * { "name" : "originalScore" }+ * ],+ * "params" : {+ * "layers" : [+ * {+ * "matrix" : [ [ 1.0, 2.0, 3.0 ],+ * [ 4.0, 5.0, 6.0 ],+ * [ 7.0, 8.0, 9.0 ],+ * [ 10.0, 11.0, 12.0 ] ],+ * "bias" : [ 13.0, 14.0, 15.0, 16.0 ],+ * "activation" : "sigmoid"+ * },+ * {+ * "matrix" : [ [ 17.0, 18.0, 19.0, 20.0 ],+ * [ 21.0, 22.0, 23.0, 24.0 ] ],+ * "bias" : [ 25.0, 26.0 ],+ * "activation" : "relu"+ * },+ * {+ * "matrix" : [ [ 27.0, 28.0 ],+ * [ 29.0, 30.0 ] ],+ * "bias" : [ 31.0, 32.0 ],+ * "activation" : "leakyrelu"+ * },+ * {+ * "matrix" : [ [ 33.0, 34.0 ],+ * [ 35.0, 36.0 ] ],+ * "bias" : [ 37.0, 38.0 ],+ * "activation" : "tanh"+ * },+ * {+ * "matrix" : [ [ 39.0, 40.0 ] ],+ * "bias" : [ 41.0 ],+ * "activation" : "identity"+ * }+ * ]+ * }+ * }+ * </pre>+ *+ * <p>Training libraries:+ ** <ul>- * <li> <a href="https://keras.io">Keras</a> is a high-level neural networks API, written in Python.- * A Keras and Solr implementation of RankNet can be found at <a href="https://github.com/airalcorn2/RankNet">https://github.com/airalcorn2/RankNet</a>.+ * <li><a href="https://keras.io">Keras</a> is a high-level neural networks API, written in+ * Python. A Keras and Solr implementation of RankNet can be found at <a+ * href="https://github.com/airalcorn2/RankNet">https://github.com/airalcorn2/RankNet</a>.* </ul>- * <p>- * Background reading:+ *+ * <p>Background reading:+ ** <ul>- * <li> <a href="http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf">- * C. Burges, T. Shaked, E. Renshaw, A. Lazier, M. Deeds, N. Hamilton, and G. Hullender. Learning to Rank Using Gradient Descent.- * Proceedings of the 22nd International Conference on Machine Learning (ICML), ACM, 2005.</a>+ * <li><a href="http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf">C. Burges, T.+ * Shaked, E. Renshaw, A. Lazier, M. Deeds, N. Hamilton, and G. Hullender. Learning to Rank+ * Using Gradient Descent. Proceedings of the 22nd International Conference on Machine+ * Learning (ICML), ACM, 2005.</a>* </ul>*/public class NeuralNetworkModel extends LTRScoringModel {/**- * layers is part of the LTRScoringModel params map- * and therefore here it does not individually+ * layers is part of the LTRScoringModel params map and therefore here it does not individually* influence the class hashCode, equals, etc.*/private List<Layer> layers;@@ -108,7 +113,9 @@ public class NeuralNetworkModel extends LTRScoringModel {public interface Layer {public float[] calculateOutput(float[] inputVec);+public int validate(int inputDim) throws ModelException;+public String describe();}@@ -155,44 +162,49 @@ public class NeuralNetworkModel extends LTRScoringModel {this.activationStr = (String) activationStr;switch (this.activationStr) {case "relu":- this.activation = new Activation() {- @Override- public float apply(float in) {- return in < 0 ? 0 : in;- }- };+ this.activation =+ new Activation() {+ @Override+ public float apply(float in) {+ return in < 0 ? 0 : in;+ }+ };break;case "leakyrelu":- this.activation = new Activation() {- @Override- public float apply(float in) {- return in < 0 ? 0.01f * in : in;- }- };+ this.activation =+ new Activation() {+ @Override+ public float apply(float in) {+ return in < 0 ? 0.01f * in : in;+ }+ };break;case "tanh":- this.activation = new Activation() {- @Override- public float apply(float in) {- return (float)Math.tanh(in);- }- };+ this.activation =+ new Activation() {+ @Override+ public float apply(float in) {+ return (float) Math.tanh(in);+ }+ };break;case "sigmoid":- this.activation = new Activation() {- @Override- public float apply(float in) {- return (float) (1 / (1 + Math.exp(-in)));- }- };+ this.activation =+ new Activation() {+ @Override+ public float apply(float in) {+ return (float) (1 / (1 + Math.exp(-in)));+ }+ };break;case "identity":- this.activation = new Activation() {- @Override- public float apply(float in) {- return in;- }- };+ this.activation =+ new Activation() {+ @Override+ public float apply(float in) {+ return in;+ }+ };break;default:this.activation = null;@@ -217,23 +229,51 @@ public class NeuralNetworkModel extends LTRScoringModel {public int validate(int inputDim) throws ModelException {if (this.numUnits != this.matrixRows) {- throw new ModelException("Dimension mismatch in model \"" + name + "\". Layer " +- Integer.toString(this.layerID) + " has " + Integer.toString(this.numUnits) +- " bias weights but " + Integer.toString(this.matrixRows) + " weight matrix rows.");+ throw new ModelException(+ "Dimension mismatch in model \""+ + name+ + "\". Layer "+ + Integer.toString(this.layerID)+ + " has "+ + Integer.toString(this.numUnits)+ + " bias weights but "+ + Integer.toString(this.matrixRows)+ + " weight matrix rows.");}if (this.activation == null) {- throw new ModelException("Invalid activation function (\""+this.activationStr+"\") in layer "+Integer.toString(this.layerID)+" of model \"" + name + "\".");+ throw new ModelException(+ "Invalid activation function (\""+ + this.activationStr+ + "\") in layer "+ + Integer.toString(this.layerID)+ + " of model \""+ + name+ + "\".");}if (inputDim != this.matrixCols) {if (this.layerID == 0) {- throw new ModelException("Dimension mismatch in model \"" + name + "\". The input has " +- Integer.toString(inputDim) + " features, but the weight matrix for layer 0 has " +- Integer.toString(this.matrixCols) + " columns.");+ throw new ModelException(+ "Dimension mismatch in model \""+ + name+ + "\". The input has "+ + Integer.toString(inputDim)+ + " features, but the weight matrix for layer 0 has "+ + Integer.toString(this.matrixCols)+ + " columns.");} else {- throw new ModelException("Dimension mismatch in model \"" + name + "\". The weight matrix for layer " +- Integer.toString(this.layerID - 1) + " has " + Integer.toString(inputDim) + " rows, but the " +- "weight matrix for layer " + Integer.toString(this.layerID) + " has " +- Integer.toString(this.matrixCols) + " columns.");+ throw new ModelException(+ "Dimension mismatch in model \""+ + name+ + "\". The weight matrix for layer "+ + Integer.toString(this.layerID - 1)+ + " has "+ + Integer.toString(inputDim)+ + " rows, but the "+ + "weight matrix for layer "+ + Integer.toString(this.layerID)+ + " has "+ + Integer.toString(this.matrixCols)+ + " columns.");}}return this.matrixRows;@@ -241,9 +281,13 @@ public class NeuralNetworkModel extends LTRScoringModel {public String describe() {final StringBuilder sb = new StringBuilder();- sb- .append("(matrix=").append(Integer.toString(this.matrixRows)).append('x').append(Integer.toString(this.matrixCols))- .append(",activation=").append(this.activationStr).append(")");+ sb.append("(matrix=")+ .append(Integer.toString(this.matrixRows))+ .append('x')+ .append(Integer.toString(this.matrixCols))+ .append(",activation=")+ .append(this.activationStr)+ .append(")");return sb.toString();}}@@ -252,7 +296,7 @@ public class NeuralNetworkModel extends LTRScoringModel {protected Layer createLayer(Object o) {final DefaultLayer layer = new DefaultLayer();if (o != null) {- SolrPluginUtils.invokeSetters(layer, ((Map<String,Object>) o).entrySet());+ SolrPluginUtils.invokeSetters(layer, ((Map<String, Object>) o).entrySet());}return layer;}@@ -266,10 +310,13 @@ public class NeuralNetworkModel extends LTRScoringModel {}}- public NeuralNetworkModel(String name, List<Feature> features,- List<Normalizer> norms,- String featureStoreName, List<Feature> allFeatures,- Map<String,Object> params) {+ public NeuralNetworkModel(+ String name,+ List<Feature> features,+ List<Normalizer> norms,+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {super(name, features, norms, featureStoreName, allFeatures, params);}@@ -284,8 +331,12 @@ public class NeuralNetworkModel extends LTRScoringModel {}if (inputDim != 1) {- throw new ModelException("The output matrix for model \"" + name + "\" has " + Integer.toString(inputDim) +- " rows, but should only have one.");+ throw new ModelException(+ "The output matrix for model \""+ + name+ + "\" has "+ + Integer.toString(inputDim)+ + " rows, but should only have one.");}}@@ -302,8 +353,8 @@ public class NeuralNetworkModel extends LTRScoringModel {}@Override- public Explanation explain(LeafReaderContext context, int doc,- float finalScore, List<Explanation> featureExplanations) {+ public Explanation explain(+ LeafReaderContext context, int doc, float finalScore, List<Explanation> featureExplanations) {final StringBuilder modelDescription = new StringBuilder();@@ -329,5 +380,4 @@ public class NeuralNetworkModel extends LTRScoringModel {return Explanation.match(finalScore, modelDescription.toString());}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/WrapperModel.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/WrapperModel.javaindex e143d3b6e36..19ff7c9b20d 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/WrapperModel.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/WrapperModel.java@@ -20,7 +20,6 @@ package org.apache.solr.ltr.model;import java.util.Collection;import java.util.List;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.Explanation;import org.apache.solr.ltr.feature.Feature;@@ -29,26 +28,28 @@ import org.apache.solr.ltr.norm.Normalizer;/*** A scoring model that wraps the other model.*- * <p>This model loads a model from an external resource during the initialization.- * The way of fetching the wrapped model is depended on- * the implementation of {@link WrapperModel#fetchModelMap()}.+ * <p>This model loads a model from an external resource during the initialization. The way of+ * fetching the wrapped model is depended on the implementation of {@link+ * WrapperModel#fetchModelMap()}.*- * <p>This model doesn't hold the actual parameters of the wrapped model,- * thus it can manage large models which are difficult to upload to ZooKeeper.+ * <p>This model doesn't hold the actual parameters of the wrapped model, thus it can manage large+ * models which are difficult to upload to ZooKeeper.** <p>Example configuration:- * <pre>{- "class": "...",- "name": "myModelName",- "params": {- ...- }- }</pre>*- * <p>NOTE: no "features" are configured in the wrapper model- * because the wrapped model's features will be used instead.- * Also note that if a "store" is configured for the wrapper- * model then it must match the "store" of the wrapped model.+ * <pre>+ * {+ * "class": "...",+ * "name": "myModelName",+ * "params": {+ * ...+ * }+ * }+ * </pre>+ *+ * <p>NOTE: no "features" are configured in the wrapper model because the wrapped model's features+ * will be used instead. Also note that if a "store" is configured for the wrapper model then it+ * must match the "store" of the wrapped model.*/public abstract class WrapperModel extends AdapterModel {@@ -78,8 +79,13 @@ public abstract class WrapperModel extends AdapterModel {return true;}- public WrapperModel(String name, List<Feature> features, List<Normalizer> norms, String featureStoreName,- List<Feature> allFeatures, Map<String, Object> params) {+ public WrapperModel(+ String name,+ List<Feature> features,+ List<Normalizer> norms,+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {super(name, features, norms, featureStoreName, allFeatures, params);}@@ -97,10 +103,16 @@ public abstract class WrapperModel extends AdapterModel {model.validate();// check feature store names matchfinal String wrappedFeatureStoreName = model.getFeatureStoreName();- if (wrappedFeatureStoreName == null || !wrappedFeatureStoreName.equals(this.getFeatureStoreName())) {- throw new ModelException("wrapper feature store name ("+this.getFeatureStoreName() +")"- + " must match the "- + "wrapped feature store name ("+wrappedFeatureStoreName+")");+ if (wrappedFeatureStoreName == null+ || !wrappedFeatureStoreName.equals(this.getFeatureStoreName())) {+ throw new ModelException(+ "wrapper feature store name ("+ + this.getFeatureStoreName()+ + ")"+ + " must match the "+ + "wrapped feature store name ("+ + wrappedFeatureStoreName+ + ")");}}}@@ -141,8 +153,8 @@ public abstract class WrapperModel extends AdapterModel {}@Override- public Explanation explain(LeafReaderContext context, int doc, float finalScore,- List<Explanation> featureExplanations) {+ public Explanation explain(+ LeafReaderContext context, int doc, float finalScore, List<Explanation> featureExplanations) {return model.explain(context, doc, finalScore, featureExplanations);}@@ -164,5 +176,4 @@ public abstract class WrapperModel extends AdapterModel {return sb.toString();}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/package-info.javaindex 32bd626f95b..4e8f26c4d53 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/model/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/model/package-info.java@@ -15,7 +15,5 @@* limitations under the License.*/-/**- * Contains Model related classes- */+/** Contains Model related classes */package org.apache.solr.ltr.model;diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/IdentityNormalizer.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/IdentityNormalizer.javaindex a3d1026064f..a5c9b4b7bbe 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/IdentityNormalizer.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/IdentityNormalizer.java@@ -19,17 +19,14 @@ package org.apache.solr.ltr.norm;import java.util.LinkedHashMap;/**- * A Normalizer that normalizes a feature value to itself. This is the- * default normalizer class, if no normalizer is configured then the- * IdentityNormalizer will be used.+ * A Normalizer that normalizes a feature value to itself. This is the default normalizer class, if+ * no normalizer is configured then the IdentityNormalizer will be used.*/public class IdentityNormalizer extends Normalizer {public static final IdentityNormalizer INSTANCE = new IdentityNormalizer();- public IdentityNormalizer() {-- }+ public IdentityNormalizer() {}@Overridepublic float normalize(float value) {@@ -37,17 +34,15 @@ public class IdentityNormalizer extends Normalizer {}@Override- public LinkedHashMap<String,Object> paramsToMap() {+ public LinkedHashMap<String, Object> paramsToMap() {return null;}@Override- protected void validate() throws NormalizerException {- }+ protected void validate() throws NormalizerException {}@Overridepublic String toString() {return getClass().getSimpleName();}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/MinMaxNormalizer.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/MinMaxNormalizer.javaindex f6322fefb39..4754d3d5526 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/MinMaxNormalizer.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/MinMaxNormalizer.java@@ -20,19 +20,22 @@ import java.util.LinkedHashMap;/*** A Normalizer to scale a feature value using a (min,max) range.- * <p>- * Example configuration:-<pre>-"norm" : {- "class" : "org.apache.solr.ltr.norm.MinMaxNormalizer",- "params" : { "min":"0", "max":"50" }-}-</pre>+ *+ * <p>Example configuration:+ *+ * <pre>+ * "norm" : {+ * "class" : "org.apache.solr.ltr.norm.MinMaxNormalizer",+ * "params" : { "min":"0", "max":"50" }+ * }+ * </pre>+ ** Example normalizations:+ ** <ul>- * <li>-5 will be normalized to -0.1- * <li>55 will be normalized to 1.1- * <li>+5 will be normalized to +0.1+ * <li>-5 will be normalized to -0.1+ * <li>55 will be normalized to 1.1+ * <li>+5 will be normalized to +0.1* </ul>*/public class MinMaxNormalizer extends Normalizer {@@ -76,9 +79,14 @@ public class MinMaxNormalizer extends Normalizer {@Overrideprotected void validate() throws NormalizerException {if (delta == 0f) {- throw- new NormalizerException("MinMax Normalizer delta must not be zero " +- "| min = " + min + ",max = " + max + ",delta = " + delta);+ throw new NormalizerException(+ "MinMax Normalizer delta must not be zero "+ + "| min = "+ + min+ + ",max = "+ + max+ + ",delta = "+ + delta);}}@@ -88,8 +96,8 @@ public class MinMaxNormalizer extends Normalizer {}@Override- public LinkedHashMap<String,Object> paramsToMap() {- final LinkedHashMap<String,Object> params = new LinkedHashMap<>(2, 1.0f);+ public LinkedHashMap<String, Object> paramsToMap() {+ final LinkedHashMap<String, Object> params = new LinkedHashMap<>(2, 1.0f);params.put("min", Float.toString(min));params.put("max", Float.toString(max));return params;@@ -103,5 +111,4 @@ public class MinMaxNormalizer extends Normalizer {sb.append(",max=").append(max).append(')');return sb.toString();}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/Normalizer.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/Normalizer.javaindex b4751732b6d..6e966750182 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/Normalizer.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/Normalizer.java@@ -18,22 +18,20 @@ package org.apache.solr.ltr.norm;import java.util.LinkedHashMap;import java.util.Map;-import org.apache.lucene.search.Explanation;import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.util.SolrPluginUtils;/**- * A normalizer normalizes the value of a feature. After the feature values- * have been computed, the {@link Normalizer#normalize(float)} methods will- * be called and the resulting values will be used by the model.+ * A normalizer normalizes the value of a feature. After the feature values have been computed, the+ * {@link Normalizer#normalize(float)} methods will be called and the resulting values will be used+ * by the model.*/public abstract class Normalizer {-public abstract float normalize(float value);- public abstract LinkedHashMap<String,Object> paramsToMap();+ public abstract LinkedHashMap<String, Object> paramsToMap();public Explanation explain(Explanation explain) {final float normalized = normalize(explain.getValue().floatValue());@@ -42,8 +40,8 @@ public abstract class Normalizer {return Explanation.match(normalized, explainDesc, explain);}- public static Normalizer getInstance(SolrResourceLoader solrResourceLoader,- String className, Map<String,Object> params) {+ public static Normalizer getInstance(+ SolrResourceLoader solrResourceLoader, String className, Map<String, Object> params) {final Normalizer f = solrResourceLoader.newInstance(className, Normalizer.class);if (params != null) {SolrPluginUtils.invokeSetters(f, params.entrySet());@@ -53,12 +51,10 @@ public abstract class Normalizer {}/**- * As part of creation of a normalizer instance, this function confirms- * that the normalizer parameters are valid.+ * As part of creation of a normalizer instance, this function confirms that the normalizer+ * parameters are valid.*- * @throws NormalizerException- * Normalizer Exception+ * @throws NormalizerException Normalizer Exception*/protected abstract void validate() throws NormalizerException;-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/NormalizerException.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/NormalizerException.javaindex 5b33f05a7a9..091b4287247 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/NormalizerException.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/NormalizerException.java@@ -27,5 +27,4 @@ public class NormalizerException extends RuntimeException {public NormalizerException(String message, Exception cause) {super(message, cause);}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/StandardNormalizer.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/StandardNormalizer.javaindex d908ea941ec..2c17822242d 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/StandardNormalizer.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/StandardNormalizer.java@@ -20,20 +20,22 @@ import java.util.LinkedHashMap;/*** A Normalizer to scale a feature value around an average-and-standard-deviation distribution.- * <p>- * Example configuration:-<pre>-"norm" : {- "class" : "org.apache.solr.ltr.norm.StandardNormalizer",- "params" : { "avg":"42", "std":"6" }-}-</pre>- * <p>- * Example normalizations:+ *+ * <p>Example configuration:+ *+ * <pre>+ * "norm" : {+ * "class" : "org.apache.solr.ltr.norm.StandardNormalizer",+ * "params" : { "avg":"42", "std":"6" }+ * }+ * </pre>+ *+ * <p>Example normalizations:+ ** <ul>- * <li>39 will be normalized to -0.5- * <li>42 will be normalized to 0- * <li>45 will be normalized to +0.5+ * <li>39 will be normalized to -0.5+ * <li>42 will be normalized to 0+ * <li>45 will be normalized to +0.5* </ul>*/public class StandardNormalizer extends Normalizer {@@ -73,15 +75,18 @@ public class StandardNormalizer extends Normalizer {@Overrideprotected void validate() throws NormalizerException {if (std <= 0f) {- throw- new NormalizerException("Standard Normalizer standard deviation must "- + "be positive | avg = " + avg + ",std = " + std);+ throw new NormalizerException(+ "Standard Normalizer standard deviation must "+ + "be positive | avg = "+ + avg+ + ",std = "+ + std);}}@Override- public LinkedHashMap<String,Object> paramsToMap() {- final LinkedHashMap<String,Object> params = new LinkedHashMap<>(2, 1.0f);+ public LinkedHashMap<String, Object> paramsToMap() {+ final LinkedHashMap<String, Object> params = new LinkedHashMap<>(2, 1.0f);params.put("avg", Float.toString(avg));params.put("std", Float.toString(std));return params;@@ -95,5 +100,4 @@ public class StandardNormalizer extends Normalizer {sb.append(",std=").append(avg).append(')');return sb.toString();}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/package-info.javaindex 164b425df52..dfdd0f513c5 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/norm/package-info.java@@ -16,8 +16,7 @@*//**- * A normalizer normalizes the value of a feature. Once that the feature values- * will be computed, the normalizer will be applied and the resulting values- * will be received by the model.+ * A normalizer normalizes the value of a feature. Once that the feature values will be computed,+ * the normalizer will be applied and the resulting values will be received by the model.*/package org.apache.solr.ltr.norm;diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/package-info.javaindex 59aebe83f2e..7266339ae34 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/package-info.java@@ -16,30 +16,24 @@*//**- * <p>- * This package contains the main logic for performing the reranking using- * a Learning to Rank model.- * </p>- * <p>- * A model will be applied on each document through a {@link org.apache.solr.ltr.LTRScoringQuery}, a- * subclass of {@link org.apache.lucene.search.Query}. As a normal query,- * the learned model will produce a new score- * for each document reranked.- * </p>- * <p>- * A {@link org.apache.solr.ltr.LTRScoringQuery} is created by providing an instance of- * {@link org.apache.solr.ltr.model.LTRScoringModel}. An instance of- * {@link org.apache.solr.ltr.model.LTRScoringModel}- * defines how to combine the features in order to create a new- * score for a document. A new Learning to Rank model is plugged- * into the framework by extending {@link org.apache.solr.ltr.model.LTRScoringModel},- * (see for example {@link org.apache.solr.ltr.model.MultipleAdditiveTreesModel} and {@link org.apache.solr.ltr.model.LinearModel}).- * </p>- * <p>- * The {@link org.apache.solr.ltr.LTRScoringQuery} will take care of computing the values of- * all the features (see {@link org.apache.solr.ltr.feature.Feature}) and then will delegate the final score- * generation to the {@link org.apache.solr.ltr.model.LTRScoringModel}, by calling the method- * {@link org.apache.solr.ltr.model.LTRScoringModel#score(float[] modelFeatureValuesNormalized) score(float[] modelFeatureValuesNormalized)}.- * </p>+ * This package contains the main logic for performing the reranking using a Learning to Rank model.+ *+ * <p>A model will be applied on each document through a {@link+ * org.apache.solr.ltr.LTRScoringQuery}, a subclass of {@link org.apache.lucene.search.Query}. As a+ * normal query, the learned model will produce a new score for each document reranked.+ *+ * <p>A {@link org.apache.solr.ltr.LTRScoringQuery} is created by providing an instance of {@link+ * org.apache.solr.ltr.model.LTRScoringModel}. An instance of {@link+ * org.apache.solr.ltr.model.LTRScoringModel} defines how to combine the features in order to create+ * a new score for a document. A new Learning to Rank model is plugged into the framework by+ * extending {@link org.apache.solr.ltr.model.LTRScoringModel}, (see for example {@link+ * org.apache.solr.ltr.model.MultipleAdditiveTreesModel} and {@link+ * org.apache.solr.ltr.model.LinearModel}).+ *+ * <p>The {@link org.apache.solr.ltr.LTRScoringQuery} will take care of computing the values of all+ * the features (see {@link org.apache.solr.ltr.feature.Feature}) and then will delegate the final+ * score generation to the {@link org.apache.solr.ltr.model.LTRScoringModel}, by calling the method+ * {@link org.apache.solr.ltr.model.LTRScoringModel#score(float[] modelFeatureValuesNormalized)+ * score(float[] modelFeatureValuesNormalized)}.*/package org.apache.solr.ltr;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.javaindex 04f4980d655..9b5594bbe0b 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@@ -21,7 +21,6 @@ import java.util.Collections;import java.util.List;import java.util.Locale;import java.util.Map;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.Explanation;import org.apache.lucene.search.ScoreMode;@@ -51,18 +50,19 @@ import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.util.SolrPluginUtils;/**- * This transformer will take care to generate and append in the response the- * features declared in the feature store of the current reranking model,- * or a specified feature store. Ex. <code>fl=id,[features store=myStore efi.user_text="ibm"]</code>- *+ * This transformer will take care to generate and append in the response the features declared in+ * the feature store of the current reranking model, or a specified feature store. Ex. <code>+ * fl=id,[features store=myStore efi.user_text="ibm"]</code>+ ** <h2>Parameters</h2>- * <code>store</code> - The feature store to extract features from. If not provided it- * will default to the features used by your reranking model.<br>- * <code>efi.*</code> - External feature information variables required by the features- * you are extracting.<br>- * <code>format</code> - The format you want the features to be returned in. Supports (dense|sparse). Defaults to dense.<br>-*/-+ *+ * <code>store</code> - The feature store to extract features from. If not provided it will default+ * to the features used by your reranking model.<br>+ * <code>efi.*</code> - External feature information variables required by the features you are+ * extracting.<br>+ * <code>format</code> - The format you want the features to be returned in. Supports+ * (dense|sparse). Defaults to dense.<br>+ */public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {// used inside fl to specify the format (dense|sparse) of the extracted features@@ -95,7 +95,8 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {}public void setDefaultFormat(String defaultFormat) {- this.defaultFormat = FeatureLogger.FeatureFormat.valueOf(defaultFormat.toUpperCase(Locale.ROOT));+ this.defaultFormat =+ FeatureLogger.FeatureFormat.valueOf(defaultFormat.toUpperCase(Locale.ROOT));}public void setCsvKeyValueDelimiter(String csvKeyValueDelimiter) {@@ -120,31 +121,29 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {}@Override- public DocTransformer create(String name, SolrParams localparams,- SolrQueryRequest req) {+ public DocTransformer create(String name, SolrParams localparams, SolrQueryRequest req) {// Hint to enable feature vector cache since we are requesting featuresSolrQueryRequestContextUtils.setIsExtractingFeatures(req);// Communicate which feature store we are requesting features forfinal String fvStoreName = localparams.get(FV_STORE);- SolrQueryRequestContextUtils.setFvStoreName(req, (fvStoreName == null ? defaultStore : fvStoreName));+ SolrQueryRequestContextUtils.setFvStoreName(+ req, (fvStoreName == null ? defaultStore : fvStoreName));// Create and supply the feature logger to be used- SolrQueryRequestContextUtils.setFeatureLogger(req,- createFeatureLogger(- localparams.get(FV_FORMAT)));+ SolrQueryRequestContextUtils.setFeatureLogger(+ req, createFeatureLogger(localparams.get(FV_FORMAT)));- return new FeatureTransformer(name, localparams, req, (fvStoreName != null) /* hasExplicitFeatureStore */);+ return new FeatureTransformer(+ name, localparams, req, (fvStoreName != null) /* hasExplicitFeatureStore */);}/**- * returns a FeatureLogger that logs the features- * 'featureFormat' param: 'dense' will write features in dense format,- * 'sparse' will write the features in sparse format, null or empty will+ * returns a FeatureLogger that logs the features 'featureFormat' param: 'dense' will write+ * features in dense format, 'sparse' will write the features in sparse format, null or empty will* default to 'sparse'*- ** @return a feature logger for the format specified.*/private FeatureLogger createFeatureLogger(String formatStr) {@@ -162,31 +161,35 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {class FeatureTransformer extends DocTransformer {- final private String name;- final private SolrParams localparams;- final private SolrQueryRequest req;- final private boolean hasExplicitFeatureStore;+ private final String name;+ private final SolrParams localparams;+ private final SolrQueryRequest req;+ private final boolean hasExplicitFeatureStore;private List<LeafReaderContext> leafContexts;private SolrIndexSearcher searcher;+/*** rerankingQueries, modelWeights have:- * length=1 - [Classic LTR] When reranking with a single model- * length=2 - [Interleaving] When reranking with interleaving (two ranking models are involved)+ *+ * <p>length=1 - [Classic LTR] When reranking with a single model+ *+ * <p>length=2 - [Interleaving] When reranking with interleaving (two ranking models are+ * involved)*/private LTRScoringQuery[] rerankingQueriesFromContext;+private LTRScoringQuery[] rerankingQueries;private LTRScoringQuery.ModelWeight[] modelWeights;private FeatureLogger featureLogger;private boolean docsWereNotReranked;- /**- * @param name- * Name of the field to be added in a document representing the- * feature vectors- */- public FeatureTransformer(String name, SolrParams localparams,- SolrQueryRequest req, boolean hasExplicitFeatureStore) {+ /** @param name Name of the field to be added in a document representing the feature vectors */+ public FeatureTransformer(+ String name,+ SolrParams localparams,+ SolrQueryRequest req,+ boolean hasExplicitFeatureStore) {this.name = name;this.localparams = localparams;this.req = req;@@ -210,89 +213,111 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {searcher = context.getSearcher();if (searcher == null) {- throw new SolrException(- SolrException.ErrorCode.BAD_REQUEST,- "searcher is null");+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "searcher is null");}leafContexts = searcher.getTopReaderContext().leaves();if (threadManager != null) {- threadManager.setExecutor(context.getRequest().getCoreContainer().getUpdateShardHandler().getUpdateExecutor());+ threadManager.setExecutor(+ context.getRequest().getCoreContainer().getUpdateShardHandler().getUpdateExecutor());}rerankingQueriesFromContext = SolrQueryRequestContextUtils.getScoringQueries(req);- docsWereNotReranked = (rerankingQueriesFromContext == null || rerankingQueriesFromContext.length == 0);+ docsWereNotReranked =+ (rerankingQueriesFromContext == null || rerankingQueriesFromContext.length == 0);String transformerFeatureStore = SolrQueryRequestContextUtils.getFvStoreName(req);- Map<String, String[]> transformerExternalFeatureInfo = LTRQParserPlugin.extractEFIParams(localparams);+ Map<String, String[]> transformerExternalFeatureInfo =+ LTRQParserPlugin.extractEFIParams(localparams);final LoggingModel loggingModel = createLoggingModel(transformerFeatureStore);- setupRerankingQueriesForLogging(transformerFeatureStore, transformerExternalFeatureInfo, loggingModel);+ setupRerankingQueriesForLogging(+ transformerFeatureStore, transformerExternalFeatureInfo, loggingModel);setupRerankingWeightsForLogging(context);}/**- * The loggingModel is an empty model that is just used to extract the features- * and log them+ * The loggingModel is an empty model that is just used to extract the features and log them+ ** @param transformerFeatureStore the explicit transformer feature store*/private LoggingModel createLoggingModel(String transformerFeatureStore) {final ManagedFeatureStore fr = ManagedFeatureStore.getManagedFeatureStore(req.getCore());final FeatureStore store = fr.getFeatureStore(transformerFeatureStore);- transformerFeatureStore = store.getName(); // if transformerFeatureStore was null before this gets actual name- return new LoggingModel(loggingModelName,- transformerFeatureStore, store.getFeatures());+ // if transformerFeatureStore was null before this gets actual name+ transformerFeatureStore = store.getName();++ return new LoggingModel(loggingModelName, transformerFeatureStore, store.getFeatures());}/*** When preparing the reranking queries for logging features various scenarios apply:- *- * No Reranking- * There is the need of a logger model from the default feature store or the explicit feature store passed- * to extract the feature vector- *- * Re Ranking- * 1) If no explicit feature store is passed, the models for each reranking query can be safely re-used- * the feature vector can be fetched from the feature vector cache.- * 2) If an explicit feature store is passed, and no reranking query uses a model with that feature store,- * There is the need of a logger model to extract the feature vector- * 3) If an explicit feature store is passed, and there is a reranking query that uses a model with that feature store,- * the model can be re-used and there is no need for a logging model+ *+ * <p>No Reranking+ *+ * <p>There is the need of a logger model from the default feature store or the explicit feature+ * store passed to extract the feature vector+ *+ * <p>Re Ranking+ *+ * <p>1) If no explicit feature store is passed, the models for each reranking query can be+ * safely re-used the feature vector can be fetched from the feature vector cache.+ *+ * <p>2) If an explicit feature store is passed, and no reranking query uses a model with that+ * feature store, There is the need of a logger model to extract the feature vector+ *+ * <p>3) If an explicit feature store is passed, and there is a reranking query that uses a+ * model with that feature store, the model can be re-used and there is no need for a logging+ * model** @param transformerFeatureStore explicit feature store for the transformer* @param transformerExternalFeatureInfo explicit efi for the transformer*/- private void setupRerankingQueriesForLogging(String transformerFeatureStore, Map<String, String[]> transformerExternalFeatureInfo, LoggingModel loggingModel) {- if (docsWereNotReranked) { //no reranking query- LTRScoringQuery loggingQuery = new LTRScoringQuery(loggingModel,- transformerExternalFeatureInfo,- true /* extractAllFeatures */,- threadManager);- rerankingQueries = new LTRScoringQuery[]{loggingQuery};+ private void setupRerankingQueriesForLogging(+ String transformerFeatureStore,+ Map<String, String[]> transformerExternalFeatureInfo,+ LoggingModel loggingModel) {+ if (docsWereNotReranked) { // no reranking query+ LTRScoringQuery loggingQuery =+ new LTRScoringQuery(+ loggingModel,+ transformerExternalFeatureInfo,+ true /* extractAllFeatures */,+ threadManager);+ rerankingQueries = new LTRScoringQuery[] {loggingQuery};} else {rerankingQueries = new LTRScoringQuery[rerankingQueriesFromContext.length];- System.arraycopy(rerankingQueriesFromContext, 0, rerankingQueries, 0, rerankingQueriesFromContext.length);-- if (transformerFeatureStore != null) {// explicit feature store for the transformer+ System.arraycopy(+ rerankingQueriesFromContext,+ 0,+ rerankingQueries,+ 0,+ rerankingQueriesFromContext.length);++ if (transformerFeatureStore != null) { // explicit feature store for the transformerLTRScoringModel matchingRerankingModel = loggingModel;for (LTRScoringQuery rerankingQuery : rerankingQueries) {- if (!(rerankingQuery instanceof OriginalRankingLTRScoringQuery) &&- transformerFeatureStore.equals(rerankingQuery.getScoringModel().getFeatureStoreName())) {+ if (!(rerankingQuery instanceof OriginalRankingLTRScoringQuery)+ && transformerFeatureStore.equals(+ rerankingQuery.getScoringModel().getFeatureStoreName())) {matchingRerankingModel = rerankingQuery.getScoringModel();}}for (int i = 0; i < rerankingQueries.length; i++) {- rerankingQueries[i] = new LTRScoringQuery(- matchingRerankingModel,- (!transformerExternalFeatureInfo.isEmpty() ? transformerExternalFeatureInfo : rerankingQueries[i].getExternalFeatureInfo()),- true /* extractAllFeatures */,- threadManager);+ rerankingQueries[i] =+ new LTRScoringQuery(+ matchingRerankingModel,+ (!transformerExternalFeatureInfo.isEmpty()+ ? transformerExternalFeatureInfo+ : rerankingQueries[i].getExternalFeatureInfo()),+ true /* extractAllFeatures */,+ threadManager);}}}}-+private void setupRerankingWeightsForLogging(ResultContext context) {modelWeights = new LTRScoringQuery.ModelWeight[rerankingQueries.length];for (int i = 0; i < rerankingQueries.length; i++) {@@ -300,9 +325,11 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {rerankingQueries[i].setOriginalQuery(context.getQuery());}rerankingQueries[i].setRequest(req);- if (!(rerankingQueries[i] instanceof OriginalRankingLTRScoringQuery) || hasExplicitFeatureStore) {+ if (!(rerankingQueries[i] instanceof OriginalRankingLTRScoringQuery)+ || hasExplicitFeatureStore) {if (rerankingQueries[i].getFeatureLogger() == null) {- rerankingQueries[i].setFeatureLogger(SolrQueryRequestContextUtils.getFeatureLogger(req));+ rerankingQueries[i].setFeatureLogger(+ SolrQueryRequestContextUtils.getFeatureLogger(req));}featureLogger = rerankingQueries[i].getFeatureLogger();try {@@ -311,31 +338,31 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e.getMessage(), e);}if (modelWeights[i] == null) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST,"error logging the features, model weight is null");}}}}-+@Override- public void transform(SolrDocument doc, int docid, float score)- throws IOException {+ public void transform(SolrDocument doc, int docid, float score) throws IOException {implTransform(doc, docid, score);}@Override- public void transform(SolrDocument doc, int docid)- throws IOException {+ public void transform(SolrDocument doc, int docid) throws IOException {implTransform(doc, docid, null);}- private void implTransform(SolrDocument doc, int docid, Float score)- throws IOException {+ private void implTransform(SolrDocument doc, int docid, Float score) throws IOException {LTRScoringQuery rerankingQuery = rerankingQueries[0];LTRScoringQuery.ModelWeight rerankingModelWeight = modelWeights[0];for (int i = 1; i < rerankingQueries.length; i++) {- if (((LTRInterleavingScoringQuery)rerankingQueriesFromContext[i]).getPickedInterleavingDocIds().contains(docid)) {+ if (((LTRInterleavingScoringQuery) rerankingQueriesFromContext[i])+ .getPickedInterleavingDocIds()+ .contains(docid)) {rerankingQuery = rerankingQueries[i];rerankingModelWeight = modelWeights[i];}@@ -343,29 +370,38 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {if (!(rerankingQuery instanceof OriginalRankingLTRScoringQuery) || hasExplicitFeatureStore) {Object featureVector = featureLogger.getFeatureVector(docid, rerankingQuery, searcher);if (featureVector == null) { // FV for this document was not in the cache- featureVector = featureLogger.makeFeatureVector(- LTRRescorer.extractFeaturesInfo(- rerankingModelWeight,- docid,- (docsWereNotReranked ? score : null),- leafContexts));+ featureVector =+ featureLogger.makeFeatureVector(+ LTRRescorer.extractFeaturesInfo(+ rerankingModelWeight,+ docid,+ (docsWereNotReranked ? score : null),+ leafContexts));}doc.addField(name, featureVector);}}-}private static class LoggingModel extends LTRScoringModel {- public LoggingModel(String name, String featureStoreName, List<Feature> allFeatures){- this(name, Collections.emptyList(), Collections.emptyList(),- featureStoreName, allFeatures, Collections.emptyMap());+ public LoggingModel(String name, String featureStoreName, List<Feature> allFeatures) {+ this(+ name,+ Collections.emptyList(),+ Collections.emptyList(),+ featureStoreName,+ allFeatures,+ Collections.emptyMap());}- protected LoggingModel(String name, List<Feature> features,- List<Normalizer> norms, String featureStoreName,- List<Feature> allFeatures, Map<String,Object> params) {+ protected LoggingModel(+ String name,+ List<Feature> features,+ List<Normalizer> norms,+ String featureStoreName,+ List<Feature> allFeatures,+ Map<String, Object> params) {super(name, features, norms, featureStoreName, allFeatures, params);}@@ -375,11 +411,13 @@ public class LTRFeatureLoggerTransformerFactory extends TransformerFactory {}@Override- public Explanation explain(LeafReaderContext context, int doc, float finalScore, List<Explanation> featureExplanations) {- return Explanation.match(finalScore, toString()- + " logging model, used only for logging the features");+ public Explanation explain(+ LeafReaderContext context,+ int doc,+ float finalScore,+ List<Explanation> featureExplanations) {+ return Explanation.match(+ finalScore, toString() + " logging model, used only for logging the features");}-}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRInterleavingTransformerFactory.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRInterleavingTransformerFactory.javaindex bffa0b3be3c..91a59e6cc3d 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRInterleavingTransformerFactory.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/LTRInterleavingTransformerFactory.java@@ -20,9 +20,9 @@ import java.io.IOException;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;-import org.apache.solr.ltr.interleaving.LTRInterleavingScoringQuery;import org.apache.solr.ltr.LTRScoringQuery;import org.apache.solr.ltr.SolrQueryRequestContextUtils;+import org.apache.solr.ltr.interleaving.LTRInterleavingScoringQuery;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.ResultContext;import org.apache.solr.response.transform.DocTransformer;@@ -30,7 +30,7 @@ import org.apache.solr.response.transform.TransformerFactory;import org.apache.solr.util.SolrPluginUtils;public class LTRInterleavingTransformerFactory extends TransformerFactory {-+@Overridepublic void init(NamedList<?> args) {super.init(args);@@ -38,25 +38,22 @@ public class LTRInterleavingTransformerFactory extends TransformerFactory {}@Override- public DocTransformer create(String name, SolrParams localparams,- SolrQueryRequest req) {+ public DocTransformer create(String name, SolrParams localparams, SolrQueryRequest req) {return new InterleavingTransformer(name, req);}-+class InterleavingTransformer extends DocTransformer {- final private String name;- final private SolrQueryRequest req;-+ private final String name;+ private final SolrQueryRequest req;+private LTRInterleavingScoringQuery[] rerankingQueries;/**- * @param name- * Name of the field to be added in a document representing the- * model picked by the interleaving process+ * @param name Name of the field to be added in a document representing the model picked by the+ * interleaving process*/- public InterleavingTransformer(String name,- SolrQueryRequest req) {+ public InterleavingTransformer(String name, SolrQueryRequest req) {this.name = name;this.req = req;}@@ -75,7 +72,8 @@ public class LTRInterleavingTransformerFactory extends TransformerFactory {if (context.getRequest() == null) {return;}- rerankingQueries = (LTRInterleavingScoringQuery[])SolrQueryRequestContextUtils.getScoringQueries(req);+ rerankingQueries =+ (LTRInterleavingScoringQuery[]) SolrQueryRequestContextUtils.getScoringQueries(req);for (int i = 0; i < rerankingQueries.length; i++) {LTRScoringQuery scoringQuery = rerankingQueries[i];@@ -83,31 +81,29 @@ public class LTRInterleavingTransformerFactory extends TransformerFactory {scoringQuery.setOriginalQuery(context.getQuery());}if (scoringQuery.getFeatureLogger() == null) {- scoringQuery.setFeatureLogger( SolrQueryRequestContextUtils.getFeatureLogger(req) );+ scoringQuery.setFeatureLogger(SolrQueryRequestContextUtils.getFeatureLogger(req));}scoringQuery.setRequest(req);}}@Override- public void transform(SolrDocument doc, int docid, float score)- throws IOException {+ public void transform(SolrDocument doc, int docid, float score) throws IOException {implTransform(doc, docid);}@Override- public void transform(SolrDocument doc, int docid)- throws IOException {+ public void transform(SolrDocument doc, int docid) throws IOException {implTransform(doc, docid);}private void implTransform(SolrDocument doc, int docid) {LTRScoringQuery rerankingQuery = rerankingQueries[0];- if (rerankingQueries.length > 1 && rerankingQueries[1].getPickedInterleavingDocIds().contains(docid)) {+ if (rerankingQueries.length > 1+ && rerankingQueries[1].getPickedInterleavingDocIds().contains(docid)) {rerankingQuery = rerankingQueries[1];}doc.addField(name, rerankingQuery.getScoringModelName());}}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/package-info.javaindex a9461355f85..0834f0ff105 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/response/transform/package-info.java@@ -16,8 +16,7 @@*//**- * APIs and implementations of {@link org.apache.solr.response.transform.DocTransformer} for modifying documents in Solr request responses+ * APIs and implementations of {@link org.apache.solr.response.transform.DocTransformer} for+ * modifying documents in Solr request responses*/package org.apache.solr.ltr.response.transform;--diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQParserPlugin.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQParserPlugin.javaindex 592f94c1783..7006b110aab 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQParserPlugin.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQParserPlugin.java@@ -20,21 +20,20 @@ import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;-+import org.apache.lucene.search.Query;import org.apache.lucene.util.ResourceLoader;import org.apache.lucene.util.ResourceLoaderAware;-import org.apache.lucene.search.Query;import org.apache.solr.common.SolrException;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrResourceLoader;-import org.apache.solr.ltr.interleaving.LTRInterleavingScoringQuery;-import org.apache.solr.ltr.interleaving.OriginalRankingLTRScoringQuery;import org.apache.solr.ltr.LTRScoringQuery;import org.apache.solr.ltr.LTRThreadModule;import org.apache.solr.ltr.SolrQueryRequestContextUtils;import org.apache.solr.ltr.interleaving.Interleaving;import org.apache.solr.ltr.interleaving.LTRInterleavingQuery;+import org.apache.solr.ltr.interleaving.LTRInterleavingScoringQuery;+import org.apache.solr.ltr.interleaving.OriginalRankingLTRScoringQuery;import org.apache.solr.ltr.model.LTRScoringModel;import org.apache.solr.ltr.store.rest.ManagedFeatureStore;import org.apache.solr.ltr.store.rest.ManagedModelStore;@@ -49,11 +48,11 @@ import org.apache.solr.util.SolrPluginUtils;/*** Plug into solr a rerank model.*- * Learning to Rank Query Parser Syntax: rq={!ltr model=6029760550880411648 reRankDocs=300+ * <p>Learning to Rank Query Parser Syntax: rq={!ltr model=6029760550880411648 reRankDocs=300* efi.myCompanyQueryIntent=0.98}- **/-public class LTRQParserPlugin extends QParserPlugin implements ResourceLoaderAware, ManagedResourceObserver {+public class LTRQParserPlugin extends QParserPlugin+ implements ResourceLoaderAware, ManagedResourceObserver {public static final String NAME = "ltr";private static final String ORIGINAL_RANKING = "_OriginalRanking_";@@ -66,25 +65,21 @@ public class LTRQParserPlugin extends QParserPlugin implements ResourceLoaderAwaprivate LTRThreadModule threadManager = null;- /** query parser plugin: the name of the attribute for setting the model **/+ /** query parser plugin: the name of the attribute for setting the model */public static final String MODEL = "model";- /** query parser plugin: default number of documents to rerank **/+ /** query parser plugin: default number of documents to rerank */public static final int DEFAULT_RERANK_DOCS = 200;- /**- * query parser plugin:the param that will select how the number of document- * to rerank- **/+ /** query parser plugin:the param that will select how the number of document to rerank */public static final String RERANK_DOCS = "reRankDocs";- /** query parser plugin: default interleaving algorithm **/+ /** query parser plugin: default interleaving algorithm */public static final String DEFAULT_INTERLEAVING_ALGORITHM = Interleaving.TEAM_DRAFT;- /** query parser plugin:the param that selects the interleaving algorithm to use **/+ /** query parser plugin:the param that selects the interleaving algorithm to use */public static final String INTERLEAVING_ALGORITHM = "interleavingAlgorithm";-@Overridepublic void init(NamedList<?> args) {super.init(args);@@ -93,32 +88,30 @@ public class LTRQParserPlugin extends QParserPlugin implements ResourceLoaderAwa}@Override- public QParser createParser(String qstr, SolrParams localParams,- SolrParams params, SolrQueryRequest req) {+ public QParser createParser(+ String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {return new LTRQParser(qstr, localParams, params, req);}/*** Given a set of local SolrParams, extract all of the efi.key=value params into a map+ ** @param localParams Local request parameters that might conatin efi params- * @return Map of efi params, where the key is the name of the efi param, and the- * value is the value of the efi param+ * @return Map of efi params, where the key is the name of the efi param, and the value is the+ * value of the efi param*/- public static Map<String,String[]> extractEFIParams(SolrParams localParams) {- final Map<String,String[]> externalFeatureInfo = new HashMap<>();- for (final Iterator<String> it = localParams.getParameterNamesIterator(); it- .hasNext();) {+ public static Map<String, String[]> extractEFIParams(SolrParams localParams) {+ final Map<String, String[]> externalFeatureInfo = new HashMap<>();+ for (final Iterator<String> it = localParams.getParameterNamesIterator(); it.hasNext(); ) {final String name = it.next();if (name.startsWith(EXTERNAL_FEATURE_INFO)) {externalFeatureInfo.put(- name.substring(EXTERNAL_FEATURE_INFO.length()),- new String[] {localParams.get(name)});+ name.substring(EXTERNAL_FEATURE_INFO.length()), new String[] {localParams.get(name)});}}return externalFeatureInfo;}-@Overridepublic void inform(ResourceLoader loader) throws IOException {final SolrResourceLoader solrResourceLoader = (SolrResourceLoader) loader;@@ -127,79 +120,96 @@ public class LTRQParserPlugin extends QParserPlugin implements ResourceLoaderAwa}@Override- public void onManagedResourceInitialized(NamedList<?> args, ManagedResource res) throws SolrException {+ public void onManagedResourceInitialized(NamedList<?> args, ManagedResource res)+ throws SolrException {if (res instanceof ManagedFeatureStore) {- fr = (ManagedFeatureStore)res;+ fr = (ManagedFeatureStore) res;}- if (res instanceof ManagedModelStore){- mr = (ManagedModelStore)res;+ if (res instanceof ManagedModelStore) {+ mr = (ManagedModelStore) res;}- if (mr != null && fr != null){+ if (mr != null && fr != null) {mr.setManagedFeatureStore(fr);// now we can safely load the modelsmr.loadStoredModels();-}}public class LTRQParser extends QParser {- public LTRQParser(String qstr, SolrParams localParams, SolrParams params,- SolrQueryRequest req) {+ public LTRQParser(+ String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {super(qstr, localParams, params, req);}@Overridepublic Query parse() throws SyntaxError {if (threadManager != null) {- threadManager.setExecutor(req.getCoreContainer().getUpdateShardHandler().getUpdateExecutor());+ threadManager.setExecutor(+ req.getCoreContainer().getUpdateShardHandler().getUpdateExecutor());}// ReRanking Modelfinal String[] modelNames = localParams.getParams(LTRQParserPlugin.MODEL);- if ((modelNames == null) || (modelNames.length!=1 && modelNames.length!=2)) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "Must provide one or two models in the request");+ if ((modelNames == null) || (modelNames.length != 1 && modelNames.length != 2)) {+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST, "Must provide one or two models in the request");}final boolean isInterleaving = (modelNames.length > 1);final boolean extractFeatures = SolrQueryRequestContextUtils.isExtractingFeatures(req);final String tranformerFeatureStoreName = SolrQueryRequestContextUtils.getFvStoreName(req);- final Map<String,String[]> externalFeatureInfo = extractEFIParams(localParams);+ final Map<String, String[]> externalFeatureInfo = extractEFIParams(localParams);LTRScoringQuery rerankingQuery = null;- LTRInterleavingScoringQuery[] rerankingQueries = new LTRInterleavingScoringQuery[modelNames.length];+ LTRInterleavingScoringQuery[] rerankingQueries =+ new LTRInterleavingScoringQuery[modelNames.length];for (int i = 0; i < modelNames.length; i++) {if (modelNames[i].isEmpty()) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "the " + LTRQParserPlugin.MODEL + " "+ i +" is empty");+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST,+ "the " + LTRQParserPlugin.MODEL + " " + i + " is empty");}if (!ORIGINAL_RANKING.equals(modelNames[i])) {final LTRScoringModel ltrScoringModel = mr.getModel(modelNames[i]);if (ltrScoringModel == null) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST,"cannot find " + LTRQParserPlugin.MODEL + " " + modelNames[i]);}final String modelFeatureStoreName = ltrScoringModel.getFeatureStoreName();- // Check if features are requested and if the model feature store and feature-transform feature store are the same- final boolean featuresRequestedFromSameStore = (modelFeatureStoreName.equals(tranformerFeatureStoreName) || tranformerFeatureStoreName == null) ? extractFeatures : false;-+ // Check if features are requested and if the model feature store and feature-transform+ // feature store are the same+ final boolean featuresRequestedFromSameStore =+ (modelFeatureStoreName.equals(tranformerFeatureStoreName)+ || tranformerFeatureStoreName == null)+ ? extractFeatures+ : false;+if (isInterleaving) {- rerankingQuery = rerankingQueries[i] = new LTRInterleavingScoringQuery(ltrScoringModel,- externalFeatureInfo,- featuresRequestedFromSameStore, threadManager);+ rerankingQuery =+ rerankingQueries[i] =+ new LTRInterleavingScoringQuery(+ ltrScoringModel,+ externalFeatureInfo,+ featuresRequestedFromSameStore,+ threadManager);} else {- rerankingQuery = new LTRScoringQuery(ltrScoringModel,- externalFeatureInfo,- featuresRequestedFromSameStore, threadManager);+ rerankingQuery =+ new LTRScoringQuery(+ ltrScoringModel,+ externalFeatureInfo,+ featuresRequestedFromSameStore,+ threadManager);rerankingQueries[i] = null;}// Enable the feature vector caching if we are extracting features, and the features// we requested are the same ones we are reranking withif (featuresRequestedFromSameStore) {- rerankingQuery.setFeatureLogger( SolrQueryRequestContextUtils.getFeatureLogger(req) );+ rerankingQuery.setFeatureLogger(SolrQueryRequestContextUtils.getFeatureLogger(req));}- }else{- rerankingQuery = rerankingQueries[i] = new OriginalRankingLTRScoringQuery(ORIGINAL_RANKING);+ } else {+ rerankingQuery =+ rerankingQueries[i] = new OriginalRankingLTRScoringQuery(ORIGINAL_RANKING);}// External features@@ -208,18 +218,19 @@ public class LTRQParserPlugin extends QParserPlugin implements ResourceLoaderAwaint reRankDocs = localParams.getInt(RERANK_DOCS, DEFAULT_RERANK_DOCS);if (reRankDocs <= 0) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "Must rerank at least 1 document");+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST, "Must rerank at least 1 document");}if (!isInterleaving) {- SolrQueryRequestContextUtils.setScoringQueries(req, new LTRScoringQuery[] { rerankingQuery });+ SolrQueryRequestContextUtils.setScoringQueries(req, new LTRScoringQuery[] {rerankingQuery});return new LTRQuery(rerankingQuery, reRankDocs);} else {- String interleavingAlgorithm = localParams.get(INTERLEAVING_ALGORITHM, DEFAULT_INTERLEAVING_ALGORITHM);+ String interleavingAlgorithm =+ localParams.get(INTERLEAVING_ALGORITHM, DEFAULT_INTERLEAVING_ALGORITHM);SolrQueryRequestContextUtils.setScoringQueries(req, rerankingQueries);- return new LTRInterleavingQuery(Interleaving.getImplementation(interleavingAlgorithm), rerankingQueries, reRankDocs);+ return new LTRInterleavingQuery(+ Interleaving.getImplementation(interleavingAlgorithm), rerankingQueries, reRankDocs);}}}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQuery.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQuery.javaindex e86fb7ec042..1e8edd4ed45 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQuery.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/search/LTRQuery.java@@ -18,7 +18,6 @@package org.apache.solr.ltr.search;import java.io.IOException;-import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.search.Query;import org.apache.solr.ltr.LTRRescorer;@@ -27,9 +26,9 @@ import org.apache.solr.search.AbstractReRankQuery;import org.apache.solr.search.RankQuery;/**- * A learning to rank Query, will incapsulate a learning to rank model, and delegate to it the rescoring- * of the documents.- **/+ * A learning to rank Query, will incapsulate a learning to rank model, and delegate to it the+ * rescoring of the documents.+ */public class LTRQuery extends AbstractReRankQuery {private static final Query defaultQuery = new MatchAllDocsQuery();private final LTRScoringQuery scoringQuery;@@ -55,7 +54,8 @@ public class LTRQuery extends AbstractReRankQuery {private boolean equalsTo(LTRQuery other) {return (mainQuery.equals(other.mainQuery)- && scoringQuery.equals(other.scoringQuery) && (reRankDocs == other.reRankDocs));+ && scoringQuery.equals(other.scoringQuery)+ && (reRankDocs == other.reRankDocs));}@Override@@ -69,8 +69,13 @@ public class LTRQuery extends AbstractReRankQuery {@Overridepublic String toString(String field) {- return "{!ltr mainQuery='" + mainQuery.toString() + "' scoringQuery='"- + scoringQuery.toString() + "' reRankDocs=" + reRankDocs + "}";+ return "{!ltr mainQuery='"+ + mainQuery.toString()+ + "' scoringQuery='"+ + scoringQuery.toString()+ + "' reRankDocs="+ + reRankDocs+ + "}";}@Overridediff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/search/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/search/package-info.javaindex 95b645b32f1..5461e25a6bf 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/search/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/search/package-info.java@@ -16,8 +16,7 @@*//**- * APIs and classes for {@linkplain org.apache.solr.search.QParserPlugin parsing} and {@linkplain org.apache.solr.search.SolrIndexSearcher processing} search requests+ * APIs and classes for {@linkplain org.apache.solr.search.QParserPlugin parsing} and {@linkplain+ * org.apache.solr.search.SolrIndexSearcher processing} search requests*/package org.apache.solr.ltr.search;--diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/FeatureStore.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/FeatureStore.javaindex ab2595ff38c..f554d63e37f 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/FeatureStore.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/FeatureStore.java@@ -20,16 +20,16 @@ import java.util.ArrayList;import java.util.Collections;import java.util.LinkedHashMap;import java.util.List;-import org.apache.solr.ltr.feature.Feature;import org.apache.solr.ltr.feature.FeatureException;public class FeatureStore {- /** the name of the default feature store **/+ /** the name of the default feature store */public static final String DEFAULT_FEATURE_STORE_NAME = "_DEFAULT_";- private final LinkedHashMap<String,Feature> store = new LinkedHashMap<>(); // LinkedHashMap because we need predictable iteration order+ // LinkedHashMap because we need predictable iteration order+ private final LinkedHashMap<String, Feature> store = new LinkedHashMap<>();private final String name;public FeatureStore(String name) {@@ -47,8 +47,8 @@ public class FeatureStore {public void add(Feature feature) {final String name = feature.getName();if (store.containsKey(name)) {- throw new FeatureException(name- + " already contained in the store, please use a different name");+ throw new FeatureException(+ name + " already contained in the store, please use a different name");}feature.setIndex(store.size());store.put(name, feature);@@ -63,5 +63,4 @@ public class FeatureStore {public String toString() {return "FeatureStore [features=" + store.keySet() + "]";}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/ModelStore.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/ModelStore.javaindex dbb065f6403..619d77800d6 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/ModelStore.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/ModelStore.java@@ -21,16 +21,13 @@ import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;-import org.apache.solr.ltr.model.LTRScoringModel;import org.apache.solr.ltr.model.ModelException;-/**- * Contains the model and features declared.- */+/** Contains the model and features declared. */public class ModelStore {- private final Map<String,LTRScoringModel> availableModels;+ private final Map<String, LTRScoringModel> availableModels;public ModelStore() {availableModels = new HashMap<>();@@ -59,16 +56,13 @@ public class ModelStore {return availableModels.remove(modelName);}- public synchronized void addModel(LTRScoringModel modeldata)- throws ModelException {+ public synchronized void addModel(LTRScoringModel modeldata) throws ModelException {final String name = modeldata.getName();if (availableModels.containsKey(name)) {- throw new ModelException("model '" + name- + "' already exists. Please use a different name");+ throw new ModelException("model '" + name + "' already exists. Please use a different name");}availableModels.put(modeldata.getName(), modeldata);}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/package-info.javaindex 1ed9bffc86e..bbefbbc4a0b 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/package-info.java@@ -15,7 +15,5 @@* limitations under the License.*/-/**- * Contains feature and model store related classes.- */+/** Contains feature and model store related classes. */package org.apache.solr.ltr.store;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.javaindex 9ef36b876de..6242f932b23 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@@ -22,7 +22,6 @@ import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;-import org.apache.solr.common.SolrException;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;@@ -37,56 +36,50 @@ import org.apache.solr.rest.ManagedResourceStorage;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-/**- * Managed resource for a storing a feature.- */-public class ManagedFeatureStore extends ManagedResource implements ManagedResource.ChildResourceSupport {-- public static void registerManagedFeatureStore(SolrResourceLoader solrResourceLoader,- ManagedResourceObserver managedResourceObserver) {- solrResourceLoader.getManagedResourceRegistry().registerManagedResource(- REST_END_POINT,- ManagedFeatureStore.class,- managedResourceObserver);+/** Managed resource for a storing a feature. */+public class ManagedFeatureStore extends ManagedResource+ implements ManagedResource.ChildResourceSupport {++ public static void registerManagedFeatureStore(+ SolrResourceLoader solrResourceLoader, ManagedResourceObserver managedResourceObserver) {+ solrResourceLoader+ .getManagedResourceRegistry()+ .registerManagedResource(+ REST_END_POINT, ManagedFeatureStore.class, managedResourceObserver);}public static ManagedFeatureStore getManagedFeatureStore(SolrCore core) {- return (ManagedFeatureStore) core.getRestManager()- .getManagedResource(REST_END_POINT);+ return (ManagedFeatureStore) core.getRestManager().getManagedResource(REST_END_POINT);}- /** the feature store rest endpoint **/+ /** the feature store rest endpoint */public static final String REST_END_POINT = "/schema/feature-store";- /** name of the attribute containing the feature class **/+ /** name of the attribute containing the feature class */static final String CLASS_KEY = "class";- /** name of the attribute containing the feature name **/+ /** name of the attribute containing the feature name */static final String NAME_KEY = "name";- /** name of the attribute containing the feature params **/+ /** name of the attribute containing the feature params */static final String PARAMS_KEY = "params";- /** name of the attribute containing the feature store used **/+ /** name of the attribute containing the feature store used */static final String FEATURE_STORE_NAME_KEY = "store";- private final Map<String,FeatureStore> stores = new HashMap<>();+ private final Map<String, FeatureStore> stores = new HashMap<>();- /**- * Managed feature store: the name of the attribute containing all the feature- * stores- **/+ /** Managed feature store: the name of the attribute containing all the feature stores */private static final String FEATURE_STORE_JSON_FIELD = "featureStores";/**- * Managed feature store: the name of the attribute containing all the- * features of a feature store- **/+ * Managed feature store: the name of the attribute containing all the features of a feature store+ */private static final String FEATURES_JSON_FIELD = "features";private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- public ManagedFeatureStore(String resourceId, SolrResourceLoader loader,- ManagedResourceStorage.StorageIO storageIO) throws SolrException {+ public ManagedFeatureStore(+ String resourceId, SolrResourceLoader loader, ManagedResourceStorage.StorageIO storageIO)+ throws SolrException {super(resourceId, loader, storageIO);-}public synchronized FeatureStore getFeatureStore(String name) {@@ -100,22 +93,22 @@ public class ManagedFeatureStore extends ManagedResource implements ManagedResou}@Override- protected void onManagedDataLoadedFromStorage(NamedList<?> managedInitArgs,- Object managedData) throws SolrException {+ protected void onManagedDataLoadedFromStorage(NamedList<?> managedInitArgs, Object managedData)+ throws SolrException {stores.clear();log.info("------ managed feature ~ loading ------");if (managedData instanceof List) {@SuppressWarnings("unchecked")- final List<Map<String,Object>> up = (List<Map<String,Object>>) managedData;- for (final Map<String,Object> u : up) {+ final List<Map<String, Object>> up = (List<Map<String, Object>>) managedData;+ for (final Map<String, Object> u : up) {final String featureStore = (String) u.get(FEATURE_STORE_NAME_KEY);addFeature(u, featureStore);}}}- public synchronized void addFeature(Map<String,Object> map, String featureStore) {+ public synchronized void addFeature(Map<String, Object> map, String featureStore) {log.info("register feature based on {}", map);final FeatureStore fstore = getFeatureStore(featureStore);final Feature feature = fromFeatureMap(solrResourceLoader, map);@@ -126,8 +119,8 @@ public class ManagedFeatureStore extends ManagedResource implements ManagedResou@Overridepublic Object applyUpdatesToManagedData(Object updates) {if (updates instanceof List) {- final List<Map<String,Object>> up = (List<Map<String,Object>>) updates;- for (final Map<String,Object> u : up) {+ final List<Map<String, Object>> up = (List<Map<String, Object>>) updates;+ for (final Map<String, Object> u : up) {final String featureStore = (String) u.get(FEATURE_STORE_NAME_KEY);addFeature(u, featureStore);}@@ -135,7 +128,7 @@ public class ManagedFeatureStore extends ManagedResource implements ManagedResouif (updates instanceof Map) {// a unique feature- Map<String,Object> updatesMap = (Map<String,Object>) updates;+ Map<String, Object> updatesMap = (Map<String, Object>) updates;final String featureStore = (String) updatesMap.get(FEATURE_STORE_NAME_KEY);addFeature(updatesMap, featureStore);}@@ -156,9 +149,8 @@ public class ManagedFeatureStore extends ManagedResource implements ManagedResou}/**- * Called to retrieve a named part (the given childId) of the resource at the- * given endpoint. Note: since we have a unique child feature store we ignore- * the childId.+ * Called to retrieve a named part (the given childId) of the resource at the given endpoint.+ * Note: since we have a unique child feature store we ignore the childId.*/@Overridepublic void doGet(BaseSolrResource endpoint, String childId) {@@ -170,11 +162,10 @@ public class ManagedFeatureStore extends ManagedResource implements ManagedResou} else {final FeatureStore store = getFeatureStore(childId);if (store == null) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "missing feature store [" + childId + "]");+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST, "missing feature store [" + childId + "]");}- response.add(FEATURES_JSON_FIELD,- featuresAsManagedResources(store));+ response.add(FEATURES_JSON_FIELD, featuresAsManagedResources(store));}}@@ -182,29 +173,30 @@ public class ManagedFeatureStore extends ManagedResource implements ManagedResoufinal List<Feature> storedFeatures = store.getFeatures();final List<Object> features = new ArrayList<Object>(storedFeatures.size());for (final Feature f : storedFeatures) {- final LinkedHashMap<String,Object> m = toFeatureMap(f);+ final LinkedHashMap<String, Object> m = toFeatureMap(f);m.put(FEATURE_STORE_NAME_KEY, store.getName());features.add(m);}return features;}- private static LinkedHashMap<String,Object> toFeatureMap(Feature feat) {- final LinkedHashMap<String,Object> o = new LinkedHashMap<>(4, 1.0f); // 1 extra for caller to add store+ private static LinkedHashMap<String, Object> toFeatureMap(Feature feat) {+ final LinkedHashMap<String, Object> o = new LinkedHashMap<>(4, 1.0f);+ // 4 = 3 + 1 i.e. 1 extra for caller to add storeo.put(NAME_KEY, feat.getName());o.put(CLASS_KEY, feat.getClass().getName());o.put(PARAMS_KEY, feat.paramsToMap());return o;}- private static Feature fromFeatureMap(SolrResourceLoader solrResourceLoader,- Map<String,Object> featureMap) {+ private static Feature fromFeatureMap(+ SolrResourceLoader solrResourceLoader, Map<String, Object> featureMap) {final String className = (String) featureMap.get(CLASS_KEY);final String name = (String) featureMap.get(NAME_KEY);@SuppressWarnings("unchecked")- final Map<String,Object> params = (Map<String,Object>) featureMap.get(PARAMS_KEY);+ final Map<String, Object> params = (Map<String, Object>) featureMap.get(PARAMS_KEY);return Feature.getInstance(solrResourceLoader, className, name, params);}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedModelStore.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedModelStore.javaindex b4f3c42ddb6..a0f0b989dbe 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedModelStore.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/ManagedModelStore.java@@ -21,16 +21,15 @@ import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;-import org.apache.solr.common.SolrException;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.ltr.feature.Feature;import org.apache.solr.ltr.model.AdapterModel;-import org.apache.solr.ltr.model.WrapperModel;import org.apache.solr.ltr.model.LTRScoringModel;import org.apache.solr.ltr.model.ModelException;+import org.apache.solr.ltr.model.WrapperModel;import org.apache.solr.ltr.norm.IdentityNormalizer;import org.apache.solr.ltr.norm.Normalizer;import org.apache.solr.ltr.store.FeatureStore;@@ -43,44 +42,38 @@ import org.apache.solr.rest.ManagedResourceStorage;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-/**- * Menaged resource for storing a model- */-public class ManagedModelStore extends ManagedResource implements ManagedResource.ChildResourceSupport {-- public static void registerManagedModelStore(SolrResourceLoader solrResourceLoader,- ManagedResourceObserver managedResourceObserver) {- solrResourceLoader.getManagedResourceRegistry().registerManagedResource(- REST_END_POINT,- ManagedModelStore.class,- managedResourceObserver);+/** Menaged resource for storing a model */+public class ManagedModelStore extends ManagedResource+ implements ManagedResource.ChildResourceSupport {++ public static void registerManagedModelStore(+ SolrResourceLoader solrResourceLoader, ManagedResourceObserver managedResourceObserver) {+ solrResourceLoader+ .getManagedResourceRegistry()+ .registerManagedResource(REST_END_POINT, ManagedModelStore.class, managedResourceObserver);}public static ManagedModelStore getManagedModelStore(SolrCore core) {- return (ManagedModelStore) core.getRestManager()- .getManagedResource(REST_END_POINT);+ return (ManagedModelStore) core.getRestManager().getManagedResource(REST_END_POINT);}- /** the model store rest endpoint **/+ /** the model store rest endpoint */public static final String REST_END_POINT = "/schema/model-store";- /**- * Managed model store: the name of the attribute containing all the models of- * a model store- **/+ /** Managed model store: the name of the attribute containing all the models of a model store */private static final String MODELS_JSON_FIELD = "models";- /** name of the attribute containing a class **/+ /** name of the attribute containing a class */static final String CLASS_KEY = "class";- /** name of the attribute containing the features **/+ /** name of the attribute containing the features */static final String FEATURES_KEY = "features";- /** name of the attribute containing a name **/+ /** name of the attribute containing a name */static final String NAME_KEY = "name";- /** name of the attribute containing a normalizer **/+ /** name of the attribute containing a normalizer */static final String NORM_KEY = "norm";- /** name of the attribute containing parameters **/+ /** name of the attribute containing parameters */static final String PARAMS_KEY = "params";- /** name of the attribute containing a store **/+ /** name of the attribute containing a store */static final String STORE_KEY = "store";private final ModelStore store;@@ -88,8 +81,9 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourcprivate static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- public ManagedModelStore(String resourceId, SolrResourceLoader loader,- ManagedResourceStorage.StorageIO storageIO) throws SolrException {+ public ManagedModelStore(+ String resourceId, SolrResourceLoader loader, ManagedResourceStorage.StorageIO storageIO)+ throws SolrException {super(resourceId, loader, storageIO);store = new ModelStore();}@@ -106,8 +100,8 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourcprivate Object managedData;@Override- protected void onManagedDataLoadedFromStorage(NamedList<?> managedInitArgs,- Object managedData) throws SolrException {+ protected void onManagedDataLoadedFromStorage(NamedList<?> managedInitArgs, Object managedData)+ throws SolrException {store.clear();// the managed models on the disk or on zookeeper will be loaded in a lazy// way, since we need to set the managed features first (unfortunately@@ -115,7 +109,6 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourc// decouple the creation of a managed resource with the reading of the data// from the storage)this.managedData = managedData;-}public void loadStoredModels() {@@ -123,16 +116,17 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourcif ((managedData != null) && (managedData instanceof List)) {@SuppressWarnings({"unchecked"})- final List<Map<String,Object>> up = (List<Map<String,Object>>) managedData;- for (final Map<String,Object> u : up) {+ final List<Map<String, Object>> up = (List<Map<String, Object>>) managedData;+ for (final Map<String, Object> u : up) {addModelFromMap(u);}}}- private void addModelFromMap(Map<String,Object> modelMap) {+ private void addModelFromMap(Map<String, Object> modelMap) {try {- final LTRScoringModel algo = fromLTRScoringModelMap(solrResourceLoader, modelMap, managedFeatureStore);+ final LTRScoringModel algo =+ fromLTRScoringModelMap(solrResourceLoader, modelMap, managedFeatureStore);addModel(algo);} catch (final ModelException e) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);@@ -155,14 +149,14 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourcprotected Object applyUpdatesToManagedData(Object updates) {if (updates instanceof List) {- final List<Map<String,Object>> up = (List<Map<String,Object>>) updates;- for (final Map<String,Object> u : up) {+ final List<Map<String, Object>> up = (List<Map<String, Object>>) updates;+ for (final Map<String, Object> u : up) {addModelFromMap(u);}}if (updates instanceof Map) {- final Map<String,Object> map = (Map<String,Object>) updates;+ final Map<String, Object> map = (Map<String, Object>) updates;addModelFromMap(map);}@@ -176,16 +170,14 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourc}/**- * Called to retrieve a named part (the given childId) of the resource at the- * given endpoint. Note: since we have a unique child managed store we ignore- * the childId.+ * Called to retrieve a named part (the given childId) of the resource at the given endpoint.+ * Note: since we have a unique child managed store we ignore the childId.*/@Overridepublic void doGet(BaseSolrResource endpoint, String childId) {final SolrQueryResponse response = endpoint.getSolrResponse();- response.add(MODELS_JSON_FIELD,- modelsAsManagedResources(store.getModels()));+ response.add(MODELS_JSON_FIELD, modelsAsManagedResources(store.getModels()));}public LTRScoringModel getModel(String modelName) {@@ -196,15 +188,13 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourc@Overridepublic String toString() {- return "ManagedModelStore [store=" + store + ", featureStores="- + managedFeatureStore + "]";+ return "ManagedModelStore [store=" + store + ", featureStores=" + managedFeatureStore + "]";}/**- * Returns the available models as a list of Maps objects. After an update the- * managed resources needs to return the resources in this format in order to- * store in json somewhere (zookeeper, disk...)- *+ * Returns the available models as a list of Maps objects. After an update the managed resources+ * needs to return the resources in this format in order to store in json somewhere (zookeeper,+ * disk...)** @return the available models as a list of Maps objects*/@@ -217,8 +207,10 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourc}@SuppressWarnings("unchecked")- public static LTRScoringModel fromLTRScoringModelMap(SolrResourceLoader solrResourceLoader,- Map<String,Object> modelMap, ManagedFeatureStore managedFeatureStore) {+ public static LTRScoringModel fromLTRScoringModelMap(+ SolrResourceLoader solrResourceLoader,+ Map<String, Object> modelMap,+ ManagedFeatureStore managedFeatureStore) {final FeatureStore featureStore =managedFeatureStore.getFeatureStore((String) modelMap.get(STORE_KEY));@@ -229,56 +221,61 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourcfinal List<Object> featureList = (List<Object>) modelMap.get(FEATURES_KEY);if (featureList != null) {for (final Object feature : featureList) {- final Map<String,Object> featureMap = (Map<String,Object>) feature;+ final Map<String, Object> featureMap = (Map<String, Object>) feature;features.add(lookupFeatureFromFeatureMap(featureMap, featureStore));norms.add(createNormalizerFromFeatureMap(solrResourceLoader, featureMap));}}- final LTRScoringModel ltrScoringModel = LTRScoringModel.getInstance(solrResourceLoader,- (String) modelMap.get(CLASS_KEY), // modelClassName- (String) modelMap.get(NAME_KEY), // modelName- features,- norms,- featureStore.getName(),- featureStore.getFeatures(),- (Map<String,Object>) modelMap.get(PARAMS_KEY));+ final LTRScoringModel ltrScoringModel =+ LTRScoringModel.getInstance(+ solrResourceLoader,+ (String) modelMap.get(CLASS_KEY), // modelClassName+ (String) modelMap.get(NAME_KEY), // modelName+ features,+ norms,+ featureStore.getName(),+ featureStore.getFeatures(),+ (Map<String, Object>) modelMap.get(PARAMS_KEY));if (ltrScoringModel instanceof AdapterModel) {- initAdapterModel(solrResourceLoader, (AdapterModel)ltrScoringModel, managedFeatureStore);+ initAdapterModel(solrResourceLoader, (AdapterModel) ltrScoringModel, managedFeatureStore);}return ltrScoringModel;}- private static void initAdapterModel(SolrResourceLoader solrResourceLoader,- AdapterModel adapterModel, ManagedFeatureStore managedFeatureStore) {+ private static void initAdapterModel(+ SolrResourceLoader solrResourceLoader,+ AdapterModel adapterModel,+ ManagedFeatureStore managedFeatureStore) {adapterModel.init(solrResourceLoader);if (adapterModel instanceof WrapperModel) {- initWrapperModel(solrResourceLoader, (WrapperModel)adapterModel, managedFeatureStore);+ initWrapperModel(solrResourceLoader, (WrapperModel) adapterModel, managedFeatureStore);}}- private static void initWrapperModel(SolrResourceLoader solrResourceLoader,- WrapperModel wrapperModel, ManagedFeatureStore managedFeatureStore) {- final LTRScoringModel model = fromLTRScoringModelMap(- solrResourceLoader,- wrapperModel.fetchModelMap(),- managedFeatureStore);+ private static void initWrapperModel(+ SolrResourceLoader solrResourceLoader,+ WrapperModel wrapperModel,+ ManagedFeatureStore managedFeatureStore) {+ final LTRScoringModel model =+ fromLTRScoringModelMap(+ solrResourceLoader, wrapperModel.fetchModelMap(), managedFeatureStore);if (model instanceof AdapterModel) {- initAdapterModel(solrResourceLoader, (AdapterModel)model, managedFeatureStore);+ initAdapterModel(solrResourceLoader, (AdapterModel) model, managedFeatureStore);}wrapperModel.updateModel(model);}- private static LinkedHashMap<String,Object> toLTRScoringModelMap(LTRScoringModel model) {- final LinkedHashMap<String,Object> modelMap = new LinkedHashMap<>(5, 1.0f);+ private static LinkedHashMap<String, Object> toLTRScoringModelMap(LTRScoringModel model) {+ final LinkedHashMap<String, Object> modelMap = new LinkedHashMap<>(5, 1.0f);modelMap.put(NAME_KEY, model.getName());modelMap.put(CLASS_KEY, model.getClass().getName());modelMap.put(STORE_KEY, model.getFeatureStoreName());- final List<Map<String,Object>> features = new ArrayList<>();+ final List<Map<String, Object>> features = new ArrayList<>();if (!(model instanceof WrapperModel)) {final List<Feature> featuresList = model.getFeatures();final List<Normalizer> normsList = model.getNorms();@@ -293,56 +290,57 @@ public class ManagedModelStore extends ManagedResource implements ManagedResourcreturn modelMap;}- private static Feature lookupFeatureFromFeatureMap(Map<String, Object> featureMap, FeatureStore featureStore)- {+ private static Feature lookupFeatureFromFeatureMap(+ Map<String, Object> featureMap, FeatureStore featureStore) {final String featureName = (String) featureMap.get(NAME_KEY);Feature extractedFromStore = featureName == null ? null : featureStore.get(featureName);if (extractedFromStore == null) {if (featureStore.getFeatures().isEmpty()) {throw new ModelException("Missing or empty feature store: " + featureStore.getName());} else {- throw new ModelException("Feature: " + featureName + " not found in store: " + featureStore.getName());+ throw new ModelException(+ "Feature: " + featureName + " not found in store: " + featureStore.getName());}}return extractedFromStore;}@SuppressWarnings("unchecked")- private static Normalizer createNormalizerFromFeatureMap(SolrResourceLoader solrResourceLoader,- Map<String,Object> featureMap) {- final Map<String,Object> normMap = (Map<String,Object>)featureMap.get(NORM_KEY);- return (normMap == null ? IdentityNormalizer.INSTANCE+ private static Normalizer createNormalizerFromFeatureMap(+ SolrResourceLoader solrResourceLoader, Map<String, Object> featureMap) {+ final Map<String, Object> normMap = (Map<String, Object>) featureMap.get(NORM_KEY);+ return (normMap == null+ ? IdentityNormalizer.INSTANCE: fromNormalizerMap(solrResourceLoader, normMap));}- private static LinkedHashMap<String,Object> toFeatureMap(Feature feature, Normalizer norm) {- final LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>(2, 1.0f);- map.put(NAME_KEY, feature.getName());+ private static LinkedHashMap<String, Object> toFeatureMap(Feature feature, Normalizer norm) {+ final LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(2, 1.0f);+ map.put(NAME_KEY, feature.getName());map.put(NORM_KEY, toNormalizerMap(norm));return map;}- private static Normalizer fromNormalizerMap(SolrResourceLoader solrResourceLoader,- Map<String,Object> normMap) {+ private static Normalizer fromNormalizerMap(+ SolrResourceLoader solrResourceLoader, Map<String, Object> normMap) {final String className = (String) normMap.get(CLASS_KEY);@SuppressWarnings("unchecked")- final Map<String,Object> params = (Map<String,Object>) normMap.get(PARAMS_KEY);+ final Map<String, Object> params = (Map<String, Object>) normMap.get(PARAMS_KEY);return Normalizer.getInstance(solrResourceLoader, className, params);}- private static LinkedHashMap<String,Object> toNormalizerMap(Normalizer norm) {- final LinkedHashMap<String,Object> normalizer = new LinkedHashMap<>(2, 1.0f);+ private static LinkedHashMap<String, Object> toNormalizerMap(Normalizer norm) {+ final LinkedHashMap<String, Object> normalizer = new LinkedHashMap<>(2, 1.0f);normalizer.put(CLASS_KEY, norm.getClass().getName());- final LinkedHashMap<String,Object> params = norm.paramsToMap();+ final LinkedHashMap<String, Object> params = norm.paramsToMap();if (params != null) {normalizer.put(PARAMS_KEY, params);}return normalizer;}-}diff --git a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/package-info.java b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/package-info.javaindex fbf702999d4..0ec9b5f3990 100644--- a/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/package-info.java+++ b/solr/modules/ltr/src/java/org/apache/solr/ltr/store/rest/package-info.java@@ -16,7 +16,7 @@*//**- * Contains the {@link org.apache.solr.rest.ManagedResource} that encapsulate- * the feature and the model stores.+ * Contains the {@link org.apache.solr.rest.ManagedResource} that encapsulate the feature and the+ * model stores.*/package org.apache.solr.ltr.store.rest;