Example usage for org.apache.solr.rest ManagedResource getResourceLoader

List of usage examples for org.apache.solr.rest ManagedResource getResourceLoader

Introduction

In this page you can find the example usage for org.apache.solr.rest ManagedResource getResourceLoader.

Prototype

public SolrResourceLoader getResourceLoader() 

Source Link

Document

Returns the resource loader used by this resource.

Usage

From source file:cn.howardliu.lucene.extension.ManagedSynonymFilterFactory.java

License:Apache License

/**
 * Called once, during core initialization, to initialize any analysis components
 * that depend on the data managed by this resource. It is important that the
 * analysis component is only initialized once during core initialization so that
 * text analysis is consistent, especially in a distributed environment, as we
 * don't want one server applying a different set of stop words than other servers.
 *//*from w ww .  j a v a 2  s.co m*/
@SuppressWarnings("unchecked")
@Override
public void onManagedResourceInitialized(NamedList<?> initArgs, final ManagedResource res)
        throws SolrException {
    NamedList<Object> args = (NamedList<Object>) initArgs;
    args.add("synonyms", getResourceId());
    args.add("expand", "false");
    args.add("format", "solr");

    Map<String, String> filtArgs = new HashMap<>();
    for (Map.Entry<String, ?> entry : args) {
        filtArgs.put(entry.getKey(), entry.getValue().toString());
    }
    // create the actual filter factory that pulls the synonym mappings
    // from synonymMappings using a custom parser implementation
    delegate = new SynonymFilterFactory(filtArgs) {
        @Override
        protected SynonymMap loadSynonyms(ResourceLoader loader, String cname, boolean dedup, Analyzer analyzer)
                throws IOException, ParseException {
            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder()
                    .onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
            ManagedSynonymParser parser = new ManagedSynonymParser((SynonymManager) res, dedup, analyzer);
            // ???
            InputStreamReader in = null;
            if (StringUtils.isNotBlank(synonymFile)) {
                in = new InputStreamReader(loader.openResource(synonymFile), decoder);
            }
            parser.parse(in);
            return parser.build();
        }
    };
    try {
        delegate.inform(res.getResourceLoader());
    } catch (IOException e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, e);
    }
}