use of org.apache.lucene.search.suggest.Lookup.LookupResult in project lucene-solr by apache.
the class AnalyzingInfixSuggesterTest method testEmptyAtStart.
public void testEmptyAtStart() throws Exception {
Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(newDirectory(), a, a, 3, false);
suggester.build(new InputArrayIterator(new Input[0]));
suggester.add(new BytesRef("a penny saved is a penny earned"), null, 10, new BytesRef("foobaz"));
suggester.add(new BytesRef("lend me your ear"), null, 8, new BytesRef("foobar"));
suggester.refresh();
List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), 10, true, true);
assertEquals(2, results.size());
assertEquals("a penny saved is a penny earned", results.get(0).key);
assertEquals("a penny saved is a penny <b>ear</b>ned", results.get(0).highlightKey);
assertEquals(10, results.get(0).value);
assertEquals(new BytesRef("foobaz"), results.get(0).payload);
assertEquals("lend me your ear", results.get(1).key);
assertEquals("lend me your <b>ear</b>", results.get(1).highlightKey);
assertEquals(8, results.get(1).value);
assertEquals(new BytesRef("foobar"), results.get(1).payload);
results = suggester.lookup(TestUtil.stringToCharSequence("ear ", random()), 10, true, true);
assertEquals(1, results.size());
assertEquals("lend me your ear", results.get(0).key);
assertEquals("lend me your <b>ear</b>", results.get(0).highlightKey);
assertEquals(8, results.get(0).value);
assertEquals(new BytesRef("foobar"), results.get(0).payload);
results = suggester.lookup(TestUtil.stringToCharSequence("pen", random()), 10, true, true);
assertEquals(1, results.size());
assertEquals("a penny saved is a penny earned", results.get(0).key);
assertEquals("a <b>pen</b>ny saved is a <b>pen</b>ny earned", results.get(0).highlightKey);
assertEquals(10, results.get(0).value);
assertEquals(new BytesRef("foobaz"), results.get(0).payload);
results = suggester.lookup(TestUtil.stringToCharSequence("p", random()), 10, true, true);
assertEquals(1, results.size());
assertEquals("a penny saved is a penny earned", results.get(0).key);
assertEquals("a <b>p</b>enny saved is a <b>p</b>enny earned", results.get(0).highlightKey);
assertEquals(10, results.get(0).value);
assertEquals(new BytesRef("foobaz"), results.get(0).payload);
suggester.close();
a.close();
}
use of org.apache.lucene.search.suggest.Lookup.LookupResult in project SearchServices by Alfresco.
the class AsyncBuildSuggestComponent method toSuggesterResult.
/**
* Convert NamedList (suggester response) to {@link SuggesterResult}
*/
private SuggesterResult toSuggesterResult(Map<String, SimpleOrderedMap<NamedList<Object>>> suggestionsMap) {
SuggesterResult result = new SuggesterResult();
if (suggestionsMap == null) {
return result;
}
// for each token
for (Map.Entry<String, SimpleOrderedMap<NamedList<Object>>> entry : suggestionsMap.entrySet()) {
String suggesterName = entry.getKey();
for (Iterator<Map.Entry<String, NamedList<Object>>> suggestionsIter = entry.getValue().iterator(); suggestionsIter.hasNext(); ) {
Map.Entry<String, NamedList<Object>> suggestions = suggestionsIter.next();
String tokenString = suggestions.getKey();
List<LookupResult> lookupResults = new ArrayList<>();
NamedList<Object> suggestion = suggestions.getValue();
// for each suggestion
for (int j = 0; j < suggestion.size(); j++) {
String property = suggestion.getName(j);
if (property.equals(SuggesterResultLabels.SUGGESTIONS)) {
@SuppressWarnings("unchecked") List<NamedList<Object>> suggestionEntries = (List<NamedList<Object>>) suggestion.getVal(j);
for (NamedList<Object> suggestionEntry : suggestionEntries) {
String term = (String) suggestionEntry.get(SuggesterResultLabels.SUGGESTION_TERM);
Long weight = (Long) suggestionEntry.get(SuggesterResultLabels.SUGGESTION_WEIGHT);
String payload = (String) suggestionEntry.get(SuggesterResultLabels.SUGGESTION_PAYLOAD);
LookupResult res = new LookupResult(new CharsRef(term), weight, new BytesRef(payload));
lookupResults.add(res);
}
}
result.add(suggesterName, tokenString, lookupResults);
}
}
}
return result;
}
use of org.apache.lucene.search.suggest.Lookup.LookupResult in project SearchServices by Alfresco.
the class AsyncBuildSuggestComponent method toNamedList.
/**
* Convert {@link SuggesterResult} to NamedList for constructing responses
*/
private void toNamedList(SuggesterResult suggesterResult, Map<String, SimpleOrderedMap<NamedList<Object>>> resultObj) {
for (String suggesterName : suggesterResult.getSuggesterNames()) {
SimpleOrderedMap<NamedList<Object>> results = new SimpleOrderedMap<>();
for (String token : suggesterResult.getTokens(suggesterName)) {
SimpleOrderedMap<Object> suggestionBody = new SimpleOrderedMap<>();
List<LookupResult> lookupResults = suggesterResult.getLookupResult(suggesterName, token);
suggestionBody.add(SuggesterResultLabels.SUGGESTION_NUM_FOUND, lookupResults.size());
List<SimpleOrderedMap<Object>> suggestEntriesNamedList = new ArrayList<>();
for (LookupResult lookupResult : lookupResults) {
String suggestionString = lookupResult.key.toString();
long weight = lookupResult.value;
String payload = (lookupResult.payload != null) ? lookupResult.payload.utf8ToString() : "";
SimpleOrderedMap<Object> suggestEntryNamedList = new SimpleOrderedMap<>();
suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_TERM, suggestionString);
suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_WEIGHT, weight);
suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_PAYLOAD, payload);
suggestEntriesNamedList.add(suggestEntryNamedList);
}
suggestionBody.add(SuggesterResultLabels.SUGGESTIONS, suggestEntriesNamedList);
results.add(token, suggestionBody);
}
resultObj.put(suggesterName, results);
}
}
use of org.apache.lucene.search.suggest.Lookup.LookupResult in project lucene-solr by apache.
the class FuzzySuggesterTest method testEmpty.
public void testEmpty() throws Exception {
Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.KEYWORD, false);
Directory tempDir = getDirectory();
FuzzySuggester suggester = new FuzzySuggester(tempDir, "fuzzy", analyzer);
suggester.build(new InputArrayIterator(new Input[0]));
List<LookupResult> result = suggester.lookup("a", false, 20);
assertTrue(result.isEmpty());
IOUtils.close(analyzer, tempDir);
}
use of org.apache.lucene.search.suggest.Lookup.LookupResult in project lucene-solr by apache.
the class FuzzySuggesterTest method testRandom.
public void testRandom() throws Exception {
int numQueries = atLeast(100);
final List<TermFreqPayload2> slowCompletor = new ArrayList<>();
final TreeSet<String> allPrefixes = new TreeSet<>();
final Set<String> seen = new HashSet<>();
Input[] keys = new Input[numQueries];
boolean preserveSep = random().nextBoolean();
boolean unicodeAware = random().nextBoolean();
final int numStopChars = random().nextInt(10);
final boolean preserveHoles = random().nextBoolean();
if (VERBOSE) {
System.out.println("TEST: " + numQueries + " words; preserveSep=" + preserveSep + " ; unicodeAware=" + unicodeAware + " numStopChars=" + numStopChars + " preserveHoles=" + preserveHoles);
}
for (int i = 0; i < numQueries; i++) {
int numTokens = TestUtil.nextInt(random(), 1, 4);
String key;
String analyzedKey;
while (true) {
key = "";
analyzedKey = "";
boolean lastRemoved = false;
for (int token = 0; token < numTokens; token++) {
String s;
while (true) {
// TODO: would be nice to fix this slowCompletor/comparator to
// use full range, but we might lose some coverage too...
s = TestUtil.randomSimpleString(random());
if (s.length() > 0) {
if (token > 0) {
key += " ";
}
if (preserveSep && analyzedKey.length() > 0 && (unicodeAware ? analyzedKey.codePointAt(analyzedKey.codePointCount(0, analyzedKey.length()) - 1) != ' ' : analyzedKey.charAt(analyzedKey.length() - 1) != ' ')) {
analyzedKey += " ";
}
key += s;
if (s.length() == 1 && isStopChar(s.charAt(0), numStopChars)) {
if (preserveSep && preserveHoles) {
analyzedKey += '