Java Code Examples for org.apache.solr.core.SolrCore#getSearcher()

The following examples show how to use org.apache.solr.core.SolrCore#getSearcher() . 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: FacetTreeGenerator.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
/**
 * Get a reference to the searcher for the required collection. If the collection is
 * not the same as the search collection, we assume it is under the same Solr instance.
 * @param rb the response builder holding the facets.
 * @return a counted reference to the searcher.
 * @throws SolrException if the collection cannot be found.
 */
private RefCounted<SolrIndexSearcher> getSearcherReference(ResponseBuilder rb) throws SolrException {
	RefCounted<SolrIndexSearcher> searcherRef;
	
	SolrCore currentCore = rb.req.getCore();
	if (StringUtils.isBlank(collection)) {
		searcherRef = currentCore.getSearcher();
	} else {
		// Using an alternative core - find it
		SolrCore reqCore = currentCore.getCoreDescriptor().getCoreContainer().getCore(collection);
		if (reqCore == null) {
			throw new SolrException(ErrorCode.BAD_REQUEST, "Collection \"" + collection
					+ "\" cannot be found");
		}
		searcherRef = reqCore.getSearcher();
	}
	
	return searcherRef;
}
 
Example 2
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean isInIndex(long id, LRU cache, String fieldName, boolean populateCache, SolrCore core) throws IOException
{
    if(cache.containsKey(id))
    {
        return true;
    }
    else
    {
        RefCounted<SolrIndexSearcher> refCounted = null;
        try
        {
            if(populateCache)
            {
                cache.put(id, null); // Safe to add this here because we reset this on rollback.
            }
            refCounted = core.getSearcher();
            SolrIndexSearcher searcher = refCounted.get();
            FieldType fieldType = searcher.getSchema().getField(fieldName).getType();
            TermQuery q = new TermQuery(new Term(fieldName, fieldType.readableToIndexed(Long.toString(id))));
            TopDocs topDocs = searcher.search(q, 1);
            return topDocs.totalHits > 0;
        }
        finally
        {
            ofNullable(refCounted).ifPresent(RefCounted::decref);
        }
    }
}
 
Example 3
Source File: SolrIndexSplitter.java    From lucene-solr with 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 4
Source File: IndexFetcher.java    From lucene-solr with 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 5
Source File: TestIndexSearcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void createCoreAndValidateListeners(int numTimesCalled, int numTimesCalledFirstSearcher,
    int numTimesCalledAfterGetSearcher, int numTimesCalledFirstSearcherAfterGetSearcher) throws Exception {
  CoreContainer cores = h.getCoreContainer();
  CoreDescriptor cd = h.getCore().getCoreDescriptor();
  SolrCore newCore = null;
  // reset counters
  MockSearcherListener.numberOfTimesCalled = new AtomicInteger();
  MockSearcherListener.numberOfTimesCalledFirstSearcher = new AtomicInteger();
  
  try {
    // Create a new core, this should call all the firstSearcherListeners
    newCore = cores.create("core1", cd.getInstanceDir(), ImmutableMap.of("config", "solrconfig-searcher-listeners1.xml"), false);
    
    //validate that the new core was created with the correct solrconfig
    assertNotNull(newCore.getSearchComponent("mock"));
    assertEquals(MockSearchComponent.class, newCore.getSearchComponent("mock").getClass());
    assertFalse(newCore.getSolrConfig().useColdSearcher);
    
    doQuery(newCore);
    
    assertEquals(numTimesCalled, MockSearcherListener.numberOfTimesCalled.get());
    assertEquals(numTimesCalledFirstSearcher, MockSearcherListener.numberOfTimesCalledFirstSearcher.get());
    
    addDummyDoc(newCore);
    
    // Open a new searcher, this should call the newSearcherListeners
    @SuppressWarnings({"rawtypes"})
    Future<?>[] future = new Future[1];
    newCore.getSearcher(true, false, future);
    future[0].get();
    
    assertEquals(numTimesCalledAfterGetSearcher, MockSearcherListener.numberOfTimesCalled.get());
    assertEquals(numTimesCalledFirstSearcherAfterGetSearcher, MockSearcherListener.numberOfTimesCalledFirstSearcher.get());
    
  } finally {
    if (newCore != null) {
      cores.unload("core1");
    }
  }
}
 
