Java Code Examples for org.apache.solr.core.SolrCore

The following examples show how to use org.apache.solr.core.SolrCore. These examples are extracted from open source projects. 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 Project: customized-symspell   Source File: SpellcheckComponent.java    License: MIT License 6 votes vote down vote up
@Override
public void inform(SolrCore core) {
  if (initParams == null) {
    return;
  }

  log.info("Initializing spell checkers");

  if (initParams.getName(0).equals("spellcheckers")) {
    Object cfg = initParams.getVal(0);
    if (cfg instanceof NamedList) {
      addSpellChecker(core, (NamedList) cfg);
    } else if (cfg instanceof Map) {
      addSpellChecker(core, new NamedList((Map) cfg));
    } else if (cfg instanceof List) {
      for (Object o : (List) cfg) {
        if (o instanceof Map) {
          addSpellChecker(core, new NamedList((Map) o));
        }
      }
    }
  }

  log.info("Spell checker  Initialization completed");
}
 
Example 2
Source Project: incubator-sentry   Source File: SecureCoreAdminHandler.java    License: Apache License 2.0 6 votes vote down vote up
private String getCollectionFromCoreName(String coreName) {
  SolrCore solrCore = null;
  try {
    if (coreName != null && !coreName.equals("")) {
      solrCore = coreContainer.getCore(coreName);
      if (solrCore != null) {
        return solrCore.getCoreDescriptor().getCloudDescriptor().getCollectionName();
      }
    }
  } finally {
    if (solrCore != null) {
      solrCore.close();
    }
  }
  return null;
}
 
Example 3
Source Project: lucene-solr   Source File: RecoveryStrategy.java    License: Apache License 2.0 6 votes vote down vote up
final private void cloudDebugLog(SolrCore core, String op) {
  if (!log.isDebugEnabled()) {
    return;
  }
  try {
    RefCounted<SolrIndexSearcher> searchHolder = core.getNewestSearcher(false);
    SolrIndexSearcher searcher = searchHolder.get();
    try {
      final int totalHits = searcher.count(new MatchAllDocsQuery());
      final String nodeName = core.getCoreContainer().getZkController().getNodeName();
      log.debug("[{}] {} [{} total hits]", nodeName, op, totalHits);
    } finally {
      searchHolder.decref();
    }
  } catch (Exception e) {
    log.debug("Error in solrcloud_debug block", e);
  }
}
 
Example 4
Source Project: customized-symspell   Source File: SpellcheckComponent.java    License: MIT License 6 votes vote down vote up
private void loadDefault(String unigramsFile, String bigramsFile, String exclusionsFile,
    SpellChecker spellChecker,
    SolrCore core, String exclusionListSperatorRegex) {
  try {
    if (!StringUtils.isEmpty(unigramsFile)) {
      loadUniGramFile(core.getResourceLoader().openResource(unigramsFile),
          spellChecker.getDataHolder());
    }

    if (!StringUtils.isEmpty(bigramsFile)) {
      loadBiGramFile(core.getResourceLoader().openResource(bigramsFile),
          spellChecker.getDataHolder());
    }

    if (!StringUtils.isEmpty(exclusionsFile)) {
      loadExclusions(core.getResourceLoader().openResource(exclusionsFile),
          spellChecker.getDataHolder(), exclusionListSperatorRegex);
    }

  } catch (SpellCheckException | IOException ex) {
    log.error("Error occured while loading default Configs for Spellcheck");
  }
}
 
Example 5
Source Project: BioSolr   Source File: TestMerge.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * When [shard] is not in the field list, the [shard] field should not be returned, even
 * if score is in the field list.
 */
@Test
public void testNotWantsShardWithScore() throws Exception {
  try (SolrCore core = h.getCoreContainer().getCore("merge")) {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", "*:*");
    params.add("sort", "letter asc");
    params.add("fl", "*,score");

    SolrDocumentList docs = queryDocs(core, "merge", params);
    assertEquals(3, docs.size());
    
    for (SolrDocument doc : docs) {
      assertNull(doc.getFieldValue("[shard]"));
      assertEquals(1.0f, doc.getFieldValue("score"));
    }
  }   
}
 
