diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/standard/TestStandardFactories.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/standard/TestStandardFactories.java index c88abbe2adf..83feff29cd2 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/standard/TestStandardFactories.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/standard/TestStandardFactories.java @@ -17,12 +17,14 @@ package org.apache.lucene.analysis.standard; * limitations under the License. */ +import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.BaseTokenStreamTestCase; import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.analysis.TokenStream; @@ -32,6 +34,23 @@ import org.apache.lucene.analysis.core.LetterTokenizerFactory; import org.apache.lucene.analysis.core.LowerCaseTokenizerFactory; import org.apache.lucene.analysis.core.WhitespaceTokenizerFactory; import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilterFactory; +import org.apache.lucene.analysis.util.CharArraySet; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.TopScoreDocCollector; +import org.apache.lucene.search.spans.SpanQuery; +import org.apache.lucene.search.spans.SpanTermQuery; +import org.apache.lucene.store.Directory; /** * Simple tests to ensure the standard lucene factories are working. @@ -188,4 +207,48 @@ public class TestStandardFactories extends BaseTokenStreamTestCase { TokenStream stream = factory.create(tokenizer); assertTokenStreamContents(stream, new String[] { "Ceska" }); } + + public void testBooleanSpanQuery() throws Exception { + boolean failed = false; + int hits = 0; + Directory directory = newDirectory(); + try{ + CharArraySet stops = CharArraySet.EMPTY_SET; + Analyzer indexerAnalyzer = new StandardAnalyzer(TEST_VERSION_CURRENT, stops); + //Analyzer indexerAnalyzer = new MockAnalyzer(LuceneProjectVersion.LUCENE_VERSION); + //Directory directory = new RAMDirectory(); + + IndexWriterConfig config = new IndexWriterConfig(TEST_VERSION_CURRENT, indexerAnalyzer); + IndexWriter writer = new IndexWriter(directory, config); + String FIELD = "content"; + Document d = new Document(); + d.add(new TextField(FIELD, "clockwork orange", Field.Store.YES)); + writer.addDocument(d); + writer.close(); + + IndexReader indexReader = DirectoryReader.open(directory); + IndexSearcher searcher = new IndexSearcher(indexReader); + + BooleanQuery query = new BooleanQuery(); + SpanQuery sq1 = new SpanTermQuery(new Term(FIELD, "clockwork")); + SpanQuery sq2 = new SpanTermQuery(new Term(FIELD, "clckwork")); + query.add(sq1, BooleanClause.Occur.SHOULD); + query.add(sq2, BooleanClause.Occur.SHOULD); + TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); + searcher.search(query, collector); + hits = collector.topDocs().scoreDocs.length; + for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs){ + System.out.println(scoreDoc.doc); + } + indexReader.close(); + } catch (java.lang.ArrayIndexOutOfBoundsException e){ + failed = true; + } catch (IOException e){ + + e.printStackTrace(); + } + assertEquals("Bug in boolean query composed of span queries", failed, false); + assertEquals("Bug in boolean query composed of span queries", hits, 1); + directory.close(); + } } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java index 28dc800ab5b..1eaa747fcf0 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java @@ -24,14 +24,21 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; +import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.MultiReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.similarities.DefaultSimilarity; +import org.apache.lucene.search.spans.SpanQuery; +import org.apache.lucene.search.spans.SpanTermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.NamedThreadFactory; @@ -283,4 +290,40 @@ public class TestBooleanQuery extends LuceneTestCase { r.close(); d.close(); } + + // LUCENE-4477 / LUCENE-4401: + public void testBooleanSpanQuery() throws Exception { + boolean failed = false; + int hits = 0; + Directory directory = newDirectory(); + Analyzer indexerAnalyzer = new MockAnalyzer(random()); + + IndexWriterConfig config = new IndexWriterConfig(TEST_VERSION_CURRENT, indexerAnalyzer); + IndexWriter writer = new IndexWriter(directory, config); + String FIELD = "content"; + Document d = new Document(); + d.add(new TextField(FIELD, "clockwork orange", Field.Store.YES)); + writer.addDocument(d); + writer.close(); + + IndexReader indexReader = DirectoryReader.open(directory); + IndexSearcher searcher = new IndexSearcher(indexReader); + + BooleanQuery query = new BooleanQuery(); + SpanQuery sq1 = new SpanTermQuery(new Term(FIELD, "clockwork")); + SpanQuery sq2 = new SpanTermQuery(new Term(FIELD, "clckwork")); + query.add(sq1, BooleanClause.Occur.SHOULD); + query.add(sq2, BooleanClause.Occur.SHOULD); + TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); + searcher.search(query, collector); + hits = collector.topDocs().scoreDocs.length; + for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs){ + System.out.println(scoreDoc.doc); + } + indexReader.close(); + assertEquals("Bug in boolean query composed of span queries", failed, false); + assertEquals("Bug in boolean query composed of span queries", hits, 1); + directory.close(); + } + }