Example 6
Source File: JoinQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public JoinQueryWeight(SolrIndexSearcher searcher, ScoreMode scoreMode, float boost) {
  super(JoinQuery.this, boost);
  this.scoreMode = scoreMode;
  this.fromSearcher = searcher;
  SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
  if (info != null) {
    rb = info.getResponseBuilder();
  }

  if (fromIndex == null) {
    this.fromSearcher = searcher;
  } else {
    if (info == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cross-core join must have SolrRequestInfo");
    }

    CoreContainer container = searcher.getCore().getCoreContainer();
    final SolrCore fromCore = container.getCore(fromIndex);

    if (fromCore == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cross-core join: no such core " + fromIndex);
    }

    if (info.getReq().getCore() == fromCore) {
      // if this is the same core, use the searcher passed in... otherwise we could be warming and
      // get an older searcher from the core.
      fromSearcher = searcher;
    } else {
      // This could block if there is a static warming query with a join in it, and if useColdSearcher is true.
      // Deadlock could result if two cores both had useColdSearcher and had joins that used eachother.
      // This would be very predictable though (should happen every time if misconfigured)
      fromRef = fromCore.getSearcher(false, true, null);

      // be careful not to do anything with this searcher that requires the thread local
      // SolrRequestInfo in a manner that requires the core in the request to match
      fromSearcher = fromRef.get();
    }

    if (fromRef != null) {
      final RefCounted<SolrIndexSearcher> ref = fromRef;
      info.addCloseHook(new Closeable() {
        @Override
        public void close() {
          ref.decref();
        }
      });
    }

    info.addCloseHook(new Closeable() {
      @Override
      public void close() {
        fromCore.close();
      }
    });

  }
  this.toSearcher = searcher;
}
 
Example 7
Source File: ZkController.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures that a searcher is registered for the given core and if not, waits until one is registered
 */
private static void ensureRegisteredSearcher(SolrCore core) throws InterruptedException {
  if (!core.getSolrConfig().useColdSearcher) {
    RefCounted<SolrIndexSearcher> registeredSearcher = core.getRegisteredSearcher();
    if (registeredSearcher != null) {
      if (log.isDebugEnabled()) {
        log.debug("Found a registered searcher: {} for core: {}", registeredSearcher.get(), core);
      }
      registeredSearcher.decref();
    } else  {
      @SuppressWarnings({"rawtypes"})
      Future[] waitSearcher = new Future[1];
      if (log.isInfoEnabled()) {
        log.info("No registered searcher found for core: {}, waiting until a searcher is registered before publishing as active", core.getName());
      }
      final RTimer timer = new RTimer();
      RefCounted<SolrIndexSearcher> searcher = null;
      try {
        searcher = core.getSearcher(false, true, waitSearcher, true);
        boolean success = true;
        if (waitSearcher[0] != null)  {
          if (log.isDebugEnabled()) {
            log.debug("Waiting for first searcher of core {}, id: {} to be registered", core.getName(), core);
          }
          try {
            waitSearcher[0].get();
          } catch (ExecutionException e) {
            log.warn("Wait for a searcher to be registered for core {}, id: {} failed due to: {}", core.getName(), core, e, e);
            success = false;
          }
        }
        if (success)  {
          if (searcher == null) {
            // should never happen
            if (log.isDebugEnabled()) {
              log.debug("Did not find a searcher even after the future callback for core: {}, id: {}!!!", core.getName(), core);
            }
          } else  {
            if (log.isInfoEnabled()) {
              log.info("Found a registered searcher: {}, took: {} ms for core: {}, id: {}", searcher.get(), timer.getTime(), core.getName(), core);
            }
          }
        }
      } finally {
        if (searcher != null) {
          searcher.decref();
        }
      }
    }
    RefCounted<SolrIndexSearcher> newestSearcher = core.getNewestSearcher(false);
    if (newestSearcher != null) {
      if (log.isDebugEnabled()) {
        log.debug("Found newest searcher: {} for core: {}, id: {}", newestSearcher.get(), core.getName(), core);
      }
      newestSearcher.decref();
    }
  }
}
 
