use of org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester in project elasticsearch-suggest-plugin by spinscale.
the class ShardSuggestService method getSuggestions.
private Collection<String> getSuggestions(ShardSuggestRequest shardSuggestRequest) throws IOException {
List<LookupResult> lookupResults = Lists.newArrayList();
if ("full".equals(shardSuggestRequest.suggestType())) {
AnalyzingSuggester analyzingSuggester = analyzingSuggesterCache.getUnchecked(new FieldType(shardSuggestRequest));
lookupResults.addAll(analyzingSuggester.lookup(shardSuggestRequest.term(), false, shardSuggestRequest.size()));
} else if ("fuzzy".equals(shardSuggestRequest.suggestType())) {
lookupResults.addAll(fuzzySuggesterCache.getUnchecked(new FieldType(shardSuggestRequest)).lookup(shardSuggestRequest.term(), false, shardSuggestRequest.size()));
} else {
lookupResults.addAll(lookupCache.getUnchecked(shardSuggestRequest.field()).lookup(shardSuggestRequest.term(), true, shardSuggestRequest.size() + 1));
Collection<String> suggestions = Collections2.transform(lookupResults, new LookupResultToStringFunction());
float similarity = shardSuggestRequest.similarity();
if (similarity < 1.0f && suggestions.size() < shardSuggestRequest.size()) {
suggestions = Lists.newArrayList(suggestions);
suggestions.addAll(getSimilarSuggestions(shardSuggestRequest));
}
return suggestions;
}
return Collections2.transform(lookupResults, new LookupResultToStringFunction());
}
use of org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester in project lucene-solr by apache.
the class Suggester method getSuggestions.
@Override
public SpellingResult getSuggestions(SpellingOptions options) throws IOException {
LOG.debug("getSuggestions: " + options.tokens);
if (lookup == null) {
LOG.info("Lookup is null - invoke spellchecker.build first");
return EMPTY_RESULT;
}
SpellingResult res = new SpellingResult();
CharsRef scratch = new CharsRef();
for (Token t : options.tokens) {
scratch.chars = t.buffer();
scratch.offset = 0;
scratch.length = t.length();
boolean onlyMorePopular = (options.suggestMode == SuggestMode.SUGGEST_MORE_POPULAR) && !(lookup instanceof WFSTCompletionLookup) && !(lookup instanceof AnalyzingSuggester);
List<LookupResult> suggestions = lookup.lookup(scratch, onlyMorePopular, options.count);
if (suggestions == null) {
continue;
}
if (options.suggestMode != SuggestMode.SUGGEST_MORE_POPULAR) {
Collections.sort(suggestions);
}
for (LookupResult lr : suggestions) {
res.add(t, lr.key.toString(), (int) lr.value);
}
}
return res;
}
use of org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester in project lucene-solr by apache.
the class AnalyzingLookupFactory method create.
@Override
public Lookup create(NamedList params, SolrCore core) {
// mandatory parameter
Object fieldTypeName = params.get(QUERY_ANALYZER);
if (fieldTypeName == null) {
throw new IllegalArgumentException("Error in configuration: " + QUERY_ANALYZER + " parameter is mandatory");
}
FieldType ft = core.getLatestSchema().getFieldTypeByName(fieldTypeName.toString());
if (ft == null) {
throw new IllegalArgumentException("Error in configuration: " + fieldTypeName.toString() + " is not defined in the schema");
}
Analyzer indexAnalyzer = ft.getIndexAnalyzer();
Analyzer queryAnalyzer = ft.getQueryAnalyzer();
// optional parameters
boolean exactMatchFirst = params.get(EXACT_MATCH_FIRST) != null ? Boolean.valueOf(params.get(EXACT_MATCH_FIRST).toString()) : true;
boolean preserveSep = params.get(PRESERVE_SEP) != null ? Boolean.valueOf(params.get(PRESERVE_SEP).toString()) : true;
int flags = 0;
if (exactMatchFirst) {
flags |= AnalyzingSuggester.EXACT_FIRST;
}
if (preserveSep) {
flags |= AnalyzingSuggester.PRESERVE_SEP;
}
int maxSurfaceFormsPerAnalyzedForm = params.get(MAX_SURFACE_FORMS) != null ? Integer.parseInt(params.get(MAX_SURFACE_FORMS).toString()) : 256;
int maxGraphExpansions = params.get(MAX_EXPANSIONS) != null ? Integer.parseInt(params.get(MAX_EXPANSIONS).toString()) : -1;
boolean preservePositionIncrements = params.get(PRESERVE_POSITION_INCREMENTS) != null ? Boolean.valueOf(params.get(PRESERVE_POSITION_INCREMENTS).toString()) : false;
return new AnalyzingSuggester(getTempDir(), "suggester", indexAnalyzer, queryAnalyzer, flags, maxSurfaceFormsPerAnalyzedForm, maxGraphExpansions, preservePositionIncrements);
}
Aggregations