Index: solr/src/java/org/apache/solr/schema/LatLonType.java =================================================================== --- solr/src/java/org/apache/solr/schema/LatLonType.java (revision 1059016) +++ solr/src/java/org/apache/solr/schema/LatLonType.java (working copy) @@ -371,7 +371,7 @@ @Override public Scorer scorer(AtomicReaderContext context, ScorerContext scorerContext) throws IOException { - return new SpatialScorer(getSimilarity(searcher), context, this); + return new SpatialScorer(searcher.getSimilarity(), context, this); } @Override Index: solr/src/java/org/apache/solr/search/function/FunctionQuery.java =================================================================== --- solr/src/java/org/apache/solr/search/function/FunctionQuery.java (revision 1059016) +++ solr/src/java/org/apache/solr/search/function/FunctionQuery.java (working copy) @@ -95,7 +95,7 @@ @Override public Scorer scorer(AtomicReaderContext context, ScorerContext scorerContext) throws IOException { - return new AllScorer(getSimilarity(searcher), context, this); + return new AllScorer(searcher.getSimilarity(), context, this); } @Override Index: solr/src/java/org/apache/solr/search/function/BoostedQuery.java =================================================================== --- solr/src/java/org/apache/solr/search/function/BoostedQuery.java (revision 1059016) +++ solr/src/java/org/apache/solr/search/function/BoostedQuery.java (working copy) @@ -96,7 +96,7 @@ if(subQueryScorer == null) { return null; } - return new BoostedQuery.CustomScorer(getSimilarity(searcher), context, this, subQueryScorer, boostVal); + return new BoostedQuery.CustomScorer(searcher.getSimilarity(), context, this, subQueryScorer, boostVal); } @Override Index: solr/src/java/org/apache/solr/search/SolrConstantScoreQuery.java =================================================================== --- solr/src/java/org/apache/solr/search/SolrConstantScoreQuery.java (revision 1059016) +++ solr/src/java/org/apache/solr/search/SolrConstantScoreQuery.java (working copy) @@ -61,7 +61,7 @@ private Map context; public ConstantWeight(IndexSearcher searcher) throws IOException { - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); this.context = ValueSource.newContext(searcher); if (filter instanceof SolrFilter) ((SolrFilter)filter).createWeight(context, searcher); Index: lucene/src/test/org/apache/lucene/search/TestBooleanScorer.java =================================================================== --- lucene/src/test/org/apache/lucene/search/TestBooleanScorer.java (revision 1059016) +++ lucene/src/test/org/apache/lucene/search/TestBooleanScorer.java (working copy) @@ -25,6 +25,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanQuery.BooleanWeight; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; @@ -68,6 +69,13 @@ // 'more' variable to work properly, and this test ensures that if the logic // changes, we have a test to back it up. + Directory directory = newDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(random, directory); + writer.commit(); + IndexReader ir = writer.getReader(); + writer.close(); + IndexSearcher searcher = new IndexSearcher(ir); + Similarity sim = Similarity.getDefault(); Scorer[] scorers = new Scorer[] {new Scorer(sim) { private int doc = -1; @@ -83,10 +91,15 @@ } }}; - BooleanScorer bs = new BooleanScorer(null, false, sim, 1, Arrays.asList(scorers), null, scorers.length); + BooleanWeight weight = (BooleanWeight) new BooleanQuery().createWeight(searcher); + BooleanScorer bs = new BooleanScorer(weight, false, 1, Arrays.asList(scorers), null, scorers.length); assertEquals("should have received 3000", 3000, bs.nextDoc()); assertEquals("should have received NO_MORE_DOCS", DocIdSetIterator.NO_MORE_DOCS, bs.nextDoc()); + searcher.close(); + ir.close(); + directory.close(); + } } Index: lucene/src/test/org/apache/lucene/search/spans/TestSpans.java =================================================================== --- lucene/src/test/org/apache/lucene/search/spans/TestSpans.java (revision 1059016) +++ lucene/src/test/org/apache/lucene/search/spans/TestSpans.java (working copy) @@ -410,20 +410,21 @@ } }; - SpanNearQuery snq = new SpanNearQuery( + final Similarity oldSim = searcher.getSimilarity(); + Scorer spanScorer; + try { + searcher.setSimilarity(sim); + SpanNearQuery snq = new SpanNearQuery( new SpanQuery[] { makeSpanTermQuery("t1"), makeSpanTermQuery("t2") }, slop, - ordered) { - @Override - public Similarity getSimilarity(IndexSearcher s) { - return sim; - } - }; + ordered); - Scorer spanScorer = snq.weight(searcher).scorer(new AtomicReaderContext(new SlowMultiReaderWrapper(searcher.getIndexReader())), ScorerContext.def()); - + spanScorer = snq.weight(searcher).scorer(new AtomicReaderContext(new SlowMultiReaderWrapper(searcher.getIndexReader())), ScorerContext.def()); + } finally { + searcher.setSimilarity(oldSim); + } assertTrue("first doc", spanScorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS); assertEquals("first doc number", spanScorer.docID(), 11); float score = spanScorer.score(); Index: lucene/src/java/org/apache/lucene/search/ConstantScoreQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/ConstantScoreQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/ConstantScoreQuery.java (working copy) @@ -102,7 +102,7 @@ private float queryWeight; public ConstantWeight(IndexSearcher searcher) throws IOException { - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); this.innerWeight = (query == null) ? null : query.createWeight(searcher); } Index: lucene/src/java/org/apache/lucene/search/BooleanScorer2.java =================================================================== --- lucene/src/java/org/apache/lucene/search/BooleanScorer2.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/BooleanScorer2.java (working copy) @@ -22,6 +22,7 @@ import java.util.List; import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery.BooleanWeight; /* See the description in BooleanScorer.java, comparing * BooleanScorer & BooleanScorer2 */ @@ -42,10 +43,10 @@ int maxCoord = 0; // to be increased for each non prohibited scorer int nrMatchers; // to be increased by score() of match counting scorers. - void init(Similarity sim, boolean disableCoord) { // use after all scorers have been added. + void init(boolean disableCoord) { // use after all scorers have been added. coordFactors = new float[optionalScorers.size() + requiredScorers.size() + 1]; for (int i = 0; i < coordFactors.length; i++) { - coordFactors[i] = disableCoord ? 1.0f : sim.coord(i, maxCoord); + coordFactors[i] = disableCoord ? 1.0f : ((BooleanWeight)weight).coord(i, maxCoord); } } } @@ -80,7 +81,7 @@ * @param optional * the list of optional scorers. */ - public BooleanScorer2(Weight weight, boolean disableCoord, Similarity similarity, int minNrShouldMatch, + public BooleanScorer2(BooleanWeight weight, boolean disableCoord, int minNrShouldMatch, List required, List prohibited, List optional, int maxCoord) throws IOException { super(null, weight); // Similarity not used if (minNrShouldMatch < 0) { @@ -94,8 +95,8 @@ requiredScorers = required; prohibitedScorers = prohibited; - coordinator.init(similarity, disableCoord); - countingSumScorer = makeCountingSumScorer(disableCoord, similarity); + coordinator.init(disableCoord); + countingSumScorer = makeCountingSumScorer(disableCoord); } /** Count a scorer as a single match. */ @@ -163,11 +164,10 @@ } private Scorer countingConjunctionSumScorer(boolean disableCoord, - Similarity similarity, List requiredScorers) throws IOException { // each scorer from the list counted as a single matcher final int requiredNrMatchers = requiredScorers.size(); - return new ConjunctionScorer(disableCoord ? 1.0f : similarity.coord(requiredScorers.size(), requiredScorers.size()), requiredScorers) { + return new ConjunctionScorer(disableCoord ? 1.0f : ((BooleanWeight)weight).coord(requiredScorers.size(), requiredScorers.size()), requiredScorers) { private int lastScoredDoc = -1; // Save the score of lastScoredDoc, so that we don't compute it more than // once in score(). @@ -191,9 +191,8 @@ } private Scorer dualConjunctionSumScorer(boolean disableCoord, - Similarity similarity, Scorer req1, Scorer req2) throws IOException { // non counting. - return new ConjunctionScorer(disableCoord ? 1.0f : similarity.coord(2, 2), req1, req2); + return new ConjunctionScorer(disableCoord ? 1.0f : ((BooleanWeight)weight).coord(2, 2), req1, req2); // All scorers match, so defaultSimilarity always has 1 as // the coordination factor. // Therefore the sum of the scores of two scorers @@ -203,14 +202,13 @@ /** Returns the scorer to be used for match counting and score summing. * Uses requiredScorers, optionalScorers and prohibitedScorers. */ - private Scorer makeCountingSumScorer(boolean disableCoord, - Similarity similarity) throws IOException { // each scorer counted as a single matcher + private Scorer makeCountingSumScorer(boolean disableCoord) throws IOException { // each scorer counted as a single matcher return (requiredScorers.size() == 0) - ? makeCountingSumScorerNoReq(disableCoord, similarity) - : makeCountingSumScorerSomeReq(disableCoord, similarity); + ? makeCountingSumScorerNoReq(disableCoord) + : makeCountingSumScorerSomeReq(disableCoord); } - private Scorer makeCountingSumScorerNoReq(boolean disableCoord, Similarity similarity) throws IOException { // No required scorers + private Scorer makeCountingSumScorerNoReq(boolean disableCoord) throws IOException { // No required scorers // minNrShouldMatch optional scorers are required, but at least 1 int nrOptRequired = (minNrShouldMatch < 1) ? 1 : minNrShouldMatch; Scorer requiredCountingSumScorer; @@ -219,26 +217,25 @@ else if (optionalScorers.size() == 1) requiredCountingSumScorer = new SingleMatchScorer(optionalScorers.get(0)); else { - requiredCountingSumScorer = countingConjunctionSumScorer(disableCoord, similarity, optionalScorers); + requiredCountingSumScorer = countingConjunctionSumScorer(disableCoord, optionalScorers); } return addProhibitedScorers(requiredCountingSumScorer); } - private Scorer makeCountingSumScorerSomeReq(boolean disableCoord, Similarity similarity) throws IOException { // At least one required scorer. + private Scorer makeCountingSumScorerSomeReq(boolean disableCoord) throws IOException { // At least one required scorer. if (optionalScorers.size() == minNrShouldMatch) { // all optional scorers also required. ArrayList allReq = new ArrayList(requiredScorers); allReq.addAll(optionalScorers); - return addProhibitedScorers(countingConjunctionSumScorer(disableCoord, similarity, allReq)); + return addProhibitedScorers(countingConjunctionSumScorer(disableCoord, allReq)); } else { // optionalScorers.size() > minNrShouldMatch, and at least one required scorer Scorer requiredCountingSumScorer = requiredScorers.size() == 1 ? new SingleMatchScorer(requiredScorers.get(0)) - : countingConjunctionSumScorer(disableCoord, similarity, requiredScorers); + : countingConjunctionSumScorer(disableCoord, requiredScorers); if (minNrShouldMatch > 0) { // use a required disjunction scorer over the optional scorers return addProhibitedScorers( dualConjunctionSumScorer( // non counting disableCoord, - similarity, requiredCountingSumScorer, countingDisjunctionSumScorer( optionalScorers, Index: lucene/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java (working copy) @@ -107,11 +107,6 @@ } @Override - public Similarity getSimilarity(IndexSearcher searcher) { - return maskedQuery.getSimilarity(searcher); - } - - @Override public Query rewrite(IndexReader reader) throws IOException { FieldMaskingSpanQuery clone = null; Index: lucene/src/java/org/apache/lucene/search/spans/SpanWeight.java =================================================================== --- lucene/src/java/org/apache/lucene/search/spans/SpanWeight.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/spans/SpanWeight.java (working copy) @@ -42,7 +42,7 @@ public SpanWeight(SpanQuery query, IndexSearcher searcher) throws IOException { - this.similarity = query.getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); this.query = query; terms=new HashSet(); Index: lucene/src/java/org/apache/lucene/search/MultiPhraseQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/MultiPhraseQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/MultiPhraseQuery.java (working copy) @@ -137,7 +137,7 @@ public MultiPhraseWeight(IndexSearcher searcher) throws IOException { - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); // compute idf final int maxDoc = searcher.maxDoc(); Index: lucene/src/java/org/apache/lucene/search/FilteredQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/FilteredQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/FilteredQuery.java (working copy) @@ -62,7 +62,7 @@ @Override public Weight createWeight(final IndexSearcher searcher) throws IOException { final Weight weight = query.createWeight (searcher); - final Similarity similarity = query.getSimilarity(searcher); + final Similarity similarity = searcher.getSimilarity(); return new Weight() { private float value; Index: lucene/src/java/org/apache/lucene/search/function/ValueSourceQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/function/ValueSourceQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/function/ValueSourceQuery.java (working copy) @@ -69,7 +69,7 @@ float queryWeight; public ValueSourceWeight(IndexSearcher searcher) { - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); } /*(non-Javadoc) @see org.apache.lucene.search.Weight#getQuery() */ Index: lucene/src/java/org/apache/lucene/search/function/CustomScoreQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/function/CustomScoreQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/function/CustomScoreQuery.java (working copy) @@ -189,7 +189,7 @@ boolean qStrict; public CustomWeight(IndexSearcher searcher) throws IOException { - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); this.subQueryWeight = subQuery.weight(searcher); this.valSrcWeights = new Weight[valSrcQueries.length]; for(int i = 0; i < valSrcQueries.length; i++) { Index: lucene/src/java/org/apache/lucene/search/BooleanQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/BooleanQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/BooleanQuery.java (working copy) @@ -169,7 +169,7 @@ public BooleanWeight(IndexSearcher searcher, boolean disableCoord) throws IOException { - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); this.disableCoord = disableCoord; weights = new ArrayList(clauses.size()); for (int i = 0 ; i < clauses.size(); i++) { @@ -201,6 +201,9 @@ return sum ; } + public float coord(int overlap, int maxOverlap) { + return similarity.coord(overlap, maxOverlap); + } @Override public void normalize(float norm) { @@ -273,7 +276,7 @@ sumExpl.setMatch(0 < coord ? Boolean.TRUE : Boolean.FALSE); sumExpl.setValue(sum); - final float coordFactor = disableCoord ? 1.0f : similarity.coord(coord, maxCoord); + final float coordFactor = disableCoord ? 1.0f : coord(coord, maxCoord); if (coordFactor == 1.0f) { return sumExpl; // eliminate wrapper } else { @@ -312,7 +315,7 @@ // Check if we can return a BooleanScorer if (!scorerContext.scoreDocsInOrder && scorerContext.topScorer && required.size() == 0 && prohibited.size() < 32) { - return new BooleanScorer(this, disableCoord, similarity, minNrShouldMatch, optional, prohibited, maxCoord); + return new BooleanScorer(this, disableCoord, minNrShouldMatch, optional, prohibited, maxCoord); } if (required.size() == 0 && optional.size() == 0) { @@ -326,7 +329,7 @@ } // Return a BooleanScorer2 - return new BooleanScorer2(this, disableCoord, similarity, minNrShouldMatch, required, prohibited, optional, maxCoord); + return new BooleanScorer2(this, disableCoord, minNrShouldMatch, required, prohibited, optional, maxCoord); } @Override Index: lucene/src/java/org/apache/lucene/search/Query.java =================================================================== --- lucene/src/java/org/apache/lucene/search/Query.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/Query.java (working copy) @@ -98,7 +98,7 @@ Query query = searcher.rewrite(this); Weight weight = query.createWeight(searcher); float sum = weight.sumOfSquaredWeights(); - float norm = getSimilarity(searcher).queryNorm(sum); + float norm = searcher.getSimilarity().queryNorm(sum); if (Float.isInfinite(norm) || Float.isNaN(norm)) norm = 1.0f; weight.normalize(norm); @@ -124,16 +124,7 @@ // needs to be implemented by query subclasses throw new UnsupportedOperationException(); } - - /** Expert: Returns the Similarity implementation to be used for this query. - * Subclasses may override this method to specify their own Similarity - * implementation, perhaps one that delegates through that of the Searcher. - * By default the Searcher's Similarity implementation is returned.*/ - public Similarity getSimilarity(IndexSearcher searcher) { - return searcher.getSimilarity(); - } - /** Returns a clone of this query. */ @Override public Object clone() { Index: lucene/src/java/org/apache/lucene/search/PhraseQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/PhraseQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/PhraseQuery.java (working copy) @@ -146,7 +146,7 @@ public PhraseWeight(IndexSearcher searcher) throws IOException { - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); idfExp = similarity.idfExplain(terms, searcher); idf = idfExp.getIdf(); Index: lucene/src/java/org/apache/lucene/search/BooleanScorer.java =================================================================== --- lucene/src/java/org/apache/lucene/search/BooleanScorer.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/BooleanScorer.java (working copy) @@ -22,6 +22,7 @@ import org.apache.lucene.index.IndexReader.AtomicReaderContext; import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery.BooleanWeight; /* Description from Doug Cutting (excerpted from * LUCENE-1483): @@ -197,7 +198,7 @@ private Bucket current; private int doc = -1; - BooleanScorer(Weight weight, boolean disableCoord, Similarity similarity, int minNrShouldMatch, + BooleanScorer(BooleanWeight weight, boolean disableCoord, int minNrShouldMatch, List optionalScorers, List prohibitedScorers, int maxCoord) throws IOException { super(null, weight); // Similarity not used this.minNrShouldMatch = minNrShouldMatch; @@ -223,7 +224,7 @@ coordFactors = new float[optionalScorers.size() + 1]; for (int i = 0; i < coordFactors.length; i++) { - coordFactors[i] = disableCoord ? 1.0f : similarity.coord(i, maxCoord); + coordFactors[i] = disableCoord ? 1.0f : weight.coord(i, maxCoord); } } Index: lucene/src/java/org/apache/lucene/search/TermQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/TermQuery.java (revision 1059016) +++ lucene/src/java/org/apache/lucene/search/TermQuery.java (working copy) @@ -54,7 +54,7 @@ throws IOException { assert termStates != null : "PerReaderTermState must not be null"; this.termStates = termStates; - this.similarity = getSimilarity(searcher); + this.similarity = searcher.getSimilarity(); if (docFreq != -1) { idfExp = similarity.idfExplain(term, searcher, docFreq); } else { Index: lucene/contrib/queries/src/java/org/apache/lucene/search/BoostingQuery.java =================================================================== --- lucene/contrib/queries/src/java/org/apache/lucene/search/BoostingQuery.java (revision 1059016) +++ lucene/contrib/queries/src/java/org/apache/lucene/search/BoostingQuery.java (working copy) @@ -21,10 +21,9 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.DefaultSimilarity; import org.apache.lucene.search.Query; import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.search.Similarity; + /** * The BoostingQuery class can be used to effectively demote results that match a given query. * Unlike the "NOT" clause, this still selects documents that contain undesirable terms, @@ -56,10 +55,9 @@ @Override public Query rewrite(IndexReader reader) throws IOException { BooleanQuery result = new BooleanQuery() { - @Override - public Similarity getSimilarity(IndexSearcher searcher) { - return new DefaultSimilarity() { + public Weight createWeight(IndexSearcher searcher) throws IOException { + return new BooleanWeight(searcher, false) { @Override public float coord(int overlap, int max) {