From 7ff11fc34d2dd9cb1cd69dbd6088941ddb592968 Mon Sep 17 00:00:00 2001 From: Daniel Naber Date: Wed, 19 Jan 2005 23:32:52 +0000 Subject: [PATCH] fix for bug 33161 (UnsupportedOperationException for some combinations of BooleanQuery and MultiPhraseQuery) PR: 33161 Submitted by: Rhett Sutphin git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150733 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/search/MultiPhraseQuery.java | 9 +++- .../lucene/search/TestMultiPhraseQuery.java | 43 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/java/org/apache/lucene/search/MultiPhraseQuery.java b/src/java/org/apache/lucene/search/MultiPhraseQuery.java index b20d0871883..f92d2038a02 100644 --- a/src/java/org/apache/lucene/search/MultiPhraseQuery.java +++ b/src/java/org/apache/lucene/search/MultiPhraseQuery.java @@ -225,7 +225,7 @@ public class MultiPhraseQuery extends Query { } } - protected Weight createWeight(Searcher searcher) { + public Query rewrite(IndexReader reader) { if (termArrays.size() == 1) { // optimize one-term case Term[] terms = (Term[])termArrays.get(0); BooleanQuery boq = new BooleanQuery(); @@ -233,8 +233,13 @@ public class MultiPhraseQuery extends Query { boq.add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD); } boq.setBoost(getBoost()); - return boq.createWeight(searcher); + return boq; + } else { + return this; } + } + + protected Weight createWeight(Searcher searcher) { return new MultiPhraseWeight(searcher); } diff --git a/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java b/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java index 5f40b315ed1..227cf3c636e 100644 --- a/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java +++ b/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java @@ -39,13 +39,11 @@ import java.util.LinkedList; */ public class TestMultiPhraseQuery extends TestCase { - public TestMultiPhraseQuery(String name) - { + public TestMultiPhraseQuery(String name) { super(name); } - public void testPhrasePrefix() throws IOException - { + public void testPhrasePrefix() throws IOException { RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); add("blueberry pie", writer); @@ -128,10 +126,43 @@ public class TestMultiPhraseQuery extends TestCase } - private void add(String s, IndexWriter writer) throws IOException - { + private void add(String s, IndexWriter writer) throws IOException { Document doc = new Document(); doc.add(new Field("body", s, Field.Store.YES, Field.Index.TOKENIZED)); writer.addDocument(doc); } + + public void testBooleanQueryContainingSingleTermPrefixQuery() throws IOException { + // this tests against bug 33161 (now fixed) + // In order to cause the bug, the outer query must have more than one term + // and all terms required. + // The contained PhraseMultiQuery must contain exactly one term array. + + RAMDirectory indexStore = new RAMDirectory(); + IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); + add("blueberry pie", writer); + add("blueberry chewing gum", writer); + add("blue raspberry pie", writer); + writer.optimize(); + writer.close(); + + IndexSearcher searcher = new IndexSearcher(indexStore); + // This query will be equivalent to +body:pie +body:"blue*" + BooleanQuery q = new BooleanQuery(); + q.add(new TermQuery(new Term("body", "pie")), BooleanClause.Occur.MUST); + + MultiPhraseQuery trouble = new MultiPhraseQuery(); + trouble.add(new Term[] { + new Term("body", "blueberry"), + new Term("body", "blue") + }); + q.add(trouble, BooleanClause.Occur.MUST); + + // exception will be thrown here without fix + Hits hits = searcher.search(q); + + assertEquals("Wrong number of hits", 2, hits.length()); + searcher.close(); + } + }