Example 6
Source Project: BioSolr   Source File: SimpleTreeFacetComponentTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBadRequest_missingLocalParams() {
	SolrCore core = h.getCore();
	
	ModifiableSolrParams params = new ModifiableSolrParams();
	params.add("q", "*:*");
	params.add("facet", "true");
	params.add("facet.tree", "true");
	params.add("facet.tree.field", "node_id");
	
    SolrQueryResponse rsp = new SolrQueryResponse();
    rsp.add("responseHeader", new SimpleOrderedMap<>());
    SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

    SolrRequestHandler handler = core.getRequestHandler(requestHandler);
    handler.handleRequest(req, rsp);
    req.close();
      
    assertNotNull(rsp.getException());
}
 
Example 7
Source Project: vind   Source File: FieldAnalyzerService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * analyzes string like the given field
 * @param field the name of the field
 * @param value the string to analyze
 * @return the analyzed string
 */
public static String analyzeString(SolrCore core, String field, String value) {
    try {
        StringBuilder b = new StringBuilder();
        try (TokenStream ts = core.getLatestSchema().getFieldType(field).getQueryAnalyzer().tokenStream(field, new StringReader(value))) {
            ts.reset();
            while (ts.incrementToken()) {
                b.append(" ");
                CharTermAttribute attr = ts.getAttribute(CharTermAttribute.class);
                b.append(attr);
            }
        }

        return b.toString().trim();
    } catch (IOException e) {
        //FIXME: This error should be properly logged!
        e.printStackTrace();
        return value;
    }
}
 
Example 8
Source Project: lucene-solr   Source File: SyncStrategy.java    License: Apache License 2.0 6 votes vote down vote up
public PeerSync.PeerSyncResult sync(ZkController zkController, SolrCore core, ZkNodeProps leaderProps,
    boolean peerSyncOnlyWithActive) {
  if (SKIP_AUTO_RECOVERY) {
    return PeerSync.PeerSyncResult.success();
  }

  if (isClosed) {
    log.warn("Closed, skipping sync up.");
    return PeerSync.PeerSyncResult.failure();
  }

  recoveryRequests.clear();

  if (log.isInfoEnabled()) {
    log.info("Sync replicas to {}", ZkCoreNodeProps.getCoreUrl(leaderProps));
  }

  if (core.getUpdateHandler().getUpdateLog() == null) {
    log.error("No UpdateLog found - cannot sync");
    return PeerSync.PeerSyncResult.failure();
  }

  return syncReplicas(zkController, core, leaderProps, peerSyncOnlyWithActive);
}
 
Example 9
Source Project: lucene-solr   Source File: SoftAutoCommitTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void createMonitor() throws Exception {
  assumeFalse("This test is not working on Windows (or maybe machines with only 2 CPUs)",
    Constants.WINDOWS);

  SolrCore core = h.getCore();

  updater = (DirectUpdateHandler2) core.getUpdateHandler();
  updater.setCommitWithinSoftCommit(true); // foce to default, let tests change as needed
  monitor = new MockEventListener();

  core.registerNewSearcherListener(monitor);
  updater.registerSoftCommitCallback(monitor);
  updater.registerCommitCallback(monitor);

  // isolate searcher getting ready from this test
  monitor.searcher.poll(5000, MILLISECONDS);
}
 
Example 10
Source Project: lucene-solr   Source File: RandomTestDictionaryFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RandomTestDictionary 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 name = (String)params.get(CommonParams.NAME);
  if (name == null) { // Shouldn't happen since this is the component name
    throw new IllegalArgumentException(CommonParams.NAME + " is a mandatory parameter");
  }
  long maxItems = DEFAULT_MAX_ITEMS;
  Object specifiedMaxItems = params.get(RAND_DICT_MAX_ITEMS);
  if (specifiedMaxItems != null) {
    maxItems = Long.parseLong(specifiedMaxItems.toString());
  }
  return new RandomTestDictionary(name, maxItems);
}
 
