use of org.apache.lucene.search.suggest.tst.TSTLookup in project lucene-solr by apache.
the class PersistenceTest method runTest.
private void runTest(Class<? extends Lookup> lookupClass, boolean supportsExactWeights) throws Exception {
// Add all input keys.
Lookup lookup;
Directory tempDir = getDirectory();
if (lookupClass == TSTLookup.class) {
lookup = new TSTLookup(tempDir, "suggest");
} else if (lookupClass == FSTCompletionLookup.class) {
lookup = new FSTCompletionLookup(tempDir, "suggest");
} else {
lookup = lookupClass.newInstance();
}
Input[] keys = new Input[this.keys.length];
for (int i = 0; i < keys.length; i++) keys[i] = new Input(this.keys[i], i);
lookup.build(new InputArrayIterator(keys));
// Store the suggester.
Path storeDir = createTempDir(LuceneTestCase.getTestClass().getSimpleName());
lookup.store(Files.newOutputStream(storeDir.resolve("lookup.dat")));
// Re-read it from disk.
lookup = lookupClass.newInstance();
lookup.load(Files.newInputStream(storeDir.resolve("lookup.dat")));
// Assert validity.
Random random = random();
long previous = Long.MIN_VALUE;
for (Input k : keys) {
List<LookupResult> list = lookup.lookup(TestUtil.bytesToCharSequence(k.term, random), false, 1);
assertEquals(1, list.size());
LookupResult lookupResult = list.get(0);
assertNotNull(k.term.utf8ToString(), lookupResult.key);
if (supportsExactWeights) {
assertEquals(k.term.utf8ToString(), k.v, lookupResult.value);
} else {
assertTrue(lookupResult.value + ">=" + previous, lookupResult.value >= previous);
previous = lookupResult.value;
}
}
tempDir.close();
}
Aggregations