Search in sources :

Example 1 with AnalyzingInfixSuggester

use of org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester in project lucene-solr by apache.

the class AnalyzingInfixLookupFactory 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
    String indexPath = params.get(INDEX_PATH) != null ? params.get(INDEX_PATH).toString() : DEFAULT_INDEX_PATH;
    if (new File(indexPath).isAbsolute() == false) {
        indexPath = core.getDataDir() + File.separator + indexPath;
    }
    int minPrefixChars = params.get(MIN_PREFIX_CHARS) != null ? Integer.parseInt(params.get(MIN_PREFIX_CHARS).toString()) : AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS;
    boolean allTermsRequired = params.get(ALL_TERMS_REQUIRED) != null ? Boolean.getBoolean(params.get(ALL_TERMS_REQUIRED).toString()) : AnalyzingInfixSuggester.DEFAULT_ALL_TERMS_REQUIRED;
    boolean highlight = params.get(HIGHLIGHT) != null ? Boolean.getBoolean(params.get(HIGHLIGHT).toString()) : AnalyzingInfixSuggester.DEFAULT_HIGHLIGHT;
    try {
        return new AnalyzingInfixSuggester(FSDirectory.open(new File(indexPath).toPath()), indexAnalyzer, queryAnalyzer, minPrefixChars, true, allTermsRequired, highlight) {

            @Override
            public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws IOException {
                List<LookupResult> res = super.lookup(key, contexts, num, allTermsRequired, doHighlight);
                if (doHighlight) {
                    List<LookupResult> res2 = new ArrayList<>();
                    for (LookupResult hit : res) {
                        res2.add(new LookupResult(hit.highlightKey.toString(), hit.highlightKey, hit.value, hit.payload, hit.contexts));
                    }
                    res = res2;
                }
                return res;
            }
        };
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AnalyzingInfixSuggester(org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester) Set(java.util.Set) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Analyzer(org.apache.lucene.analysis.Analyzer) FieldType(org.apache.solr.schema.FieldType) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 AnalyzingInfixSuggester (org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester)1 FieldType (org.apache.solr.schema.FieldType)1