Search in sources :

Example 1 with FileDictionary

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

the class FileDictionaryFactory method create.

@Override
public Dictionary create(SolrCore core, SolrIndexSearcher searcher) {
    if (params == null) {
        // should not happen; implies setParams was not called
        throw new IllegalStateException("Value of params not set");
    }
    String sourceLocation = (String) params.get(Suggester.LOCATION);
    if (sourceLocation == null) {
        throw new IllegalArgumentException(Suggester.LOCATION + " parameter is mandatory for using FileDictionary");
    }
    String fieldDelimiter = (params.get(FIELD_DELIMITER) != null) ? (String) params.get(FIELD_DELIMITER) : FileDictionary.DEFAULT_FIELD_DELIMITER;
    try {
        return new FileDictionary(new InputStreamReader(core.getResourceLoader().openResource(sourceLocation), StandardCharsets.UTF_8), fieldDelimiter);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileDictionary(org.apache.lucene.search.suggest.FileDictionary) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 2 with FileDictionary

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

the class Suggester method build.

@Override
public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {
    LOG.info("build()");
    if (sourceLocation == null) {
        reader = searcher.getIndexReader();
        dictionary = new HighFrequencyDictionary(reader, field, threshold);
    } else {
        try {
            dictionary = new FileDictionary(new InputStreamReader(core.getResourceLoader().openResource(sourceLocation), StandardCharsets.UTF_8));
        } catch (UnsupportedEncodingException e) {
            // should not happen
            LOG.error("should not happen", e);
        }
    }
    lookup.build(dictionary);
    if (storeDir != null) {
        File target = new File(storeDir, factory.storeFileName());
        if (!lookup.store(new FileOutputStream(target))) {
            if (sourceLocation == null) {
                assert reader != null && field != null;
                LOG.error("Store Lookup build from index on field: " + field + " failed reader has: " + reader.maxDoc() + " docs");
            } else {
                LOG.error("Store Lookup build from sourceloaction: " + sourceLocation + " failed");
            }
        } else {
            LOG.info("Stored suggest data to: " + target.getAbsolutePath());
        }
    }
}
Also used : HighFrequencyDictionary(org.apache.lucene.search.spell.HighFrequencyDictionary) FileDictionary(org.apache.lucene.search.suggest.FileDictionary) InputStreamReader(java.io.InputStreamReader) FileOutputStream(java.io.FileOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) File(java.io.File)

Aggregations

InputStreamReader (java.io.InputStreamReader)2 FileDictionary (org.apache.lucene.search.suggest.FileDictionary)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HighFrequencyDictionary (org.apache.lucene.search.spell.HighFrequencyDictionary)1