Apache Lucene open-source search software
LUCENE-6926: Take the match cost into account for MUST_NOT clauses.
Adrien Grand345050110e93
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex efe7c12cce4..f604916a77d 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -143,6 +143,9 @@ Optimizationsstopping to evaluate them as soon as a single one matches. The other iteratorswill be confirmed lazily when computing score() or freq(). (Adrien Grand)+* LUCENE-6926: MUST_NOT clauses now use the match cost API to run the slow bits+ last whenever possible. (Adrien Grand)+Bug Fixes* LUCENE-6918: LRUQueryCache.onDocIdSetEviction is only called when at leastdiff --git a/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.java b/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.javaindex 35f17c26404..d585cbb6e5e 100644--- a/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.java@@ -57,67 +57,13 @@ class ReqExclScorer extends Scorer {/** Confirms whether or not the given {@link TwoPhaseIterator}* matches on the current document. */- private static boolean matches(TwoPhaseIterator it) throws IOException {+ private static boolean matchesOrNull(TwoPhaseIterator it) throws IOException {return it == null || it.matches();}- /** Confirm whether there is a match given the current positions of the- * req and excl approximations. This method has 2 important properties:- * - it only calls matches() on excl if the excl approximation is on- * the same doc ID as the req approximation- * - it does NOT call matches() on req if the excl approximation is exact- * and is on the same doc ID as the req approximation */- private static boolean matches(int doc, int exclDoc,- TwoPhaseIterator reqTwoPhaseIterator,- TwoPhaseIterator exclTwoPhaseIterator) throws IOException {- assert exclDoc >= doc;- if (doc == exclDoc && matches(exclTwoPhaseIterator)) {- return false;- }- return matches(reqTwoPhaseIterator);- }-@Overridepublic DocIdSetIterator iterator() {- return new DocIdSetIterator() {-- /** Advance to the next non-excluded doc. */- private int toNonExcluded(int doc) throws IOException {- int exclDoc = exclApproximation.docID();- for (;; doc = reqApproximation.nextDoc()) {- if (doc == NO_MORE_DOCS) {- return NO_MORE_DOCS;- }- if (exclDoc < doc) {- exclDoc = exclApproximation.advance(doc);- }- if (matches(doc, exclDoc, reqTwoPhaseIterator, exclTwoPhaseIterator)) {- return doc;- }- }- }-- @Override- public int nextDoc() throws IOException {- return toNonExcluded(reqApproximation.nextDoc());- }-- @Override- public int advance(int target) throws IOException {- return toNonExcluded(reqApproximation.advance(target));- }-- @Override- public int docID() {- return reqApproximation.docID();- }-- @Override- public long cost() {- return reqApproximation.cost();- }-- };+ return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());}@Override@@ -140,28 +86,96 @@ class ReqExclScorer extends Scorer {return Collections.singleton(new ChildScorer(reqScorer, "MUST"));}+ /**+ * Estimation of the number of operations required to call DISI.advance.+ * This is likely completely wrong, especially given that the cost of+ * this method usually depends on how far you want to advance, but it's+ * probably better than nothing.+ */+ private static final int ADVANCE_COST = 10;++ private static float matchCost(+ DocIdSetIterator reqApproximation,+ TwoPhaseIterator reqTwoPhaseIterator,+ DocIdSetIterator exclApproximation,+ TwoPhaseIterator exclTwoPhaseIterator) {+ float matchCost = 2; // we perform 2 comparisons to advance exclApproximation+ if (reqTwoPhaseIterator != null) {+ // this two-phase iterator must always be matched+ matchCost += reqTwoPhaseIterator.matchCost();+ }++ // match cost of the prohibited clause: we need to advance the approximation+ // and match the two-phased iterator+ final float exclMatchCost = ADVANCE_COST+ + (exclTwoPhaseIterator == null ? 0 : exclTwoPhaseIterator.matchCost());++ // upper value for the ratio of documents that reqApproximation matches that+ // exclApproximation also matches+ float ratio;+ if (reqApproximation.cost() <= 0) {+ ratio = 1f;+ } else if (exclApproximation.cost() <= 0) {+ ratio = 0f;+ } else {+ ratio = (float) Math.min(reqApproximation.cost(), exclApproximation.cost()) / reqApproximation.cost();+ }+ matchCost += ratio * exclMatchCost;++ return matchCost;+ }+@Overridepublic TwoPhaseIterator twoPhaseIterator() {- if (reqTwoPhaseIterator == null) {- return null;- }- return new TwoPhaseIterator(reqApproximation) {-- @Override- public boolean matches() throws IOException {- final int doc = reqApproximation.docID();- // check if the doc is not excluded- int exclDoc = exclApproximation.docID();- if (exclDoc < doc) {- exclDoc = exclApproximation.advance(doc);+ final float matchCost = matchCost(reqApproximation, reqTwoPhaseIterator, exclApproximation, exclTwoPhaseIterator);++ if (reqTwoPhaseIterator == null+ || (exclTwoPhaseIterator != null && reqTwoPhaseIterator.matchCost() <= exclTwoPhaseIterator.matchCost())) {+ // reqTwoPhaseIterator is LESS costly than exclTwoPhaseIterator, check it first+ return new TwoPhaseIterator(reqApproximation) {++ @Override+ public boolean matches() throws IOException {+ final int doc = reqApproximation.docID();+ // check if the doc is not excluded+ int exclDoc = exclApproximation.docID();+ if (exclDoc < doc) {+ exclDoc = exclApproximation.advance(doc);+ }+ if (exclDoc != doc) {+ return matchesOrNull(reqTwoPhaseIterator);+ }+ return matchesOrNull(reqTwoPhaseIterator) && !matchesOrNull(exclTwoPhaseIterator);}- return ReqExclScorer.matches(doc, exclDoc, reqTwoPhaseIterator, exclTwoPhaseIterator);- }-- @Override- public float matchCost() {- return reqTwoPhaseIterator.matchCost(); // TODO: also use cost of exclApproximation.advance()- }- };++ @Override+ public float matchCost() {+ return matchCost;+ }+ };+ } else {+ // reqTwoPhaseIterator is MORE costly than exclTwoPhaseIterator, check it first+ return new TwoPhaseIterator(reqApproximation) {++ @Override+ public boolean matches() throws IOException {+ final int doc = reqApproximation.docID();+ // check if the doc is not excluded+ int exclDoc = exclApproximation.docID();+ if (exclDoc < doc) {+ exclDoc = exclApproximation.advance(doc);+ }+ if (exclDoc != doc) {+ return matchesOrNull(reqTwoPhaseIterator);+ }+ return !matchesOrNull(exclTwoPhaseIterator) && matchesOrNull(reqTwoPhaseIterator);+ }++ @Override+ public float matchCost() {+ return matchCost;+ }+ };+ }}}