|
| 1 | +package com.browseengine.bobo.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 9 | +import org.apache.lucene.document.Document; |
| 10 | +import org.apache.lucene.document.Field.Store; |
| 11 | +import org.apache.lucene.document.TextField; |
| 12 | +import org.apache.lucene.index.DirectoryReader; |
| 13 | +import org.apache.lucene.index.IndexWriter; |
| 14 | +import org.apache.lucene.index.IndexWriterConfig; |
| 15 | +import org.apache.lucene.queryparser.classic.QueryParser; |
| 16 | +import org.apache.lucene.search.IndexSearcher; |
| 17 | +import org.apache.lucene.search.Query; |
| 18 | +import org.apache.lucene.search.ScoreDoc; |
| 19 | +import org.apache.lucene.search.TopDocs; |
| 20 | +import org.apache.lucene.search.TopScoreDocCollector; |
| 21 | +import org.apache.lucene.store.RAMDirectory; |
| 22 | +import org.apache.lucene.util.Version; |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import com.browseengine.bobo.api.BoboBrowser; |
| 28 | +import com.browseengine.bobo.api.BoboMultiReader; |
| 29 | +import com.browseengine.bobo.api.BrowseHit; |
| 30 | +import com.browseengine.bobo.api.BrowseRequest; |
| 31 | +import com.browseengine.bobo.api.BrowseResult; |
| 32 | +import com.browseengine.bobo.facets.FacetHandler; |
| 33 | + |
| 34 | +public class BasicIndexingTest { |
| 35 | + |
| 36 | + public BasicIndexingTest() { |
| 37 | + // TODO Auto-generated constructor stub |
| 38 | + } |
| 39 | + |
| 40 | + private IndexWriter m_indexWriter; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() throws Exception { |
| 44 | + IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, |
| 45 | + new StandardAnalyzer(Version.LUCENE_43)); |
| 46 | + config.setMaxBufferedDocs(1000); |
| 47 | + m_indexWriter = new IndexWriter(new RAMDirectory(), config); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void tearDown() throws Exception { |
| 52 | + m_indexWriter.close(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + //This test FAILS |
| 57 | + public void testWithInterleavedCommitsUsingBobo() throws Exception { |
| 58 | + String text = "text"; |
| 59 | + |
| 60 | + Document doc1 = new Document(); |
| 61 | + doc1.add(new TextField(text, "Foo1", Store.YES)); |
| 62 | + m_indexWriter.addDocument(doc1); |
| 63 | + m_indexWriter.commit(); |
| 64 | + |
| 65 | + Document doc2 = new Document(); |
| 66 | + doc2.add(new TextField(text, "Foo2", Store.YES)); |
| 67 | + m_indexWriter.addDocument(doc2); |
| 68 | + m_indexWriter.commit(); |
| 69 | + |
| 70 | + Document doc3 = new Document(); |
| 71 | + doc3.add(new TextField(text, "Foo3", Store.YES)); |
| 72 | + m_indexWriter.addDocument(doc3); |
| 73 | + m_indexWriter.commit(); |
| 74 | + |
| 75 | + List<FacetHandler<?>> handlerList = Arrays |
| 76 | + .asList(new FacetHandler<?>[] {}); |
| 77 | + |
| 78 | + DirectoryReader reader = BoboMultiReader.open(m_indexWriter, true); |
| 79 | + |
| 80 | + BoboMultiReader boboMultiReader = BoboMultiReader.getInstance(reader, |
| 81 | + handlerList); |
| 82 | + |
| 83 | + BrowseRequest br = new BrowseRequest(); |
| 84 | + br.setCount(10); |
| 85 | + br.setOffset(0); |
| 86 | + |
| 87 | + QueryParser parser = new QueryParser(Version.LUCENE_43, "text", |
| 88 | + new StandardAnalyzer(Version.LUCENE_43)); |
| 89 | + Query q = parser.parse("Foo*"); |
| 90 | + br.setQuery(q); |
| 91 | + |
| 92 | + BoboBrowser browser = new BoboBrowser(boboMultiReader); |
| 93 | + BrowseResult result = browser.browse(br); |
| 94 | + |
| 95 | + int totalHits = result.getNumHits(); |
| 96 | + BrowseHit[] hits = result.getHits(); |
| 97 | + |
| 98 | + assertEquals("should be 3 hits", 3, totalHits); |
| 99 | + assertEquals("should be doc 0", 0, hits[0].getDocid()); |
| 100 | + assertEquals("should be doc 1", 1, hits[1].getDocid()); // <-- This is |
| 101 | + // where the |
| 102 | + // test fails, |
| 103 | + // because all |
| 104 | + // three browser |
| 105 | + // hits are |
| 106 | + // returned with |
| 107 | + // doc id 0 |
| 108 | + assertEquals("should be doc 2", 2, hits[2].getDocid()); |
| 109 | + |
| 110 | + result.close(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + //This test PASSES |
| 115 | + public void testWithSingleCommit() throws Exception { |
| 116 | + String text = "text"; |
| 117 | + |
| 118 | + Document doc1 = new Document(); |
| 119 | + doc1.add(new TextField(text, "Foo1", Store.YES)); |
| 120 | + m_indexWriter.addDocument(doc1); |
| 121 | + |
| 122 | + Document doc2 = new Document(); |
| 123 | + doc2.add(new TextField(text, "Foo2", Store.YES)); |
| 124 | + m_indexWriter.addDocument(doc2); |
| 125 | + |
| 126 | + Document doc3 = new Document(); |
| 127 | + doc3.add(new TextField(text, "Foo3", Store.YES)); |
| 128 | + m_indexWriter.addDocument(doc3); |
| 129 | + |
| 130 | + m_indexWriter.commit(); |
| 131 | + |
| 132 | + List<FacetHandler<?>> handlerList = Arrays |
| 133 | + .asList(new FacetHandler<?>[] {}); |
| 134 | + |
| 135 | + DirectoryReader reader = BoboMultiReader.open(m_indexWriter, true); |
| 136 | + BoboMultiReader boboMultiReader = BoboMultiReader.getInstance(reader, |
| 137 | + handlerList); |
| 138 | + |
| 139 | + BrowseRequest br = new BrowseRequest(); |
| 140 | + br.setCount(10); |
| 141 | + br.setOffset(0); |
| 142 | + |
| 143 | + QueryParser parser = new QueryParser(Version.LUCENE_43, "text", |
| 144 | + new StandardAnalyzer(Version.LUCENE_43)); |
| 145 | + Query q = parser.parse("Foo*"); |
| 146 | + br.setQuery(q); |
| 147 | + |
| 148 | + BoboBrowser browser = new BoboBrowser(boboMultiReader); |
| 149 | + BrowseResult result = browser.browse(br); |
| 150 | + |
| 151 | + int totalHits = result.getNumHits(); |
| 152 | + BrowseHit[] hits = result.getHits(); |
| 153 | + |
| 154 | + assertEquals("should be 3 hits", 3, totalHits); |
| 155 | + assertEquals("should be doc 0", 0, hits[0].getDocid()); |
| 156 | + assertEquals("should be doc 1", 1, hits[1].getDocid()); |
| 157 | + assertEquals("should be doc 2", 2, hits[2].getDocid()); |
| 158 | + |
| 159 | + result.close(); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + //This test PASSES |
| 164 | + public void testWithInterleavedCommitsUsingLuceneQuery() throws Exception { |
| 165 | + String text = "text"; |
| 166 | + |
| 167 | + Document doc1 = new Document(); |
| 168 | + doc1.add(new TextField(text, "Foo1", Store.YES)); |
| 169 | + m_indexWriter.addDocument(doc1); |
| 170 | + m_indexWriter.commit(); |
| 171 | + |
| 172 | + Document doc2 = new Document(); |
| 173 | + doc2.add(new TextField(text, "Foo2", Store.YES)); |
| 174 | + m_indexWriter.addDocument(doc2); |
| 175 | + m_indexWriter.commit(); |
| 176 | + |
| 177 | + Document doc3 = new Document(); |
| 178 | + doc3.add(new TextField(text, "Foo3", Store.YES)); |
| 179 | + m_indexWriter.addDocument(doc3); |
| 180 | + m_indexWriter.commit(); |
| 181 | + |
| 182 | + DirectoryReader reader = DirectoryReader.open(m_indexWriter, true); |
| 183 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 184 | + TopScoreDocCollector docCollector = TopScoreDocCollector.create(100, |
| 185 | + true); |
| 186 | + QueryParser queryParser = new QueryParser(Version.LUCENE_43, "text", |
| 187 | + new StandardAnalyzer(Version.LUCENE_43)); |
| 188 | + Query query = queryParser.parse("Foo*"); |
| 189 | + searcher.search(query, docCollector); |
| 190 | + TopDocs docs = docCollector.topDocs(); |
| 191 | + ScoreDoc[] scoreDocs = docs.scoreDocs; |
| 192 | + |
| 193 | + assertEquals("should be doc 0", 0, scoreDocs[0].doc); |
| 194 | + assertEquals("should be doc 1", 1, scoreDocs[1].doc); |
| 195 | + assertEquals("should be doc 2", 2, scoreDocs[2].doc); |
| 196 | + |
| 197 | + reader.close(); |
| 198 | + } |
| 199 | +} |
0 commit comments