Example 11
Source Project: lucene-solr   Source File: StreamHandler.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
public void inform(SolrCore core) {
  String defaultCollection;
  String defaultZkhost;
  CoreContainer coreContainer = core.getCoreContainer();
  this.solrClientCache = coreContainer.getSolrClientCache();
  this.coreName = core.getName();
  String cacheKey = this.getClass().getName() + "_" + coreName + "_";
  this.objectCache = coreContainer.getObjectCache().computeIfAbsent(cacheKey + "objectCache",
      ConcurrentHashMap.class, k-> new ConcurrentHashMap());
  if (coreContainer.isZooKeeperAware()) {
    defaultCollection = core.getCoreDescriptor().getCollectionName();
    defaultZkhost = core.getCoreContainer().getZkController().getZkServerAddress();
    streamFactory.withCollectionZkHost(defaultCollection, defaultZkhost);
    streamFactory.withDefaultZkHost(defaultZkhost);
    modelCache = coreContainer.getObjectCache().computeIfAbsent(cacheKey + "modelCache",
        ModelCache.class,
        k -> new ModelCache(250, defaultZkhost, solrClientCache));
  }
  streamFactory.withSolrResourceLoader(core.getResourceLoader());

  // This pulls all the overrides and additions from the config
  addExpressiblePlugins(streamFactory, core);
}
 
Example 12
Source Project: lucene-solr   Source File: SuggestComponentTest.java    License: Apache License 2.0 6 votes vote down vote up
private void reloadCore(boolean createNewCore) throws Exception {
  if (createNewCore) {
    CoreContainer cores = h.getCoreContainer();
    SolrCore core = h.getCore();
    String dataDir1 = core.getDataDir();
    CoreDescriptor cd = core.getCoreDescriptor();
    h.close();
    createCore();
    SolrCore createdCore = h.getCore();
    assertEquals(dataDir1, createdCore.getDataDir());
    assertEquals(createdCore, h.getCore());
  } else {
    h.reload();
    // On regular reloading, wait until the new searcher is registered
    waitForWarming();
  }
  
  assertQ(req("qt", "/select",
      "q", "*:*"), 
      "//*[@numFound='11']"
      );
}
 
Example 13
@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 field = (String)params.get(SolrSpellChecker.FIELD);
  
  if (field == null) {
    throw new IllegalArgumentException(SolrSpellChecker.FIELD + " is a mandatory parameter");
  }
  
  float threshold = params.get(THRESHOLD_TOKEN_FREQUENCY) == null ? 0.0f
      : (Float)params.get(THRESHOLD_TOKEN_FREQUENCY);
  
  return new HighFrequencyDictionary(searcher.getIndexReader(), field, threshold);
}
 
Example 14
Source Project: lucene-solr   Source File: TestTlogReplica.java    License: Apache License 2.0 6 votes vote down vote up
private void waitForReplicasCatchUp(int numTry) throws IOException, InterruptedException {
  String leaderTimeCommit = getSolrCore(true).get(0).getDeletionPolicy().getLatestCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
  if (leaderTimeCommit == null) return;
  for (int i = 0; i < numTry; i++) {
    boolean inSync = true;
    for (SolrCore solrCore : getSolrCore(false)) {
      String replicateTimeCommit = solrCore.getDeletionPolicy().getLatestCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
      if (!leaderTimeCommit.equals(replicateTimeCommit)) {
        inSync = false;
        Thread.sleep(500);
        break;
      }
    }
    if (inSync) return;
  }

  fail("Some replicas are not in sync with leader");

}
 
Example 15
Source Project: lucene-solr   Source File: HttpShardHandlerFactory.java    License: Apache License 2.0 6 votes vote down vote up
protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req) {
  final SolrParams params = req.getParams();
  final SolrCore core = req.getCore(); // explicit check for null core (temporary?, for tests)
  @SuppressWarnings("resource")
  ZkController zkController = core == null ? null : core.getCoreContainer().getZkController();
  if (zkController != null) {
    return requestReplicaListTransformerGenerator.getReplicaListTransformer(
        params,
        zkController.getZkStateReader().getClusterProperties()
            .getOrDefault(ZkStateReader.DEFAULT_SHARD_PREFERENCES, "")
            .toString(),
        zkController.getNodeName(),
        zkController.getBaseUrl(),
        zkController.getSysPropsCacher()
    );
  } else {
    return requestReplicaListTransformerGenerator.getReplicaListTransformer(params);
  }
}
 
Example 16
Source Project: lucene-solr   Source File: UpdateProcessorTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected void processDeleteById(final String chain, String id) throws IOException {
  SolrCore core = h.getCore();
  UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);
  assertNotNull("No Chain named: " + chain, pc);

  SolrQueryResponse rsp = new SolrQueryResponse();

  SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());

  DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
  cmd.setId(id);
  UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
  try {
    processor.processDelete(cmd);
  } finally {
    req.close();
  }
}
 
