diff --git a/CHANGES.txt b/CHANGES.txt index 66c10fd33b2..13ca7dd6517 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -647,6 +647,12 @@ New features 3.0 and will then replace the current core QueryParser, which has been deprecated with this patch. (Luis Alves and Adriano Campos via Michael Busch) + +36. LUCENE-1776: Add an option to not collect payloads for an ordered + SpanNearQuery. Payloads were not lazily loaded in this case as + the javadocs implied. If you have payloads and want to use an ordered + SpanNearQuery that does not need to use the payloads, you can + disable loading them with a new constructor switch. (Mark Miller) Optimizations diff --git a/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java b/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java index 326e0f02dc9..dd42774a59f 100644 --- a/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java +++ b/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java @@ -72,13 +72,19 @@ class NearSpansOrdered implements PayloadSpans { }; private SpanNearQuery query; + private boolean collectPayloads = true; + + public NearSpansOrdered(SpanNearQuery spanNearQuery, IndexReader reader) throws IOException { + this(spanNearQuery, reader, true); + } - public NearSpansOrdered(SpanNearQuery spanNearQuery, IndexReader reader) + public NearSpansOrdered(SpanNearQuery spanNearQuery, IndexReader reader, boolean collectPayloads) throws IOException { if (spanNearQuery.getClauses().length < 2) { throw new IllegalArgumentException("Less than 2 clauses: " + spanNearQuery); } + this.collectPayloads = collectPayloads; allowedSlop = spanNearQuery.getSlop(); SpanQuery[] clauses = spanNearQuery.getClauses(); subSpans = new PayloadSpans[clauses.length]; @@ -101,6 +107,7 @@ class NearSpansOrdered implements PayloadSpans { public int end() { return matchEnd; } // TODO: Remove warning after API has been finalized + // TODO: Would be nice to be able to lazy load payloads public Collection/**/ getPayload() throws IOException { return matchPayload; } @@ -122,7 +129,9 @@ class NearSpansOrdered implements PayloadSpans { } more = true; } - matchPayload.clear(); + if(collectPayloads) { + matchPayload.clear(); + } return advanceAfterOrdered(); } @@ -145,7 +154,9 @@ class NearSpansOrdered implements PayloadSpans { return false; } } - matchPayload.clear(); + if(collectPayloads) { + matchPayload.clear(); + } return advanceAfterOrdered(); } @@ -250,7 +261,7 @@ class NearSpansOrdered implements PayloadSpans { int lastEnd = matchEnd; for (int i = subSpans.length - 2; i >= 0; i--) { PayloadSpans prevSpans = subSpans[i]; - if (prevSpans.isPayloadAvailable()) { + if (collectPayloads && prevSpans.isPayloadAvailable()) { Collection payload = prevSpans.getPayload(); possiblePayload = new ArrayList(payload.size()); possiblePayload.addAll(payload); @@ -274,7 +285,7 @@ class NearSpansOrdered implements PayloadSpans { } else { // prevSpans still before (lastStart, lastEnd) prevStart = ppStart; prevEnd = ppEnd; - if (prevSpans.isPayloadAvailable()) { + if (collectPayloads && prevSpans.isPayloadAvailable()) { Collection payload = prevSpans.getPayload(); possiblePayload = new ArrayList(payload.size()); possiblePayload.addAll(payload); @@ -283,7 +294,7 @@ class NearSpansOrdered implements PayloadSpans { } } - if (possiblePayload != null) { + if (collectPayloads && possiblePayload != null) { possibleMatchPayloads.addAll(possiblePayload); } @@ -302,7 +313,7 @@ class NearSpansOrdered implements PayloadSpans { boolean match = matchSlop <= allowedSlop; - if(match && possibleMatchPayloads.size() > 0) { + if(collectPayloads && match && possibleMatchPayloads.size() > 0) { matchPayload.addAll(possibleMatchPayloads); } diff --git a/src/java/org/apache/lucene/search/spans/PayloadSpans.java b/src/java/org/apache/lucene/search/spans/PayloadSpans.java index 96a35bd74be..8a5d7e78569 100644 --- a/src/java/org/apache/lucene/search/spans/PayloadSpans.java +++ b/src/java/org/apache/lucene/search/spans/PayloadSpans.java @@ -33,9 +33,11 @@ public interface PayloadSpans extends Spans{ * This is invalid until {@link #next()} is called for * the first time. * This method must not be called more than once after each call - * of {@link #next()}. However, payloads are loaded lazily, + * of {@link #next()}. However, most SpanQuerys load payloads lazily, * so if the payload data for the current position is not needed, - * this method may not be called at all for performance reasons.
+ * this method may not be called at all for performance reasons. + * The ordered case of SpanNearQuery does not load lazily and has + * an option to turn off payload loading.
*
* Note that the return type is a collection, thus the ordering should not be relied upon. *
diff --git a/src/java/org/apache/lucene/search/spans/SpanNearQuery.java b/src/java/org/apache/lucene/search/spans/SpanNearQuery.java index b797ed1c1ea..f4f29cb3712 100644 --- a/src/java/org/apache/lucene/search/spans/SpanNearQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanNearQuery.java @@ -39,12 +39,17 @@ public class SpanNearQuery extends SpanQuery implements Cloneable { private boolean inOrder; private String field; + private boolean collectPayloads; /** Construct a SpanNearQuery. Matches spans matching a span from each * clause, with up to slop total unmatched positions between * them. * When inOrder is true, the spans from each clause * must be * ordered as in clauses. */ public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder) { + this(clauses, slop, inOrder, true); + } + + public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder, boolean collectPayloads) { // copy clauses array into an ArrayList this.clauses = new ArrayList(clauses.length); @@ -57,7 +62,7 @@ public class SpanNearQuery extends SpanQuery implements Cloneable { } this.clauses.add(clause); } - + this.collectPayloads = collectPayloads; this.slop = slop; this.inOrder = inOrder; } @@ -126,7 +131,7 @@ public class SpanNearQuery extends SpanQuery implements Cloneable { return ((SpanQuery)clauses.get(0)).getPayloadSpans(reader); return inOrder - ? (PayloadSpans) new NearSpansOrdered(this, reader) + ? (PayloadSpans) new NearSpansOrdered(this, reader, collectPayloads) : (PayloadSpans) new NearSpansUnordered(this, reader); }