|
30 | 30 | import org.apache.lucene.index.IndexWriter;
|
31 | 31 | import org.apache.lucene.index.IndexWriterConfig;
|
32 | 32 | import org.apache.lucene.index.Term;
|
| 33 | +import org.apache.lucene.search.BooleanClause; |
| 34 | +import org.apache.lucene.search.BooleanQuery; |
| 35 | +import org.apache.lucene.search.Query; |
| 36 | +import org.apache.lucene.search.TermQuery; |
33 | 37 | import org.apache.lucene.store.Directory;
|
34 | 38 | import org.apache.lucene.store.FSDirectory;
|
| 39 | +import org.apache.lucene.util.BytesRef; |
35 | 40 | import org.junit.Test;
|
36 | 41 | import org.opengrok.suggest.query.SuggesterPrefixQuery;
|
| 42 | +import org.opengrok.suggest.query.SuggesterWildcardQuery; |
37 | 43 |
|
38 | 44 | import java.io.IOException;
|
39 | 45 | import java.nio.file.Files;
|
40 | 46 | import java.nio.file.Path;
|
41 | 47 | import java.time.Duration;
|
| 48 | +import java.util.AbstractMap.SimpleEntry; |
42 | 49 | import java.util.Collections;
|
43 | 50 | import java.util.List;
|
44 | 51 | import java.util.Map;
|
| 52 | +import java.util.Map.Entry; |
45 | 53 | import java.util.concurrent.TimeUnit;
|
46 | 54 | import java.util.stream.Collectors;
|
47 | 55 |
|
48 | 56 | import static org.awaitility.Awaitility.await;
|
49 | 57 | import static org.hamcrest.MatcherAssert.assertThat;
|
50 | 58 | import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
|
| 59 | +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; |
51 | 60 | import static org.junit.Assert.assertFalse;
|
| 61 | +import static org.junit.Assert.assertTrue; |
52 | 62 |
|
53 | 63 | public class SuggesterTest {
|
54 | 64 |
|
@@ -114,7 +124,7 @@ private SuggesterTestData initSuggester() throws IOException {
|
114 | 124 |
|
115 | 125 | Path tempSuggesterDir = Files.createTempDirectory("opengrok");
|
116 | 126 |
|
117 |
| - Suggester s = new Suggester(tempSuggesterDir.toFile(), 10, Duration.ofMinutes(1), false, |
| 127 | + Suggester s = new Suggester(tempSuggesterDir.toFile(), 10, Duration.ofMinutes(1), true, |
118 | 128 | true, Collections.singleton("test"));
|
119 | 129 |
|
120 | 130 | s.init(Collections.singleton(new Suggester.NamedIndexDir("test", tempIndexDir)));
|
@@ -216,4 +226,58 @@ public void testRemove() throws IOException {
|
216 | 226 | FileUtils.deleteDirectory(t.indexDir.toFile());
|
217 | 227 | }
|
218 | 228 |
|
| 229 | + @Test |
| 230 | + public void testComplexQuerySearch() throws IOException { |
| 231 | + SuggesterTestData t = initSuggester(); |
| 232 | + |
| 233 | + List<LookupResultItem> res = t.s.search(Collections.singletonList(t.getNamedIndexReader()), |
| 234 | + new SuggesterWildcardQuery(new Term("test", "*1")), null); |
| 235 | + |
| 236 | + assertThat(res.stream().map(LookupResultItem::getPhrase).collect(Collectors.toList()), |
| 237 | + contains("term1")); |
| 238 | + |
| 239 | + t.close(); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + public void testOnSearch() throws IOException { |
| 244 | + SuggesterTestData t = initSuggester(); |
| 245 | + |
| 246 | + Query q = new BooleanQuery.Builder() |
| 247 | + .add(new TermQuery(new Term("test", "term1")), BooleanClause.Occur.MUST) |
| 248 | + .add(new TermQuery(new Term("test", "term3")), BooleanClause.Occur.MUST) |
| 249 | + .build(); |
| 250 | + |
| 251 | + t.s.onSearch(Collections.singleton("test"), q); |
| 252 | + |
| 253 | + List<Entry<BytesRef, Integer>> res = t.s.getSearchCounts("test", "test", 0, 10); |
| 254 | + |
| 255 | + assertThat(res, containsInAnyOrder(new SimpleEntry<>(new BytesRef("term1"), 1), |
| 256 | + new SimpleEntry<>(new BytesRef("term3"), 1))); |
| 257 | + |
| 258 | + t.close(); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void testGetSearchCountsForUnknown() throws IOException { |
| 263 | + SuggesterTestData t = initSuggester(); |
| 264 | + |
| 265 | + assertTrue(t.s.getSearchCounts("unknown", "unknown", 0, 10).isEmpty()); |
| 266 | + |
| 267 | + t.close(); |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + public void testIncreaseSearchCount() throws IOException { |
| 272 | + SuggesterTestData t = initSuggester(); |
| 273 | + |
| 274 | + t.s.increaseSearchCount("test", new Term("test", "term2"), 100); |
| 275 | + |
| 276 | + List<Entry<BytesRef, Integer>> res = t.s.getSearchCounts("test", "test", 0, 10); |
| 277 | + |
| 278 | + assertThat(res, contains(new SimpleEntry<>(new BytesRef("term2"), 100))); |
| 279 | + |
| 280 | + t.close(); |
| 281 | + } |
| 282 | + |
219 | 283 | }
|
0 commit comments