Java Code Examples for org.apache.solr.core.SolrResourceLoader#openResource()

The following examples show how to use org.apache.solr.core.SolrResourceLoader#openResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexSchemaFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an index schema created from a local resource.  The input is usually from the core descriptor.
 */
public IndexSchema create(String resourceName, SolrConfig config) {
  SolrResourceLoader loader = config.getResourceLoader();
  InputStream schemaInputStream = null;

  if (null == resourceName) {
    resourceName = IndexSchema.DEFAULT_SCHEMA_FILE;
  }

  try {
    schemaInputStream = loader.openResource(resourceName);
  } catch (Exception e) {
    final String msg = "Error loading schema resource " + resourceName;
    log.error(msg, e);
    throw new SolrException(ErrorCode.SERVER_ERROR, msg, e);
  }
  InputSource inputSource = new InputSource(schemaInputStream);
  inputSource.setSystemId(SystemIdResolver.createSystemIdFromResourceName(resourceName));
  IndexSchema schema = new IndexSchema(resourceName, inputSource, config.luceneMatchVersion, loader, config.getSubstituteProperties());
  return schema;
}
 
Example 2
Source File: TestAdapterModel.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void init(SolrResourceLoader solrResourceLoader) throws ModelException {
  super.init(solrResourceLoader);
  try (
      InputStream is = solrResourceLoader.openResource(answerFileName);
      InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
      BufferedReader br = new BufferedReader(isr)
      ) {
    answerValue = Float.parseFloat(br.readLine());
  } catch (IOException e) {
    throw new ModelException("Failed to get the answerValue from the given answerFileName (" + answerFileName + ")", e);
  }
}
 
Example 3
Source File: SchemaManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private ManagedIndexSchema getFreshManagedSchema(SolrCore core) throws IOException,
    KeeperException, InterruptedException {

  SolrResourceLoader resourceLoader = core.getResourceLoader();
  String name = core.getLatestSchema().getResourceName();
  if (resourceLoader instanceof ZkSolrResourceLoader) {
    final ZkSolrResourceLoader zkLoader = (ZkSolrResourceLoader)resourceLoader;
    SolrZkClient zkClient = zkLoader.getZkController().getZkClient();
    try {
      if (!zkClient.exists(zkLoader.getConfigSetZkPath() + "/" + name, true)) {
        String backupName = name + ManagedIndexSchemaFactory.UPGRADED_SCHEMA_EXTENSION;
        if (!zkClient.exists(zkLoader.getConfigSetZkPath() + "/" + backupName, true)) {
          log.warn("Unable to retrieve fresh managed schema, neither {} nor {} exist.", name, backupName);
          // use current schema
          return (ManagedIndexSchema) core.getLatestSchema();
        } else {
          name = backupName;
        }
      }
    } catch (Exception e) {
      log.warn("Unable to retrieve fresh managed schema {}", name, e);
      // use current schema
      return (ManagedIndexSchema) core.getLatestSchema();
    }
    InputStream in = resourceLoader.openResource(name);
    if (in instanceof ZkSolrResourceLoader.ZkByteArrayInputStream) {
      int version = ((ZkSolrResourceLoader.ZkByteArrayInputStream) in).getStat().getVersion();
      log.info("managed schema loaded . version : {} ", version);
      return new ManagedIndexSchema(core.getSolrConfig(), name, new InputSource(in), true, name, version,
          core.getLatestSchema().getSchemaUpdateLock());
    } else {
      return (ManagedIndexSchema) core.getLatestSchema();
    }
  } else {
    return (ManagedIndexSchema) core.getLatestSchema();
  }
}
 
Example 4
Source File: StatelessScriptUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public Reader openReader(SolrResourceLoader resourceLoader) throws IOException {
  InputStream input = resourceLoader.openResource(fileName);
  return org.apache.lucene.util.IOUtils.getDecodingReader
    (input, StandardCharsets.UTF_8);
}
 
Example 5
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
@Override
public void inform( SolrCore core ) {
  if (initParams != null) {
    SolrResourceLoader resourceLoader = core.getResourceLoader( );
      
    synonymsFile = (String)initParams.get( "synonyms" );
    if (synonymsFile != null) {
      Analyzer analyzer = new Analyzer() {
      @Override
        protected TokenStreamComponents createComponents(String fieldName) {
          Tokenizer tokenizer = new KeywordTokenizer();
          return new TokenStreamComponents(tokenizer, tokenizer );
        }
      };
              
      try {
        SolrSynonymParser parser = new SolrSynonymParser(true, true, analyzer);
        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
                                                                    .onUnmappableCharacter(CodingErrorAction.REPORT);
                  
        parser.parse(new InputStreamReader( resourceLoader.openResource(synonymsFile), decoder));
        this.synonyms = parser.build( );
      }
      catch ( Exception e ) {
        // ???
        Log.warn( "Parsing Synonyms Got Exception " + e );
      }
    }
      
    String stopwordsFile = (String)initParams.get( "stopwords" );
    if (stopwordsFile != null) {
      this.stopwords = new HashSet<String>( );
      try {
        BufferedReader br = new BufferedReader( new InputStreamReader( resourceLoader.openResource( stopwordsFile )));
        String line = null;
        while ((line = br.readLine( )) != null) {
          stopwords.add( line.toLowerCase( ) );
        }
        br.close( );
      }
      catch ( IOException ioe ) {
        Log.warn( "Adding Stopwords Got Exception " + ioe );
      }
    }
  }
    
  core.registerFirstSearcherListener( this );
  core.registerNewSearcherListener( this );
}
 
Example 6
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
@Override
public void inform( SolrCore core ) {
  if (initParams != null) {
    SolrResourceLoader resourceLoader = core.getResourceLoader( );
      
    synonymsFile = (String)initParams.get( "synonyms" );
    if (synonymsFile != null) {
      Analyzer analyzer = new Analyzer() {
      @Override
        protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
          Tokenizer tokenizer = new KeywordTokenizer( reader );
          return new TokenStreamComponents(tokenizer, tokenizer );
        }
      };
              
      try {
        SolrSynonymParser parser = new SolrSynonymParser(true, true, analyzer);
        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
                                                                    .onUnmappableCharacter(CodingErrorAction.REPORT);
                  
        parser.parse(new InputStreamReader( resourceLoader.openResource(synonymsFile), decoder));
        this.synonyms = parser.build( );
      }
      catch ( Exception e ) {
        // ???
        Log.warn( "Parsing Synonyms Got Exception " + e );
      }
    }
      
    String stopwordsFile = (String)initParams.get( "stopwords" );
    if (stopwordsFile != null) {
      this.stopwords = new HashSet<String>( );
      try {
        BufferedReader br = new BufferedReader( new InputStreamReader( resourceLoader.openResource( stopwordsFile )));
        String line = null;
        while ((line = br.readLine( )) != null) {
          stopwords.add( line.toLowerCase( ) );
        }
        br.close( );
      }
      catch ( IOException ioe ) {
        Log.warn( "Adding Stopwords Got Exception " + ioe );
      }
    }
  }
    
  core.registerFirstSearcherListener( this );
  core.registerNewSearcherListener( this );
}