LUCENE-6984: SpanMultiTermQueryWrapper no longer modifies its wrapped query

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1725719 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alan Woodward 2016-01-20 13:36:56 +00:00
parent d66dd2f9f0
commit e5de90bbf1
3 changed files with 38 additions and 17 deletions

View File

@ -194,6 +194,9 @@ Bug Fixes
long document in SimilarityBase or BM25Similarity. Add more warnings to sims
that will not work well with extreme tf values. (Ahmet Arslan, Robert Muir)
* LUCENE-6984: SpanMultiTermQueryWrapper no longer modifies its wrapped query.
(Alan Woodward, Adrien Grand)
Other
* LUCENE-6924: Upgrade randomizedtesting to 2.3.2. (Dawid Weiss)

View File

@ -17,6 +17,11 @@ package org.apache.lucene.search.spans;
* limitations under the License.
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext;
@ -27,11 +32,6 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoringRewrite;
import org.apache.lucene.search.TopTermsRewrite;
import java.io.IOException;
import java.util.Objects;
import java.util.List;
import java.util.ArrayList;
/**
* Wraps any {@link MultiTermQuery} as a {@link SpanQuery},
* so it can be nested within other SpanQuery classes.
@ -49,7 +49,9 @@ import java.util.ArrayList;
* </pre></blockquote>
*/
public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQuery {
protected final Q query;
private SpanRewriteMethod rewriteMethod;
/**
* Create a new SpanMultiTermQueryWrapper.
@ -64,13 +66,16 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
@SuppressWarnings({"rawtypes","unchecked"})
public SpanMultiTermQueryWrapper(Q query) {
this.query = Objects.requireNonNull(query);
this.rewriteMethod = selectRewriteMethod(query);
}
private static SpanRewriteMethod selectRewriteMethod(MultiTermQuery query) {
MultiTermQuery.RewriteMethod method = query.getRewriteMethod();
if (method instanceof TopTermsRewrite) {
final int pqsize = ((TopTermsRewrite) method).getSize();
setRewriteMethod(new TopTermsSpanBooleanQueryRewrite(pqsize));
return new TopTermsSpanBooleanQueryRewrite(pqsize);
} else {
setRewriteMethod(SCORING_SPAN_QUERY_REWRITE);
return SCORING_SPAN_QUERY_REWRITE;
}
}
@ -78,10 +83,7 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
* Expert: returns the rewriteMethod
*/
public final SpanRewriteMethod getRewriteMethod() {
final MultiTermQuery.RewriteMethod m = query.getRewriteMethod();
if (!(m instanceof SpanRewriteMethod))
throw new UnsupportedOperationException("You can only use SpanMultiTermQueryWrapper with a suitable SpanRewriteMethod.");
return (SpanRewriteMethod) m;
return rewriteMethod;
}
/**
@ -89,7 +91,7 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
* to be a span rewrite method.
*/
public final void setRewriteMethod(SpanRewriteMethod rewriteMethod) {
query.setRewriteMethod(rewriteMethod);
this.rewriteMethod = rewriteMethod;
}
@Override
@ -121,10 +123,7 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
@Override
public Query rewrite(IndexReader reader) throws IOException {
final Query q = query.rewrite(reader);
if (!(q instanceof SpanQuery))
throw new UnsupportedOperationException("You can only use SpanMultiTermQueryWrapper with a suitable SpanRewriteMethod.");
return q;
return rewriteMethod.rewrite(reader, query);
}
@Override

View File

@ -17,6 +17,8 @@ package org.apache.lucene.search.spans;
* limitations under the License.
*/
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
@ -24,11 +26,13 @@ import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.Test;
/**
* Tests for {@link SpanMultiTermQueryWrapper}, wrapping a few MultiTermQueries.
@ -226,4 +230,19 @@ public class TestSpanMultiTermQueryWrapper extends LuceneTestCase {
spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
}
@Test
public void testWrappedQueryIsNotModified() {
final PrefixQuery pq = new PrefixQuery(new Term("field", "test"));
int pqHash = pq.hashCode();
SpanMultiTermQueryWrapper<PrefixQuery> wrapper = new SpanMultiTermQueryWrapper<>(pq);
assertEquals(pqHash, pq.hashCode());
wrapper.setRewriteMethod(new SpanMultiTermQueryWrapper.SpanRewriteMethod() {
@Override
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
return null;
}
});
assertEquals(pqHash, pq.hashCode());
}
}