diff --git a/CHANGES.txt b/CHANGES.txt index 621c0838b2b..60352713a66 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -84,6 +84,13 @@ Changes in backwards compatibility policy be passed upon instantiation. As a result, IndexWriter was removed as a method argument from all MergePolicy methods. (Shai Erera via Mike McCandless) + + 6. LUCENE-1748: LUCENE-1001 introduced PayloadSpans, but this was a back + compat break and caused custom SpanQuery implementations to fail at runtime + in a variety of ways. This issue attempts to remedy things by causing + a compile time break on custom SpanQuery implementations and removing + the PayloadSpans class, with its functionality now moved to Spans. + (Hugh Cayless, Mark Miller) Changes in runtime behavior @@ -363,6 +370,13 @@ API Changes new QueryParser framework in Lucene 3.0, that is currently located in contrib. (see New Features 35.) (Luis Alves and Adriano Campos via Michael Busch) + +36. LUCENE-1748: LUCENE-1001 introduced PayloadSpans, but this was a back + compat break and caused custom SpanQuery implementations to fail at runtime + in a variety of ways. This issue attempts to remedy things by causing + a compile time break on custom SpanQuery implementations and removing + the PayloadSpans class, with its functionality now moved to Spans. + (Hugh Cayless, Mark Miller) Bug fixes diff --git a/common-build.xml b/common-build.xml index e6f69b0f31d..18176a1ff4e 100644 --- a/common-build.xml +++ b/common-build.xml @@ -42,7 +42,7 @@ - + diff --git a/contrib/regex/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java b/contrib/regex/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java index 10ab1fb6daa..79f2bcde19d 100644 --- a/contrib/regex/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java +++ b/contrib/regex/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java @@ -27,7 +27,6 @@ import org.apache.lucene.search.spans.SpanOrQuery; import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.search.spans.SpanTermQuery; import org.apache.lucene.search.spans.Spans; -import org.apache.lucene.search.spans.PayloadSpans; import org.apache.lucene.util.ToStringUtils; import java.io.IOException; @@ -77,10 +76,6 @@ public class SpanRegexQuery extends SpanQuery implements RegexQueryCapable { throw new UnsupportedOperationException("Query should have been rewritten"); } - public PayloadSpans getPayloadSpans(IndexReader reader) throws IOException { - throw new UnsupportedOperationException("Query should have been rewritten"); - } - public String getField() { return term.field(); } diff --git a/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java b/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java index 610ca67ce9e..2756f9cb34a 100644 --- a/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java +++ b/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java @@ -1,6 +1,5 @@ package org.apache.lucene.search.payloads; -import org.apache.lucene.search.BooleanClause; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -26,6 +25,7 @@ import java.util.List; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.DisjunctionMaxQuery; import org.apache.lucene.search.FilteredQuery; @@ -33,11 +33,11 @@ import org.apache.lucene.search.MultiPhraseQuery; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; -import org.apache.lucene.search.spans.PayloadSpans; import org.apache.lucene.search.spans.SpanNearQuery; import org.apache.lucene.search.spans.SpanOrQuery; import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.search.spans.SpanTermQuery; +import org.apache.lucene.search.spans.Spans; /** * Experimental class to get set of payloads for most standard Lucene queries. @@ -173,7 +173,7 @@ public class PayloadSpanUtil { private void getPayloads(Collection payloads, SpanQuery query) throws IOException { - PayloadSpans spans = query.getPayloadSpans(reader); + Spans spans = query.getSpans(reader); while (spans.next() == true) { if (spans.isPayloadAvailable()) { diff --git a/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java b/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java index 7314e684a0d..52a5c2cc43d 100644 --- a/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java +++ b/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java @@ -93,9 +93,6 @@ public class FieldMaskingSpanQuery extends SpanQuery { public Spans getSpans(IndexReader reader) throws IOException { return maskedQuery.getSpans(reader); } - public PayloadSpans getPayloadSpans(IndexReader reader) throws IOException { - return maskedQuery.getPayloadSpans(reader); - } /** @deprecated use {@link #extractTerms(Set)} instead. */ public Collection getTerms() { diff --git a/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java b/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java index dd42774a59f..e80b1031227 100644 --- a/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java +++ b/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Collection; @@ -48,13 +47,13 @@ import java.util.Set; *
t1 t2 .. t3      
*
      t1 .. t2 t3
*/ -class NearSpansOrdered implements PayloadSpans { +class NearSpansOrdered implements Spans { private final int allowedSlop; private boolean firstTime = true; private boolean more = false; /** The spans in the same order as the SpanNearQuery */ - private final PayloadSpans[] subSpans; + private final Spans[] subSpans; /** Indicates that all subSpans have same doc() */ private boolean inSameDoc = false; @@ -64,7 +63,7 @@ class NearSpansOrdered implements PayloadSpans { private int matchEnd = -1; private List/**/ matchPayload; - private final PayloadSpans[] subSpansByDoc; + private final Spans[] subSpansByDoc; private final Comparator spanDocComparator = new Comparator() { public int compare(Object o1, Object o2) { return ((Spans)o1).doc() - ((Spans)o2).doc(); @@ -87,11 +86,11 @@ class NearSpansOrdered implements PayloadSpans { this.collectPayloads = collectPayloads; allowedSlop = spanNearQuery.getSlop(); SpanQuery[] clauses = spanNearQuery.getClauses(); - subSpans = new PayloadSpans[clauses.length]; + subSpans = new Spans[clauses.length]; matchPayload = new LinkedList(); - subSpansByDoc = new PayloadSpans[clauses.length]; + subSpansByDoc = new Spans[clauses.length]; for (int i = 0; i < clauses.length; i++) { - subSpans[i] = clauses[i].getPayloadSpans(reader); + subSpans[i] = clauses[i].getSpans(reader); subSpansByDoc[i] = subSpans[i]; // used in toSameDoc() } query = spanNearQuery; // kept for toString() only. @@ -260,7 +259,7 @@ class NearSpansOrdered implements PayloadSpans { int lastStart = matchStart; int lastEnd = matchEnd; for (int i = subSpans.length - 2; i >= 0; i--) { - PayloadSpans prevSpans = subSpans[i]; + Spans prevSpans = subSpans[i]; if (collectPayloads && prevSpans.isPayloadAvailable()) { Collection payload = prevSpans.getPayload(); possiblePayload = new ArrayList(payload.size()); diff --git a/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java b/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java index 226b827bcc1..67e2c5ade53 100644 --- a/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java +++ b/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java @@ -27,7 +27,7 @@ import java.util.List; import java.util.Set; import java.util.HashSet; -class NearSpansUnordered implements PayloadSpans { +class NearSpansUnordered implements Spans { private SpanNearQuery query; private List ordered = new ArrayList(); // spans in query order @@ -62,13 +62,13 @@ class NearSpansUnordered implements PayloadSpans { /** Wraps a Spans, and can be used to form a linked list. */ - private class SpansCell implements PayloadSpans { - private PayloadSpans spans; + private class SpansCell implements Spans { + private Spans spans; private SpansCell next; private int length = -1; private int index; - public SpansCell(PayloadSpans spans, int index) { + public SpansCell(Spans spans, int index) { this.spans = spans; this.index = index; } @@ -124,7 +124,7 @@ class NearSpansUnordered implements PayloadSpans { queue = new CellQueue(clauses.length); for (int i = 0; i < clauses.length; i++) { SpansCell cell = - new SpansCell(clauses[i].getPayloadSpans(reader), i); + new SpansCell(clauses[i].getSpans(reader), i); ordered.add(cell); } } diff --git a/src/java/org/apache/lucene/search/spans/PayloadSpans.java b/src/java/org/apache/lucene/search/spans/PayloadSpans.java deleted file mode 100644 index 8a5d7e78569..00000000000 --- a/src/java/org/apache/lucene/search/spans/PayloadSpans.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.apache.lucene.search.spans; -/** - * 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.util.Collection; -import java.io.IOException; - - -/** - * - * WARNING: The status of the Payloads feature is experimental. - * The APIs introduced here might change in the future and will not be - * supported anymore in such a case. - * - **/ -public interface PayloadSpans extends Spans{ - /** - * Returns the payload data for the current span. - * 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, 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. - * 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. - *
- *

- * WARNING: The status of the Payloads feature is experimental. - * The APIs introduced here might change in the future and will not be - * supported anymore in such a case. - * - * @return a List of byte arrays containing the data of this payload, otherwise null if isPayloadAvailable is false - * @throws java.io.IOException - */ - // TODO: Remove warning after API has been finalized - Collection/**/ getPayload() throws IOException; - - /** - * Checks if a payload can be loaded at this position. - *

- * Payloads can only be loaded once per call to - * {@link #next()}. - *

- *

- * WARNING: The status of the Payloads feature is experimental. - * The APIs introduced here might change in the future and will not be - * supported anymore in such a case. - * - * @return true if there is a payload available at this position that can be loaded - */ - // TODO: Remove warning after API has been finalized - public boolean isPayloadAvailable(); - -} diff --git a/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java b/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java index b65233aa91e..4dbc61281c5 100644 --- a/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java @@ -74,13 +74,9 @@ public class SpanFirstQuery extends SpanQuery implements Cloneable { match.extractTerms(terms); } - public PayloadSpans getPayloadSpans(IndexReader reader) throws IOException { - return (PayloadSpans) getSpans(reader); - } - public Spans getSpans(final IndexReader reader) throws IOException { - return new PayloadSpans() { - private PayloadSpans spans = match.getPayloadSpans(reader); + return new Spans() { + private Spans spans = match.getSpans(reader); public boolean next() throws IOException { while (spans.next()) { // scan to next match diff --git a/src/java/org/apache/lucene/search/spans/SpanNearQuery.java b/src/java/org/apache/lucene/search/spans/SpanNearQuery.java index f4f29cb3712..a7509970643 100644 --- a/src/java/org/apache/lucene/search/spans/SpanNearQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanNearQuery.java @@ -125,18 +125,14 @@ public class SpanNearQuery extends SpanQuery implements Cloneable { public Spans getSpans(final IndexReader reader) throws IOException { if (clauses.size() == 0) // optimize 0-clause case - return new SpanOrQuery(getClauses()).getPayloadSpans(reader); + return new SpanOrQuery(getClauses()).getSpans(reader); if (clauses.size() == 1) // optimize 1-clause case - return ((SpanQuery)clauses.get(0)).getPayloadSpans(reader); + return ((SpanQuery)clauses.get(0)).getSpans(reader); return inOrder - ? (PayloadSpans) new NearSpansOrdered(this, reader, collectPayloads) - : (PayloadSpans) new NearSpansUnordered(this, reader); - } - - public PayloadSpans getPayloadSpans(IndexReader reader) throws IOException { - return (PayloadSpans) getSpans(reader); + ? (Spans) new NearSpansOrdered(this, reader, collectPayloads) + : (Spans) new NearSpansUnordered(this, reader); } public Query rewrite(IndexReader reader) throws IOException { diff --git a/src/java/org/apache/lucene/search/spans/SpanNotQuery.java b/src/java/org/apache/lucene/search/spans/SpanNotQuery.java index 8ae5ffb296f..2094c3a2322 100644 --- a/src/java/org/apache/lucene/search/spans/SpanNotQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanNotQuery.java @@ -75,8 +75,8 @@ public class SpanNotQuery extends SpanQuery implements Cloneable { } public Spans getSpans(final IndexReader reader) throws IOException { - return new PayloadSpans() { - private PayloadSpans includeSpans = include.getPayloadSpans(reader); + return new Spans() { + private Spans includeSpans = include.getSpans(reader); private boolean moreInclude = true; private Spans excludeSpans = exclude.getSpans(reader); @@ -157,10 +157,6 @@ public class SpanNotQuery extends SpanQuery implements Cloneable { }; } - public PayloadSpans getPayloadSpans(IndexReader reader) throws IOException { - return (PayloadSpans) getSpans(reader); - } - public Query rewrite(IndexReader reader) throws IOException { SpanNotQuery clone = null; diff --git a/src/java/org/apache/lucene/search/spans/SpanOrQuery.java b/src/java/org/apache/lucene/search/spans/SpanOrQuery.java index 2171f59c7b0..5af10626874 100644 --- a/src/java/org/apache/lucene/search/spans/SpanOrQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanOrQuery.java @@ -167,22 +167,18 @@ public class SpanOrQuery extends SpanQuery implements Cloneable { } } - public PayloadSpans getPayloadSpans(final IndexReader reader) throws IOException { - return (PayloadSpans)getSpans(reader); - } - public Spans getSpans(final IndexReader reader) throws IOException { if (clauses.size() == 1) // optimize 1-clause case - return ((SpanQuery)clauses.get(0)).getPayloadSpans(reader); + return ((SpanQuery)clauses.get(0)).getSpans(reader); - return new PayloadSpans() { + return new Spans() { private SpanQueue queue = null; private boolean initSpanQueue(int target) throws IOException { queue = new SpanQueue(clauses.size()); Iterator i = clauses.iterator(); while (i.hasNext()) { - PayloadSpans spans = ((SpanQuery)i.next()).getPayloadSpans(reader); + Spans spans = ((SpanQuery)i.next()).getSpans(reader); if ( ((target == -1) && spans.next()) || ((target != -1) && spans.skipTo(target))) { queue.put(spans); @@ -209,7 +205,7 @@ public class SpanOrQuery extends SpanQuery implements Cloneable { return queue.size() != 0; } - private PayloadSpans top() { return (PayloadSpans)queue.top(); } + private Spans top() { return (Spans)queue.top(); } public boolean skipTo(int target) throws IOException { if (queue == null) { @@ -239,7 +235,7 @@ public class SpanOrQuery extends SpanQuery implements Cloneable { // TODO: Remove warning after API has been finalized public Collection/**/ getPayload() throws IOException { ArrayList result = null; - PayloadSpans theTop = top(); + Spans theTop = top(); if (theTop != null && theTop.isPayloadAvailable()) { result = new ArrayList(theTop.getPayload()); } @@ -248,7 +244,7 @@ public class SpanOrQuery extends SpanQuery implements Cloneable { // TODO: Remove warning after API has been finalized public boolean isPayloadAvailable() { - PayloadSpans top = top(); + Spans top = top(); return top != null && top.isPayloadAvailable(); } diff --git a/src/java/org/apache/lucene/search/spans/SpanQuery.java b/src/java/org/apache/lucene/search/spans/SpanQuery.java index ee290d307db..c3e76e42f55 100644 --- a/src/java/org/apache/lucene/search/spans/SpanQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanQuery.java @@ -32,22 +32,6 @@ public abstract class SpanQuery extends Query { * to search for spans. */ public abstract Spans getSpans(IndexReader reader) throws IOException; - /** - * Returns the matches for this query in an index, including access to any {@link org.apache.lucene.index.Payload}s at those - * positions. Implementing classes that want access to the payloads will need to implement this. - * @param reader The {@link org.apache.lucene.index.IndexReader} to use to get spans/payloads - * @return null - * @throws IOException if there is an error accessing the payload - * - * - * WARNING: The status of the Payloads feature is experimental. - * The APIs introduced here might change in the future and will not be - * supported anymore in such a case. - */ - public PayloadSpans getPayloadSpans(IndexReader reader) throws IOException{ - return null; - } - /** Returns the name of the field matched by this query.*/ public abstract String getField(); diff --git a/src/java/org/apache/lucene/search/spans/SpanTermQuery.java b/src/java/org/apache/lucene/search/spans/SpanTermQuery.java index 2d60ffd2a3d..e37d2d44890 100644 --- a/src/java/org/apache/lucene/search/spans/SpanTermQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanTermQuery.java @@ -79,9 +79,4 @@ public class SpanTermQuery extends SpanQuery { return new TermSpans(reader.termPositions(term), term); } - - public PayloadSpans getPayloadSpans(IndexReader reader) throws IOException { - return (PayloadSpans) getSpans(reader); - } - } diff --git a/src/java/org/apache/lucene/search/spans/Spans.java b/src/java/org/apache/lucene/search/spans/Spans.java index 730a3889784..1e00a8664ca 100644 --- a/src/java/org/apache/lucene/search/spans/Spans.java +++ b/src/java/org/apache/lucene/search/spans/Spans.java @@ -18,6 +18,7 @@ package org.apache.lucene.search.spans; */ import java.io.IOException; +import java.util.Collection; /** Expert: an enumeration of span matches. Used to implement span searching. * Each span represents a range of term positions within a document. Matches @@ -50,5 +51,46 @@ public interface Spans { /** Returns the end position of the current match. Initially invalid. */ int end(); + + /** + * Returns the payload data for the current span. + * 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, most payloads are loaded lazily, + * so if the payload data for the current position is not needed, + * this method may not be called at all for performance reasons. An ordered + * SpanQuery does not lazy load, so if you have payloads in your index and + * you do not want ordered SpanNearQuerys to collect payloads, you can + * disable collection with a constructor option.
+ *
+ * Note that the return type is a collection, thus the ordering should not be relied upon. + *
+ *

+ * WARNING: The status of the Payloads feature is experimental. + * The APIs introduced here might change in the future and will not be + * supported anymore in such a case. + * + * @return a List of byte arrays containing the data of this payload, otherwise null if isPayloadAvailable is false + * @throws java.io.IOException + */ + // TODO: Remove warning after API has been finalized + Collection/**/ getPayload() throws IOException; + + /** + * Checks if a payload can be loaded at this position. + *

+ * Payloads can only be loaded once per call to + * {@link #next()}. + *

+ *

+ * WARNING: The status of the Payloads feature is experimental. + * The APIs introduced here might change in the future and will not be + * supported anymore in such a case. + * + * @return true if there is a payload available at this position that can be loaded + */ + // TODO: Remove warning after API has been finalized + public boolean isPayloadAvailable(); } diff --git a/src/java/org/apache/lucene/search/spans/TermSpans.java b/src/java/org/apache/lucene/search/spans/TermSpans.java index ec4efb1ed13..7127f56e8dc 100644 --- a/src/java/org/apache/lucene/search/spans/TermSpans.java +++ b/src/java/org/apache/lucene/search/spans/TermSpans.java @@ -27,7 +27,7 @@ import java.util.Collection; * Expert: * Public for extension only */ -public class TermSpans implements PayloadSpans { +public class TermSpans implements Spans { protected TermPositions positions; protected Term term; protected int doc; diff --git a/src/test/org/apache/lucene/search/TestPositionIncrement.java b/src/test/org/apache/lucene/search/TestPositionIncrement.java index 9d82e416cba..4056c2dfba5 100644 --- a/src/test/org/apache/lucene/search/TestPositionIncrement.java +++ b/src/test/org/apache/lucene/search/TestPositionIncrement.java @@ -45,7 +45,6 @@ import org.apache.lucene.analysis.LowerCaseTokenizer; import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.index.Payload; import org.apache.lucene.search.payloads.PayloadSpanUtil; -import org.apache.lucene.search.spans.PayloadSpans; import org.apache.lucene.search.spans.SpanNearQuery; import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.search.spans.SpanTermQuery; @@ -279,7 +278,7 @@ public class TestPositionIncrement extends LuceneTestCase { count = 0; boolean sawZero = false; //System.out.println("\ngetPayloadSpans test"); - PayloadSpans pspans = snq.getPayloadSpans(is.getIndexReader()); + Spans pspans = snq.getSpans(is.getIndexReader()); while (pspans.next()) { //System.out.println(pspans.doc() + " - " + pspans.start() + " - "+ pspans.end()); Collection payloads = pspans.getPayload(); diff --git a/src/test/org/apache/lucene/search/spans/JustCompileSearchSpans.java b/src/test/org/apache/lucene/search/spans/JustCompileSearchSpans.java index 38e2fe3aff2..59dbf58eb2a 100644 --- a/src/test/org/apache/lucene/search/spans/JustCompileSearchSpans.java +++ b/src/test/org/apache/lucene/search/spans/JustCompileSearchSpans.java @@ -57,6 +57,14 @@ final class JustCompileSearchSpans { public int start() { throw new UnsupportedOperationException(UNSUPPORTED_MSG); } + + public Collection getPayload() throws IOException { + throw new UnsupportedOperationException(UNSUPPORTED_MSG); + } + + public boolean isPayloadAvailable() { + throw new UnsupportedOperationException(UNSUPPORTED_MSG); + } } @@ -81,7 +89,7 @@ final class JustCompileSearchSpans { } - static final class JustCompilePayloadSpans implements PayloadSpans { + static final class JustCompilePayloadSpans implements Spans { public Collection getPayload() throws IOException { throw new UnsupportedOperationException(UNSUPPORTED_MSG); diff --git a/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java b/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java index 3bd6d0312be..26beb01be9f 100644 --- a/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java +++ b/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java @@ -26,7 +26,6 @@ import java.util.Set; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.LowerCaseTokenizer; -import org.apache.lucene.analysis.Token; import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.PayloadAttribute; @@ -51,7 +50,7 @@ import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.LuceneTestCase; public class TestPayloadSpans extends LuceneTestCase { - private final static boolean DEBUG = false; + private final static boolean DEBUG = true; private IndexSearcher searcher; private Similarity similarity = new DefaultSimilarity(); protected IndexReader indexReader; @@ -73,14 +72,14 @@ public class TestPayloadSpans extends LuceneTestCase { public void testSpanTermQuery() throws Exception { SpanTermQuery stq; - PayloadSpans spans; + Spans spans; stq = new SpanTermQuery(new Term(PayloadHelper.FIELD, "seventy")); - spans = stq.getPayloadSpans(indexReader); + spans = stq.getSpans(indexReader); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 100, 1, 1, 1); stq = new SpanTermQuery(new Term(PayloadHelper.NO_PAYLOAD_FIELD, "seventy")); - spans = stq.getPayloadSpans(indexReader); + spans = stq.getSpans(indexReader); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 100, 0, 0, 0); } @@ -91,7 +90,7 @@ public class TestPayloadSpans extends LuceneTestCase { SpanFirstQuery sfq; match = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one")); sfq = new SpanFirstQuery(match, 2); - PayloadSpans spans = sfq.getPayloadSpans(indexReader); + Spans spans = sfq.getSpans(indexReader); checkSpans(spans, 109, 1, 1, 1); //Test more complicated subclause SpanQuery[] clauses = new SpanQuery[2]; @@ -99,20 +98,49 @@ public class TestPayloadSpans extends LuceneTestCase { clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "hundred")); match = new SpanNearQuery(clauses, 0, true); sfq = new SpanFirstQuery(match, 2); - checkSpans(sfq.getPayloadSpans(indexReader), 100, 2, 1, 1); + checkSpans(sfq.getSpans(indexReader), 100, 2, 1, 1); match = new SpanNearQuery(clauses, 0, false); sfq = new SpanFirstQuery(match, 2); - checkSpans(sfq.getPayloadSpans(indexReader), 100, 2, 1, 1); + checkSpans(sfq.getSpans(indexReader), 100, 2, 1, 1); } + public void testSpanNot() throws Exception { + SpanQuery[] clauses = new SpanQuery[2]; + clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one")); + clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three")); + SpanQuery spq = new SpanNearQuery(clauses, 5, true); + SpanNotQuery snq = new SpanNotQuery(spq, new SpanTermQuery(new Term(PayloadHelper.FIELD, "two"))); + checkSpans(snq.getSpans(getSpanNotSearcher().getIndexReader()), 1,new int[]{2}); + } + + public IndexSearcher getSpanNotSearcher() + throws IOException { + RAMDirectory directory = new RAMDirectory(); + PayloadAnalyzer analyzer = new PayloadAnalyzer(); + IndexWriter writer = new IndexWriter(directory, analyzer, true); + writer.setSimilarity(similarity); + + Document doc = new Document(); + doc.add(new Field(PayloadHelper.FIELD, "one two three one four three", + Field.Store.YES, Field.Index.ANALYZED)); + writer.addDocument(doc); + + writer.close(); + + IndexSearcher searcher = new IndexSearcher(directory); + searcher.setSimilarity(similarity); + return searcher; + + } + public void testNestedSpans() throws Exception { SpanTermQuery stq; - PayloadSpans spans; + Spans spans; IndexSearcher searcher = getSearcher(); stq = new SpanTermQuery(new Term(PayloadHelper.FIELD, "mark")); - spans = stq.getPayloadSpans(searcher.getIndexReader()); + spans = stq.getSpans(searcher.getIndexReader()); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 0, null); @@ -123,7 +151,7 @@ public class TestPayloadSpans extends LuceneTestCase { clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "xx")); SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 12, false); - spans = spanNearQuery.getPayloadSpans(searcher.getIndexReader()); + spans = spanNearQuery.getSpans(searcher.getIndexReader()); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 2, new int[]{3,3}); @@ -135,7 +163,7 @@ public class TestPayloadSpans extends LuceneTestCase { spanNearQuery = new SpanNearQuery(clauses, 6, true); - spans = spanNearQuery.getPayloadSpans(searcher.getIndexReader()); + spans = spanNearQuery.getSpans(searcher.getIndexReader()); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 1, new int[]{3}); @@ -157,13 +185,13 @@ public class TestPayloadSpans extends LuceneTestCase { // yy within 6 of xx within 6 of rr - spans = nestedSpanNearQuery.getPayloadSpans(searcher.getIndexReader()); + spans = nestedSpanNearQuery.getSpans(searcher.getIndexReader()); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 2, new int[]{3,3}); } public void testFirstClauseWithoutPayload() throws Exception { - PayloadSpans spans; + Spans spans; IndexSearcher searcher = getSearcher(); SpanQuery[] clauses = new SpanQuery[3]; @@ -187,13 +215,13 @@ public class TestPayloadSpans extends LuceneTestCase { SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false); - spans = nestedSpanNearQuery.getPayloadSpans(searcher.getIndexReader()); + spans = nestedSpanNearQuery.getSpans(searcher.getIndexReader()); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 1, new int[]{3}); } public void testHeavilyNestedSpanQuery() throws Exception { - PayloadSpans spans; + Spans spans; IndexSearcher searcher = getSearcher(); SpanQuery[] clauses = new SpanQuery[3]; @@ -222,7 +250,7 @@ public class TestPayloadSpans extends LuceneTestCase { SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false); - spans = nestedSpanNearQuery.getPayloadSpans(searcher.getIndexReader()); + spans = nestedSpanNearQuery.getSpans(searcher.getIndexReader()); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 2, new int[]{8, 8}); } @@ -243,7 +271,7 @@ public class TestPayloadSpans extends LuceneTestCase { SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k")); SpanQuery[] sqs = { stq1, stq2 }; SpanNearQuery snq = new SpanNearQuery(sqs, 1, true); - PayloadSpans spans = snq.getPayloadSpans(is.getIndexReader()); + Spans spans = snq.getSpans(is.getIndexReader()); TopDocs topDocs = is.search(snq, 1); Set payloadSet = new HashSet(); @@ -277,7 +305,7 @@ public class TestPayloadSpans extends LuceneTestCase { SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k")); SpanQuery[] sqs = { stq1, stq2 }; SpanNearQuery snq = new SpanNearQuery(sqs, 0, true); - PayloadSpans spans = snq.getPayloadSpans(is.getIndexReader()); + Spans spans = snq.getSpans(is.getIndexReader()); TopDocs topDocs = is.search(snq, 1); Set payloadSet = new HashSet(); @@ -311,7 +339,7 @@ public class TestPayloadSpans extends LuceneTestCase { SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k")); SpanQuery[] sqs = { stq1, stq2 }; SpanNearQuery snq = new SpanNearQuery(sqs, 0, true); - PayloadSpans spans = snq.getPayloadSpans(is.getIndexReader()); + Spans spans = snq.getSpans(is.getIndexReader()); TopDocs topDocs = is.search(snq, 1); Set payloadSet = new HashSet(); @@ -364,7 +392,7 @@ public class TestPayloadSpans extends LuceneTestCase { } - private void checkSpans(PayloadSpans spans, int expectedNumSpans, int expectedNumPayloads, + private void checkSpans(Spans spans, int expectedNumSpans, int expectedNumPayloads, int expectedPayloadLength, int expectedFirstByte) throws IOException { assertTrue("spans is null and it shouldn't be", spans != null); //each position match should have a span associated with it, since there is just one underlying term query, there should @@ -420,7 +448,7 @@ public class TestPayloadSpans extends LuceneTestCase { return searcher; } - private void checkSpans(PayloadSpans spans, int numSpans, int[] numPayloads) throws IOException { + private void checkSpans(Spans spans, int numSpans, int[] numPayloads) throws IOException { int cnt = 0; while (spans.next() == true) {