Example 17
Source Project: lucene-solr   Source File: SolrFeature.java    License: Apache License 2.0 6 votes vote down vote up
private LocalSolrQueryRequest makeRequest(SolrCore core, String solrQuery,
    List<String> fqs, String df) {
  final NamedList<String> returnList = new NamedList<String>();
  if ((solrQuery != null) && !solrQuery.isEmpty()) {
    returnList.add(CommonParams.Q, solrQuery);
  }
  if (fqs != null) {
    for (final String fq : fqs) {
      returnList.add(CommonParams.FQ, fq);
    }
  }
  if ((df != null) && !df.isEmpty()) {
    returnList.add(CommonParams.DF, df);
  }
  if (returnList.size() > 0) {
    return new LocalSolrQueryRequest(core, returnList);
  } else {
    return null;
  }
}
 
Example 18
@Test
public void testFailNonIndexedSigWithOverwriteDupes() throws Exception {
  SolrCore core = h.getCore();
  SignatureUpdateProcessorFactory f = new SignatureUpdateProcessorFactory();
  NamedList<String> initArgs = new NamedList<>();
  initArgs.add("overwriteDupes", "true");
  initArgs.add("signatureField", "signatureField_sS");
  f.init(initArgs);
  boolean exception_ok = false;
  try {
    f.inform(core);
  } catch (Exception e) {
    exception_ok = true;
  }
  assertTrue("Should have gotten an exception from inform(SolrCore)", 
             exception_ok);
}
 
Example 19
Source Project: lucene-solr   Source File: AnalysisAfterCoreReloadTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void tearDown() throws Exception {
  String configDir;
  try (SolrCore core = h.getCoreContainer().getCore(collection)) {
    configDir = core.getResourceLoader().getConfigDir();
  }
  super.tearDown();
  if (new File(configDir, "stopwords.txt.bak").exists()) {
    FileUtils.deleteQuietly(new File(configDir, "stopwords.txt"));
    FileUtils.moveFile(new File(configDir, "stopwords.txt.bak"), new File(configDir, "stopwords.txt"));
  }
}
 
Example 20
Source Project: lucene-solr   Source File: QueryElevationComponent.java    License: Apache License 2.0 5 votes vote down vote up
private void parseFieldType(SolrCore core) throws InitializationException {
  String a = initArgs.get(FIELD_TYPE);
  if (a != null) {
    FieldType ft = core.getLatestSchema().getFieldTypes().get(a);
    if (ft == null) {
      throw new InitializationException("Parameter " + FIELD_TYPE + " defines an unknown field type \"" + a + "\"", InitializationExceptionCause.UNKNOWN_FIELD_TYPE);
    }
    queryAnalyzer = ft.getQueryAnalyzer();
  }
}
 
Example 21
Source Project: lucene-solr   Source File: TestZKPropertiesWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Code copied from {@link org.apache.solr.util.TestHarness#query(String, SolrQueryRequest)} because it is not
 * <code>static</code> there (it could have been) and we do not have an instance of {@link org.apache.solr.util.TestHarness}.
 */
private static String localQuery(String handler, SolrQueryRequest req) throws Exception {
  try {
    SolrCore core = req.getCore();
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    core.execute(core.getRequestHandler(handler),req,rsp); // TODO the core doesn't have the request handler
    if (rsp.getException() != null) {
      throw rsp.getException();
    }
    QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
    if (responseWriter instanceof BinaryQueryResponseWriter) {
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(32000);
      BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) responseWriter;
      writer.write(byteArrayOutputStream, req, rsp);
      return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
    } else {
      StringWriter sw = new StringWriter(32000);
      responseWriter.write(sw,req,rsp);
      return sw.toString();
    }

  } finally {
    req.close();
    SolrRequestInfo.clearRequestInfo();
  }
}
 
Example 22
@Override
public void inform(SolrCore core) {
  solrResourceLoader = core.getResourceLoader();

  for (TypeMapping typeMapping : typeMappings) {
    typeMapping.populateValueClasses(core);
  }
}
 
