use of org.apache.lucene.search.suggest.InputArrayIterator in project lucene-solr by apache.
the class BlendedInfixSuggesterTest method testNullPrefixToken.
/**
* Handle trailing spaces that result in no prefix token LUCENE-6093
*/
public void testNullPrefixToken() throws IOException {
BytesRef payload = new BytesRef("lake");
Input[] keys = new Input[] { new Input("top of the lake", 8, payload) };
Path tempDir = createTempDir("BlendedInfixSuggesterTest");
Analyzer a = new StandardAnalyzer(CharArraySet.EMPTY_SET);
BlendedInfixSuggester suggester = new BlendedInfixSuggester(newFSDirectory(tempDir), a, a, AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS, BlendedInfixSuggester.BlenderType.POSITION_LINEAR, BlendedInfixSuggester.DEFAULT_NUM_FACTOR, false);
suggester.build(new InputArrayIterator(keys));
getInResults(suggester, "of ", payload, 1);
getInResults(suggester, "the ", payload, 1);
getInResults(suggester, "lake ", payload, 1);
suggester.close();
}
use of org.apache.lucene.search.suggest.InputArrayIterator in project lucene-solr by apache.
the class BlendedInfixSuggesterTest method rying.
public void rying() throws IOException {
BytesRef lake = new BytesRef("lake");
BytesRef star = new BytesRef("star");
BytesRef ret = new BytesRef("ret");
Input[] keys = new Input[] { new Input("top of the lake", 15, lake), new Input("star wars: episode v - the empire strikes back", 12, star), new Input("the returned", 10, ret) };
Path tempDir = createTempDir("BlendedInfixSuggesterTest");
Analyzer a = new StandardAnalyzer(CharArraySet.EMPTY_SET);
// if factor is small, we don't get the expected element
BlendedInfixSuggester suggester = new BlendedInfixSuggester(newFSDirectory(tempDir), a, a, AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS, BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL, BlendedInfixSuggester.DEFAULT_NUM_FACTOR, false);
suggester.build(new InputArrayIterator(keys));
List<Lookup.LookupResult> responses = suggester.lookup("the", 4, true, false);
for (Lookup.LookupResult response : responses) {
System.out.println(response);
}
suggester.close();
}
use of org.apache.lucene.search.suggest.InputArrayIterator in project lucene-solr by apache.
the class BlendedInfixSuggesterTest method testSuggesterCountForAllLookups.
public void testSuggesterCountForAllLookups() throws IOException {
Input[] keys = new Input[] { new Input("lend me your ears", 1), new Input("as you sow so shall you reap", 1) };
Path tempDir = createTempDir("BlendedInfixSuggesterTest");
Analyzer a = new StandardAnalyzer(CharArraySet.EMPTY_SET);
// BlenderType.LINEAR is used by default (remove position*10%)
BlendedInfixSuggester suggester = new BlendedInfixSuggester(newFSDirectory(tempDir), a);
suggester.build(new InputArrayIterator(keys));
String term = "you";
List<Lookup.LookupResult> responses = suggester.lookup(term, false, 1);
assertEquals(1, responses.size());
responses = suggester.lookup(term, false, 2);
assertEquals(2, responses.size());
responses = suggester.lookup(term, 1, false, false);
assertEquals(1, responses.size());
responses = suggester.lookup(term, 2, false, false);
assertEquals(2, responses.size());
responses = suggester.lookup(term, (Map) null, 1, false, false);
assertEquals(1, responses.size());
responses = suggester.lookup(term, (Map) null, 2, false, false);
assertEquals(2, responses.size());
responses = suggester.lookup(term, (Set) null, 1, false, false);
assertEquals(1, responses.size());
responses = suggester.lookup(term, (Set) null, 2, false, false);
assertEquals(2, responses.size());
responses = suggester.lookup(term, null, false, 1);
assertEquals(1, responses.size());
responses = suggester.lookup(term, null, false, 2);
assertEquals(2, responses.size());
responses = suggester.lookup(term, (BooleanQuery) null, 1, false, false);
assertEquals(1, responses.size());
responses = suggester.lookup(term, (BooleanQuery) null, 2, false, false);
assertEquals(2, responses.size());
suggester.close();
}
use of org.apache.lucene.search.suggest.InputArrayIterator in project lucene-solr by apache.
the class FuzzySuggesterTest method testKeyword.
/** this is basically the WFST test ported to KeywordAnalyzer. so it acts the same */
public void testKeyword() throws Exception {
Input[] keys = new Input[] { new Input("foo", 50), new Input("bar", 10), new Input("barbar", 12), new Input("barbara", 6) };
Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.KEYWORD, false);
Directory tempDir = getDirectory();
FuzzySuggester suggester = new FuzzySuggester(tempDir, "fuzzy", analyzer);
suggester.build(new InputArrayIterator(keys));
List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence("bariar", random()), false, 2);
assertEquals(2, results.size());
assertEquals("barbar", results.get(0).key.toString());
assertEquals(12, results.get(0).value, 0.01F);
results = suggester.lookup(TestUtil.stringToCharSequence("barbr", random()), false, 2);
assertEquals(2, results.size());
assertEquals("barbar", results.get(0).key.toString());
assertEquals(12, results.get(0).value, 0.01F);
results = suggester.lookup(TestUtil.stringToCharSequence("barbara", random()), false, 2);
assertEquals(2, results.size());
assertEquals("barbara", results.get(0).key.toString());
assertEquals(6, results.get(0).value, 0.01F);
results = suggester.lookup(TestUtil.stringToCharSequence("barbar", random()), false, 2);
assertEquals(2, results.size());
assertEquals("barbar", results.get(0).key.toString());
assertEquals(12, results.get(0).value, 0.01F);
assertEquals("barbara", results.get(1).key.toString());
assertEquals(6, results.get(1).value, 0.01F);
results = suggester.lookup(TestUtil.stringToCharSequence("barbaa", random()), false, 2);
assertEquals(2, results.size());
assertEquals("barbar", results.get(0).key.toString());
assertEquals(12, results.get(0).value, 0.01F);
assertEquals("barbara", results.get(1).key.toString());
assertEquals(6, results.get(1).value, 0.01F);
// top N of 2, but only foo is available
results = suggester.lookup(TestUtil.stringToCharSequence("f", random()), false, 2);
assertEquals(1, results.size());
assertEquals("foo", results.get(0).key.toString());
assertEquals(50, results.get(0).value, 0.01F);
// top N of 1 for 'bar': we return this even though
// barbar is higher because exactFirst is enabled:
results = suggester.lookup(TestUtil.stringToCharSequence("bar", random()), false, 1);
assertEquals(1, results.size());
assertEquals("bar", results.get(0).key.toString());
assertEquals(10, results.get(0).value, 0.01F);
// top N Of 2 for 'b'
results = suggester.lookup(TestUtil.stringToCharSequence("b", random()), false, 2);
assertEquals(2, results.size());
assertEquals("barbar", results.get(0).key.toString());
assertEquals(12, results.get(0).value, 0.01F);
assertEquals("bar", results.get(1).key.toString());
assertEquals(10, results.get(1).value, 0.01F);
// top N of 3 for 'ba'
results = suggester.lookup(TestUtil.stringToCharSequence("ba", random()), false, 3);
assertEquals(3, results.size());
assertEquals("barbar", results.get(0).key.toString());
assertEquals(12, results.get(0).value, 0.01F);
assertEquals("bar", results.get(1).key.toString());
assertEquals(10, results.get(1).value, 0.01F);
assertEquals("barbara", results.get(2).key.toString());
assertEquals(6, results.get(2).value, 0.01F);
IOUtils.close(analyzer, tempDir);
}
use of org.apache.lucene.search.suggest.InputArrayIterator in project lucene-solr by apache.
the class AnalyzingSuggesterTest method testIllegalLookupArgument.
public void testIllegalLookupArgument() throws Exception {
Analyzer a = new MockAnalyzer(random());
Directory tempDir = getDirectory();
AnalyzingSuggester suggester = new AnalyzingSuggester(tempDir, "suggest", a, a, 0, 256, -1, true);
suggester.build(new InputArrayIterator(new Input[] { new Input("а где Люси?", 7) }));
expectThrows(IllegalArgumentException.class, () -> {
suggester.lookup("а", false, 3);
});
expectThrows(IllegalArgumentException.class, () -> {
suggester.lookup("а", false, 3);
});
IOUtils.close(a, tempDir);
}
Aggregations