Apache Solr open-source search software
LUCENE-4236: move all crazies into one place
Robert Muirf3ecdd337a70
diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.javaindex e10456e016b..f6de71293d7 100644--- a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java@@ -420,18 +420,17 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {} else {float coordReq = coord(required.size(), maxCoord);float coordBoth = coord(required.size() + 1, maxCoord);- return new ReqOptSumScorer.ReqSingleOptScorer(req, opt, coordReq, coordBoth);+ return new BooleanTopLevelScorers.ReqSingleOptScorer(req, opt, coordReq, coordBoth);}} else {if (minShouldMatch > 0) {- return new ConjunctionScorer.CoordinatingConjunctionScorer(this, coords(), req, required.size(), opt);+ return new BooleanTopLevelScorers.CoordinatingConjunctionScorer(this, coords(), req, required.size(), opt);} else {- return new ReqOptSumScorer.ReqMultiOptScorer(req, opt, required.size(), coords());+ return new BooleanTopLevelScorers.ReqMultiOptScorer(req, opt, required.size(), coords());}}}- // nocommit: double-check all this@Overridepublic boolean scoresDocsOutOfOrder() {if (minNrShouldMatch > 1) {@@ -460,7 +459,7 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {if (required.size() == 1) {Scorer req = required.get(0);if (!disableCoord && maxCoord > 1) {- return new BoostedScorer(req, coord(1, maxCoord));+ return new BooleanTopLevelScorers.BoostedScorer(req, coord(1, maxCoord));} else {return req;}@@ -491,7 +490,7 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {if (optional.size() == 1) {Scorer opt = optional.get(0);if (!disableCoord && maxCoord > 1) {- return new BoostedScorer(opt, coord(1, maxCoord));+ return new BooleanTopLevelScorers.BoostedScorer(opt, coord(1, maxCoord));} else {return opt;}diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanTopLevelScorers.java b/lucene/core/src/java/org/apache/lucene/search/BooleanTopLevelScorers.javanew file mode 100644index 00000000000..6edcf2cba6b--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanTopLevelScorers.java@@ -0,0 +1,136 @@+package org.apache.lucene.search;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import java.io.IOException;++/** Internal document-at-a-time scorers used to deal with stupid coord() computation */+class BooleanTopLevelScorers {++ /**+ * Used when there is more than one scorer in a query, but a segment+ * only had one non-null scorer. This just wraps that scorer directly+ * to factor in coord().+ */+ static class BoostedScorer extends FilterScorer {+ private final float boost;++ BoostedScorer(Scorer in, float boost) {+ super(in);+ this.boost = boost;+ }++ @Override+ public float score() throws IOException {+ return in.score() * boost;+ }+ }++ /**+ * Used when there are both mandatory and optional clauses, but minShouldMatch+ * dictates that some of the optional clauses must match. The query is a conjunction,+ * but must compute coord based on how many optional subscorers matched (freq).+ */+ static class CoordinatingConjunctionScorer extends ConjunctionScorer {+ private final float coords[];+ private final int reqCount;+ private final Scorer req;+ private final Scorer opt;++ CoordinatingConjunctionScorer(Weight weight, float coords[], Scorer req, int reqCount, Scorer opt) {+ super(weight, new Scorer[] { req, opt });+ this.coords = coords;+ this.req = req;+ this.reqCount = reqCount;+ this.opt = opt;+ }++ @Override+ public float score() throws IOException {+ return (req.score() + opt.score()) * coords[reqCount + opt.freq()];+ }+ }++ /**+ * Used when there are mandatory clauses with one optional clause: we compute+ * coord based on whether the optional clause matched or not.+ */+ static class ReqSingleOptScorer extends ReqOptSumScorer {+ // coord factor if just the required part matches+ private final float coordReq;+ // coord factor if both required and optional part matches+ private final float coordBoth;++ public ReqSingleOptScorer(Scorer reqScorer, Scorer optScorer, float coordReq, float coordBoth) {+ super(reqScorer, optScorer);+ this.coordReq = coordReq;+ this.coordBoth = coordBoth;+ }++ @Override+ public float score() throws IOException {+ int curDoc = reqScorer.docID();+ float reqScore = reqScorer.score();+ if (optScorer == null) {+ return reqScore * coordReq;+ }++ int optScorerDoc = optScorer.docID();+ if (optScorerDoc < curDoc && (optScorerDoc = optScorer.advance(curDoc)) == NO_MORE_DOCS) {+ optScorer = null;+ return reqScore * coordReq;+ }++ return optScorerDoc == curDoc ? (reqScore + optScorer.score()) * coordBoth : reqScore * coordReq;+ }+ }++ /**+ * Used when there are mandatory clauses with optional clauses: we compute+ * coord based on how many optional subscorers matched (freq).+ */+ static class ReqMultiOptScorer extends ReqOptSumScorer {+ private final int requiredCount;+ private final float coords[];+ private final Scorer disjunction;++ public ReqMultiOptScorer(Scorer reqScorer, Scorer optScorer, int requiredCount, float coords[]) {+ super(reqScorer, optScorer);+ this.requiredCount = requiredCount;+ this.coords = coords;+ this.disjunction = optScorer;+ }++ @Override+ public float score() throws IOException {+ int curDoc = reqScorer.docID();+ float reqScore = reqScorer.score();+ if (optScorer == null) {+ return reqScore * coords[requiredCount];+ }++ int optScorerDoc = optScorer.docID();+ if (optScorerDoc < curDoc && (optScorerDoc = optScorer.advance(curDoc)) == NO_MORE_DOCS) {+ optScorer = null;+ return reqScore * coords[requiredCount];+ }++ return optScorerDoc == curDoc ? (reqScore + optScorer.score()) * coords[requiredCount + disjunction.freq()] : reqScore * coords[requiredCount];+ }+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/search/BoostedScorer.java b/lucene/core/src/java/org/apache/lucene/search/BoostedScorer.javadeleted file mode 100644index 37dd089cff9..00000000000--- a/lucene/core/src/java/org/apache/lucene/search/BoostedScorer.java+++ /dev/null@@ -1,35 +0,0 @@-package org.apache.lucene.search;--/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF licenses this file to You under the Apache License, Version 2.0- * (the "License"); you may not use this file except in compliance with- * the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--import java.io.IOException;--// nocommit: WTF?-final class BoostedScorer extends FilterScorer {- private final float boost;-- BoostedScorer(Scorer in, float boost) {- super(in);- this.boost = boost;- }-- @Override- public float score() throws IOException {- return in.score() * boost;- }-}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java b/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.javaindex 1ed83557892..3e811873618 100644--- a/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java@@ -139,25 +139,4 @@ class ConjunctionScorer extends Scorer {this.cost = scorer.cost();}}-- // nocommit: WTF?- static class CoordinatingConjunctionScorer extends ConjunctionScorer {- private final float coords[];- private final int reqCount;- private final Scorer req;- private final Scorer opt;-- CoordinatingConjunctionScorer(Weight weight, float coords[], Scorer req, int reqCount, Scorer opt) {- super(weight, new Scorer[] { req, opt });- this.coords = coords;- this.req = req;- this.reqCount = reqCount;- this.opt = opt;- }-- @Override- public float score() throws IOException {- return (req.score() + opt.score()) * coords[reqCount + opt.freq()];- }- }}diff --git a/lucene/core/src/java/org/apache/lucene/search/ReqOptSumScorer.java b/lucene/core/src/java/org/apache/lucene/search/ReqOptSumScorer.javaindex edaadae298c..d7b4d86bb82 100644--- a/lucene/core/src/java/org/apache/lucene/search/ReqOptSumScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/ReqOptSumScorer.java@@ -104,67 +104,5 @@ class ReqOptSumScorer extends Scorer {public long cost() {return reqScorer.cost();}-- // nocommit: WTF?- static class ReqSingleOptScorer extends ReqOptSumScorer {- // coord factor if just the required part matches- private final float coordReq;- // coord factor if both required and optional part matches- private final float coordBoth;-- public ReqSingleOptScorer(Scorer reqScorer, Scorer optScorer, float coordReq, float coordBoth) {- super(reqScorer, optScorer);- this.coordReq = coordReq;- this.coordBoth = coordBoth;- }-- @Override- public float score() throws IOException {- int curDoc = reqScorer.docID();- float reqScore = reqScorer.score();- if (optScorer == null) {- return reqScore * coordReq;- }-- int optScorerDoc = optScorer.docID();- if (optScorerDoc < curDoc && (optScorerDoc = optScorer.advance(curDoc)) == NO_MORE_DOCS) {- optScorer = null;- return reqScore * coordReq;- }-- return optScorerDoc == curDoc ? (reqScore + optScorer.score()) * coordBoth : reqScore * coordReq;- }- }-- // nocommit: WTF?- static class ReqMultiOptScorer extends ReqOptSumScorer {- private final int requiredCount;- private final float coords[];- private final Scorer disjunction;-- public ReqMultiOptScorer(Scorer reqScorer, Scorer optScorer, int requiredCount, float coords[]) {- super(reqScorer, optScorer);- this.requiredCount = requiredCount;- this.coords = coords;- this.disjunction = optScorer;- }-- @Override- public float score() throws IOException {- int curDoc = reqScorer.docID();- float reqScore = reqScorer.score();- if (optScorer == null) {- return reqScore * coords[requiredCount];- }-- int optScorerDoc = optScorer.docID();- if (optScorerDoc < curDoc && (optScorerDoc = optScorer.advance(curDoc)) == NO_MORE_DOCS) {- optScorer = null;- return reqScore * coords[requiredCount];- }-- return optScorerDoc == curDoc ? (reqScore + optScorer.score()) * coords[requiredCount + disjunction.freq()] : reqScore * coords[requiredCount];- }- }}