Example 8
Source File: IndexSizeEstimatorTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testEstimator() throws Exception {
  JettySolrRunner jetty = cluster.getRandomJetty(random());
  String randomCoreName = jetty.getCoreContainer().getAllCoreNames().iterator().next();
  SolrCore core = jetty.getCoreContainer().getCore(randomCoreName);
  RefCounted<SolrIndexSearcher> searcherRef = core.getSearcher();
  try {
    SolrIndexSearcher searcher = searcherRef.get();
    // limit the max length
    IndexSizeEstimator estimator = new IndexSizeEstimator(searcher.getRawReader(), 20, 50, true, true);
    IndexSizeEstimator.Estimate estimate = estimator.estimate();
    Map<String, Long> fieldsBySize = estimate.getFieldsBySize();
    assertFalse("empty fieldsBySize", fieldsBySize.isEmpty());
    assertEquals(fieldsBySize.toString(), fields.size(), fieldsBySize.size());
    fieldsBySize.forEach((k, v) -> assertTrue("unexpected size of " + k + ": " + v, v > 0));
    Map<String, Long> typesBySize = estimate.getTypesBySize();
    assertFalse("empty typesBySize", typesBySize.isEmpty());
    assertTrue("expected at least 8 types: " + typesBySize.toString(), typesBySize.size() >= 8);
    typesBySize.forEach((k, v) -> assertTrue("unexpected size of " + k + ": " + v, v > 0));
    Map<String, Object> summary = estimate.getSummary();
    assertNotNull("summary", summary);
    assertFalse("empty summary", summary.isEmpty());
    assertEquals(summary.keySet().toString(), fields.size(), summary.keySet().size());
    Map<String, Object> details = estimate.getDetails();
    assertNotNull("details", details);
    assertFalse("empty details", details.isEmpty());
    // by type
    assertEquals(details.keySet().toString(), 6, details.keySet().size());

    // check sampling
    estimator.setSamplingThreshold(searcher.getRawReader().maxDoc() / 2);
    IndexSizeEstimator.Estimate sampledEstimate = estimator.estimate();
    Map<String, Long> sampledFieldsBySize = sampledEstimate.getFieldsBySize();
    assertFalse("empty fieldsBySize", sampledFieldsBySize.isEmpty());
    // verify that the sampled values are within 50% of the original values
    fieldsBySize.forEach((field, size) -> {
      Long sampledSize = sampledFieldsBySize.get(field);
      assertNotNull("sampled size for " + field + " is missing in " + sampledFieldsBySize, sampledSize);
      double delta = (double) size * 0.5;
      assertEquals("sampled size of " + field + " is wildly off", (double)size, (double)sampledSize, delta);
    });
    // verify the reader is still usable - SOLR-13694
    IndexReader reader = searcher.getRawReader();
    for (LeafReaderContext context : reader.leaves()) {
      LeafReader leafReader = context.reader();
      assertTrue("unexpected LeafReader class: " + leafReader.getClass().getName(), leafReader instanceof CodecReader);
      Bits liveDocs = leafReader.getLiveDocs();
      CodecReader codecReader = (CodecReader) leafReader;
      StoredFieldsReader storedFieldsReader = codecReader.getFieldsReader();
      StoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
      assertNotNull(storedFieldsReader);
      for (int docId = 0; docId < leafReader.maxDoc(); docId++) {
        if (liveDocs != null && !liveDocs.get(docId)) {
          continue;
        }
        storedFieldsReader.visitDocument(docId, visitor);
      }
    }
  } finally {
    searcherRef.decref();
    core.close();
  }
}