From 3e833dd79f113119524c330fd4e40e91a131deca Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 27 Apr 2011 14:50:02 +0000 Subject: [PATCH] LUCENE-3048: Improve BooleanQuery rewrite documentation git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1097133 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/lucene/search/BooleanQuery.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lucene/src/java/org/apache/lucene/search/BooleanQuery.java b/lucene/src/java/org/apache/lucene/search/BooleanQuery.java index 8e2240cdea9..ecddb66bb96 100644 --- a/lucene/src/java/org/apache/lucene/search/BooleanQuery.java +++ b/lucene/src/java/org/apache/lucene/search/BooleanQuery.java @@ -367,8 +367,12 @@ public class BooleanQuery extends Query implements Iterable { Query query = c.getQuery().rewrite(reader); // rewrite first if (getBoost() != 1.0f) { // incorporate boost - if (query == c.getQuery()) // if rewrite was no-op + if (query == c.getQuery()) { // if rewrite was no-op query = (Query)query.clone(); // then clone before boost + } + // Since the BooleanQuery only has 1 clause, the BooleanQuery will be + // written out. Therefore the rewritten Query's boost must incorporate both + // the clause's boost, and the boost of the BooleanQuery itself query.setBoost(getBoost() * query.getBoost()); } @@ -381,8 +385,12 @@ public class BooleanQuery extends Query implements Iterable { BooleanClause c = clauses.get(i); Query query = c.getQuery().rewrite(reader); if (query != c.getQuery()) { // clause rewrote: must clone - if (clone == null) + if (clone == null) { + // The BooleanQuery clone is lazily initialized so only initialize + // it if a rewritten clause differs from the original clause (and hasn't been + // initialized already). If nothing differs, the clone isn't needlessly created clone = (BooleanQuery)this.clone(); + } clone.clauses.set(i, new BooleanClause(query, c.getOccur())); } }