Search in sources :

Example 1 with AnalyzingSuggester

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());
}
Also used : LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) AnalyzingSuggester(org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester)

Example 2 with AnalyzingSuggester

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;
}
Also used : WFSTCompletionLookup(org.apache.lucene.search.suggest.fst.WFSTCompletionLookup) SpellingResult(org.apache.solr.spelling.SpellingResult) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) Token(org.apache.lucene.analysis.Token) AnalyzingSuggester(org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester) CharsRef(org.apache.lucene.util.CharsRef)

Example 3 with AnalyzingSuggester

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);
}
Also used : Analyzer(org.apache.lucene.analysis.Analyzer) AnalyzingSuggester(org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester) FieldType(org.apache.solr.schema.FieldType)

Aggregations

AnalyzingSuggester (org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester)3 LookupResult (org.apache.lucene.search.suggest.Lookup.LookupResult)2 Analyzer (org.apache.lucene.analysis.Analyzer)1 Token (org.apache.lucene.analysis.Token)1 WFSTCompletionLookup (org.apache.lucene.search.suggest.fst.WFSTCompletionLookup)1 CharsRef (org.apache.lucene.util.CharsRef)1 FieldType (org.apache.solr.schema.FieldType)1 SpellingResult (org.apache.solr.spelling.SpellingResult)1