Example 23
Source Project: lucene-solr   Source File: IndexFetcher.java    License: Apache License 2.0 5 votes vote down vote up
private void openNewSearcherAndUpdateCommitPoint() throws IOException {
  RefCounted<SolrIndexSearcher> searcher = null;
  IndexCommit commitPoint;
  // must get the latest solrCore object because the one we have might be closed because of a reload
  // todo stop keeping solrCore around
  SolrCore core = solrCore.getCoreContainer().getCore(solrCore.getName());
  try {
    @SuppressWarnings({"rawtypes"})
    Future[] waitSearcher = new Future[1];
    searcher = core.getSearcher(true, true, waitSearcher, true);
    if (waitSearcher[0] != null) {
      try {
        waitSearcher[0].get();
      } catch (InterruptedException | ExecutionException e) {
        SolrException.log(log, e);
      }
    }
    commitPoint = searcher.get().getIndexReader().getIndexCommit();
  } finally {
    if (searcher != null) {
      searcher.decref();
    }
    core.close();
  }

  // update the commit point in replication handler
  replicationHandler.indexCommitPoint = commitPoint;

}
 
Example 24
@Override
public void inform(final SolrCore core) {
  
  selector = 
    FieldMutatingUpdateProcessor.createFieldNameSelector
        (core.getResourceLoader(), core, inclusions, getDefaultSelector(core));

  for (SelectorParams exc : exclusions) {
    selector = FieldMutatingUpdateProcessor.wrap
      (selector,
       FieldMutatingUpdateProcessor.createFieldNameSelector
           (core.getResourceLoader(), core, exc, FieldMutatingUpdateProcessor.SELECT_NO_FIELDS));
  }
}
 
Example 25
Source Project: lucene-solr   Source File: SolrIndexSplitter.java    License: Apache License 2.0 5 votes vote down vote up
private void openNewSearcher(SolrCore core) throws Exception {
  @SuppressWarnings({"rawtypes"})
  Future[] waitSearcher = new Future[1];
  core.getSearcher(true, false, waitSearcher, true);
  if (waitSearcher[0] != null) {
    waitSearcher[0].get();
  }
}
 
Example 26
private void reloadSuggester(SolrSuggester suggester, SolrCore core,
		SolrIndexSearcher searcher) throws IOException {
	File storeFile = suggester.getStoreFile();
	if (storeFile == null || !storeFile.exists()) {
		suggester.build(core, searcher);
	} else {
		suggester.reload(core, searcher);
	}
}
 
Example 27
Source Project: jate   Source File: AppATTF.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public List<JATETerm> extract(SolrCore core, String jatePropertyFile)
        throws IOException, JATEException {
    JATEProperties properties = getJateProperties(jatePropertyFile);

    return extract(core, properties);
}
 
Example 28
Source Project: lucene-solr   Source File: WFSTLookupFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Lookup create(@SuppressWarnings({"rawtypes"})NamedList params, SolrCore core) {
  boolean exactMatchFirst = params.get(EXACT_MATCH_FIRST) != null
  ? Boolean.valueOf(params.get(EXACT_MATCH_FIRST).toString())
  : true;

  return new WFSTCompletionLookup(getTempDir(), "suggester", exactMatchFirst);
}
 
Example 29
Source Project: lucene-solr   Source File: SuggestComponent.java    License: Apache License 2.0 5 votes vote down vote up
public SuggesterListener(SolrCore core, SolrSuggester checker, boolean buildOnCommit, boolean buildOnOptimize, boolean buildOnStartup, boolean isCoreReload) {
  this.core = core;
  this.suggester = checker;
  this.buildOnCommit = buildOnCommit;
  this.buildOnOptimize = buildOnOptimize;
  this.buildOnStartup = buildOnStartup;
  this.isCoreReload = isCoreReload;
}
 
Example 30
@Override
public List<JATETerm> rankingAndFiltering(SolrCore core, String jatePropertyFile, Map<String, String> params,
                                          Algorithm algorithm) throws IOException, JATEException {
    for (TermRecognitionProcessor termRecognitionProcessor : processors) {
        List<JATETerm> terms = termRecognitionProcessor.rankingAndFiltering(core, jatePropertyFile, params, algorithm);
        if (terms != null) {
            return terms;
        }
    }
    return null;
}