Java Code Examples for org.apache.solr.request.SolrRequestInfo#clearRequestInfo()

The following examples show how to use org.apache.solr.request.SolrRequestInfo#clearRequestInfo() . 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: HttpSolrCall.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void destroy() {
  try {
    if (solrReq != null) {
      log.debug("Closing out SolrRequest: {}", solrReq);
      solrReq.close();
    }
  } finally {
    try {
      if (core != null) core.close();
    } finally {
      if (mustClearSolrRequestInfo) {
        SolrRequestInfo.clearRequestInfo();
      }
    }
    AuthenticationPlugin authcPlugin = cores.getAuthenticationPlugin();
    if (authcPlugin != null) authcPlugin.closeRequest();
  }
}
 
Example 2
Source File: TestCrossCoreJoin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public String query(SolrCore core, SolrQueryRequest req) throws Exception {
  String handler = "standard";
  if (req.getParams().get("qt") != null) {
    handler = req.getParams().get("qt");
  }
  if (req.getParams().get("wt") == null){
    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
    params.set("wt", "xml");
    req.setParams(params);
  }
  SolrQueryResponse rsp = new SolrQueryResponse();
  SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
  core.execute(core.getRequestHandler(handler), req, rsp);
  if (rsp.getException() != null) {
    throw rsp.getException();
  }
  StringWriter sw = new StringWriter(32000);
  QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
  responseWriter.write(sw, req, rsp);
  req.close();
  SolrRequestInfo.clearRequestInfo();
  return sw.toString();
}
 
Example 3
Source File: IgnoreCommitOptimizeUpdateProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
SolrQueryResponse processCommit(final String chain, boolean optimize, Boolean commitEndPoint) 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());

  if (commitEndPoint != null) {
    ((ModifiableSolrParams)req.getParams()).set(
        DistributedUpdateProcessor.COMMIT_END_POINT, commitEndPoint.booleanValue());
  }

  try {
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req,rsp));
    CommitUpdateCommand cmd = new CommitUpdateCommand(req, false);
    cmd.optimize = optimize;
    UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
    processor.processCommit(cmd);
  } finally {
    SolrRequestInfo.clearRequestInfo();
    req.close();
  }
  return rsp;
}
 
Example 4
Source File: TestOverriddenPrefixQueryForCustomFieldType.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * @see org.apache.lucene.search.QueryUtils#check
 * @see org.apache.lucene.search.QueryUtils#checkEqual
 */
protected void assertQueryEquals(final SolrQueryRequest req,
                                 final String... inputs) throws Exception {

  final Query[] queries = new Query[inputs.length];

  try {
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    for (int i = 0; i < inputs.length; i++) {
      queries[i] = (QParser.getParser(inputs[i], req).getQuery());
    }
  } finally {
    SolrRequestInfo.clearRequestInfo();
  }

  for (int i = 0; i < queries.length; i++) {
    org.apache.lucene.search.QueryUtils.check(queries[i]);
    for (int j = i; j < queries.length; j++) {
      org.apache.lucene.search.QueryUtils.checkEqual(queries[i], queries[j]);
    }
  }
}
 
Example 5
Source File: UpdateProcessorTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Runs a document through the specified chain, and returns the final
 * document used when the chain is completed (NOTE: some chains may
 * modify the document in place
 */
protected SolrInputDocument processAdd(final String chain,
                                       final SolrParams requestParams,
                                       final SolrInputDocument docIn)
  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, requestParams);
  try {
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.solrDoc = docIn;

    UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
    if (null != processor) {
      // test chain might be empty or short circuited.
      processor.processAdd(cmd);
    }

    return cmd.solrDoc;
  } finally {
    SolrRequestInfo.clearRequestInfo();
    req.close();
  }
}
 
Example 6
Source File: TestHarness.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Processes a "query" using a user constructed SolrQueryRequest, and closes the request at the end.
 *
 * @param handler the name of the request handler to process the request
 * @param req the Query to process, will be closed.
 * @return The XML response to the query
 * @exception Exception any exception in the response.
 * @exception IOException if there is a problem writing the XML
 * @see LocalSolrQueryRequest
 */
public String query(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);
    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 7
Source File: TestZKPropertiesWriter.java    From lucene-solr with 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 8
Source File: UUIDUpdateProcessorFallbackTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
SolrInputDocument processAdd(final String chain,
                             final SolrInputDocument docIn, SolrParams params)
    throws IOException {

  SolrCore core = h.getCore();
  UpdateRequestProcessorChain pc = chain == null ?
      core.getUpdateProcessorChain(params) :
      core.getUpdateProcessingChain(chain);
  assertNotNull("No Chain named: " + chain, pc);

  SolrQueryResponse rsp = new SolrQueryResponse();

  SolrQueryRequest req = new LocalSolrQueryRequest
      (core, params);
  try {
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req,rsp));
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.solrDoc = docIn;

    UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
    processor.processAdd(cmd);

    return cmd.solrDoc;
  } finally {
    SolrRequestInfo.clearRequestInfo();
    req.close();
  }
}
 
Example 9
Source File: DefaultValueUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Runs a document through the specified chain, and returns the final 
 * document used when the chain is completed (NOTE: some chains may 
 * modify the document in place
 */
SolrInputDocument processAdd(final String chain, 
                             final SolrInputDocument docIn) 
  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());
  try {
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req,rsp));
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.solrDoc = docIn;

    UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
    processor.processAdd(cmd);

    return cmd.solrDoc;
  } finally {
    SolrRequestInfo.clearRequestInfo();
    req.close();
  }
}
 
Example 10
Source File: TestScoreJoinQPScore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Ignore("SOLR-7814, also don't forget cover boost at testCacheHit()")
public void testBoost() throws Exception {
  indexDataForScorring();
  ScoreMode score = ScoreMode.values()[random().nextInt(ScoreMode.values().length)];

  final SolrQueryRequest req = req("q", "{!join from=movieId_s to=id score=" + score + " b=200}title:movie", "fl", "id,score", "omitHeader", "true");
  SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, new SolrQueryResponse()));
  final Query luceneQ = QParser.getParser(req.getParams().get("q"), req).getQuery().rewrite(req.getSearcher().getSlowAtomicReader());
  assertTrue(luceneQ instanceof BoostQuery);
  float boost = ((BoostQuery) luceneQ).getBoost();
  assertEquals("" + luceneQ, Float.floatToIntBits(200), Float.floatToIntBits(boost));
  SolrRequestInfo.clearRequestInfo();
  req.close();
}
 
Example 11
Source File: QueryEqualityTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * NOTE: defType is not only used to pick the parser, but, if non-null it is
 * also to record the parser being tested for coverage sanity checking
 *
 * @see QueryUtils#check
 * @see QueryUtils#checkEqual
 * @see #testParserCoverage
 */
protected void assertQueryEquals(final String defType,
                                 final SolrQueryRequest req,
                                 final String... inputs) throws Exception {

  if (null != defType) qParsersTested.add(defType);

  final Query[] queries = new Query[inputs.length];

  try {
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req,rsp));
    for (int i = 0; i < inputs.length; i++) {
      queries[i] = QParser.getParser(inputs[i], defType, true, req).getQuery();
    }
  } finally {
    SolrRequestInfo.clearRequestInfo();
  }

  for (int i = 0; i < queries.length; i++) {
    QueryUtils.check(queries[i]);
    // yes starting j=0 is redundent, we're making sure every query
    // is equal to itself, and that the quality checks work regardless
    // of which caller/callee is used.
    for (int j = 0; j < queries.length; j++) {
      QueryUtils.checkEqual(queries[i], queries[j]);
    }
  }
}
 
Example 12
Source File: TestGroupingSearch.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGroupingSimpleFormatArrayIndexOutOfBoundsExceptionWithJavaBin() throws Exception {
  assertU(add(doc("id", "1", "nullfirst", "1")));
  assertU(add(doc("id", "2", "nullfirst", "1")));
  assertU(add(doc("id", "3", "nullfirst", "2")));
  assertU(add(doc("id", "4", "nullfirst", "2")));
  assertU(add(doc("id", "5", "nullfirst", "2")));
  assertU(add(doc("id", "6", "nullfirst", "3")));
  assertU(commit());

  SolrQueryRequest request =
      req("q", "*:*","group", "true", "group.field", "nullfirst", "group.main", "true", "wt", "javabin", "start", "4", "rows", "10");

  SolrQueryResponse response = new SolrQueryResponse();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(request, response));
    String handlerName = request.getParams().get(CommonParams.QT);
    h.getCore().execute(h.getCore().getRequestHandler(handlerName), request, response);
    BinaryResponseWriter responseWriter = new BinaryResponseWriter();
    responseWriter.write(out, request, response);
  } finally {
    request.close();
    SolrRequestInfo.clearRequestInfo();
  }

  assertEquals(6, ((ResultContext) response.getResponse()).getDocList().matches());
  new BinaryResponseParser().processResponse(new ByteArrayInputStream(out.toByteArray()), "");
  out.close();
}
 
Example 13
Source File: TestSchemalessBufferedUpdates.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private SolrInputDocument processAdd(final SolrInputDocument docIn) throws IOException {
  UpdateRequestProcessorChain processorChain = h.getCore().getUpdateProcessingChain(UPDATE_CHAIN);
  assertNotNull("Undefined URP chain '" + UPDATE_CHAIN + "'", processorChain);
  List <UpdateRequestProcessorFactory> factoriesUpToDUP = new ArrayList<>();
  for (UpdateRequestProcessorFactory urpFactory : processorChain.getProcessors()) {
    factoriesUpToDUP.add(urpFactory);
    if (urpFactory.getClass().equals(DistributedUpdateProcessorFactory.class)) 
      break;
  }
  UpdateRequestProcessorChain chainUpToDUP = new UpdateRequestProcessorChain(factoriesUpToDUP, h.getCore());
  assertNotNull("URP chain '" + UPDATE_CHAIN + "'", chainUpToDUP);
  SolrQueryResponse rsp = new SolrQueryResponse();
  SolrQueryRequest req = req();
  try {
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.solrDoc = docIn;
    UpdateRequestProcessor processor = chainUpToDUP.createProcessor(req, rsp);
    processor.processAdd(cmd);
    if (cmd.solrDoc.get("f_dt").getValue() instanceof Date) {
      // Non-JSON types (Date in this case) aren't handled properly in noggit-0.6.  Although this is fixed in
      // https://github.com/yonik/noggit/commit/ec3e732af7c9425e8f40297463cbe294154682b1 to call obj.toString(), 
      // Date::toString produces a Date representation that Solr doesn't like, so we convert using Instant::toString
      cmd.solrDoc.get("f_dt").setValue(((Date) cmd.solrDoc.get("f_dt").getValue()).toInstant().toString());
    }
    return cmd.solrDoc;
  } finally {
    SolrRequestInfo.clearRequestInfo();
    req.close();
  }
}
 
Example 14
Source File: DocExpirationUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void run() {
  // setup the request context early so the logging (including any from 
  // shouldWeDoPeriodicDelete() ) includes the core context info
  final LocalSolrQueryRequest req = new LocalSolrQueryRequest
    (factory.core, Collections.<String,String[]>emptyMap());
  try {
    // HACK: to indicate to PKI that this is a server initiated request for the purposes
    // of distributed requet/credential forwarding...
    req.setUserPrincipalName(PKIAuthenticationPlugin.NODE_IS_USER);
    
    final SolrQueryResponse rsp = new SolrQueryResponse();
    rsp.addResponseHeader(new SimpleOrderedMap<>(1));
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    try {
      
      if (! factory.iAmInChargeOfPeriodicDeletes() ) {
        // No-Op
        return;
      }
      log.info("Beginning periodic deletion of expired docs");

      UpdateRequestProcessorChain chain = core.getUpdateProcessingChain(deleteChainName);
      UpdateRequestProcessor proc = chain.createProcessor(req, rsp);
      if (null == proc) {
        log.warn("No active processors, skipping automatic deletion of expired docs using chain: {}"
                 , deleteChainName);
        return;
      }
      try {
        DeleteUpdateCommand del = new DeleteUpdateCommand(req);
        del.setQuery("{!cache=false}" + expireField + ":[* TO " +
            SolrRequestInfo.getRequestInfo().getNOW().toInstant()
                     + "]");
        proc.processDelete(del);
        
        // TODO: should this be more configurable? 
        // TODO: in particular: should hard commit be optional?
        CommitUpdateCommand commit = new CommitUpdateCommand(req, false);
        commit.softCommit = true;
        commit.openSearcher = true;
        proc.processCommit(commit);
        
      } finally {
        try {
          proc.finish();
        } finally {
          proc.close();
        }
      }

      log.info("Finished periodic deletion of expired docs");
    } catch (IOException ioe) {
      log.error("IOException in periodic deletion of expired docs: {}",
                ioe.getMessage(), ioe);
      // DO NOT RETHROW: ScheduledExecutor will suppress subsequent executions
    } catch (RuntimeException re) {
      log.error("Runtime error in periodic deletion of expired docs: {}",
                re.getMessage(), re);
      // DO NOT RETHROW: ScheduledExecutor will suppress subsequent executions
    } finally {
      SolrRequestInfo.clearRequestInfo();
    }
  } finally {
    req.close();
  }
}
 
Example 15
Source File: SolrIndexSearcher.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Warm this searcher based on an old one (primarily for auto-cache warming).
 */
@SuppressWarnings({"unchecked"})
public void warm(SolrIndexSearcher old) {
  // Make sure this is first! filters can help queryResults execute!
  long warmingStartTime = System.nanoTime();
  // warm the caches in order...
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("warming", "true");
  for (int i = 0; i < cacheList.length; i++) {
    if (log.isDebugEnabled()) {
      log.debug("autowarming [{}] from [{}]\n\t{}", this, old, old.cacheList[i]);
    }

    final SolrQueryRequest req = new LocalSolrQueryRequest(core, params) {
      @Override
      public SolrIndexSearcher getSearcher() {
        return SolrIndexSearcher.this;
      }

      @Override
      public void close() {}
    };

    final SolrQueryResponse rsp = new SolrQueryResponse();
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    try {
      cacheList[i].warm(this, old.cacheList[i]);
    } finally {
      try {
        req.close();
      } finally {
        SolrRequestInfo.clearRequestInfo();
      }
    }

    if (log.isDebugEnabled()) {
      log.debug("autowarming result for [{}]\n\t{}", this, cacheList[i]);
    }
  }
  warmupTime = TimeUnit.MILLISECONDS.convert(System.nanoTime() - warmingStartTime, TimeUnit.NANOSECONDS);
}
 
Example 16
Source File: TestSubQueryTransformer.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testJustJohnJavabin() throws Exception {
  final SolrQueryRequest johnTwoFL = req(johnAndNancyParams);
  ModifiableSolrParams params = new ModifiableSolrParams(johnTwoFL.getParams());
  params.set("q","name_s:john");
  params.set("wt","javabin");
  
  johnTwoFL.setParams(params);
  
  final NamedList<Object> unmarshalled;
  SolrCore core = johnTwoFL.getCore();
  SolrQueryResponse rsp = new SolrQueryResponse();
  SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));

  SolrQueryResponse response = h.queryAndResponse(
      johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);

  BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  responseWriter.write(bytes, johnTwoFL, response);

  try (JavaBinCodec jbc = new JavaBinCodec()) {
    unmarshalled = (NamedList<Object>) jbc.unmarshal(
        new ByteArrayInputStream(bytes.toByteArray()));
  }

  johnTwoFL.close();
  SolrRequestInfo.clearRequestInfo();
  
  SolrDocumentList resultDocs = (SolrDocumentList)(unmarshalled.get("response"));
  
    Map<String,String> engText = new HashMap<>();
    engText.put("text_t", "These guys develop stuff");
    
    Map<String,String> engId = new HashMap<>();
    engId.put("text_t", "These guys develop stuff");
    engId.put("dept_id_s_dv", "Engineering");
    
    for (int docNum : new int []{0, peopleMultiplier-1}) {
      SolrDocument employeeDoc = resultDocs.get(docNum);
      assertEquals("john", employeeDoc.getFieldValue("name_s_dv"));
      for (String subResult : new String []{"depts", "depts_i"}) {

        SolrDocumentList subDoc = (SolrDocumentList)employeeDoc.getFieldValue(subResult);
        for (int deptNum : new int []{0, deptMultiplier-1}) {
          SolrDocument deptDoc = subDoc.get(deptNum);
          Object expectedDept = (subResult.equals("depts") ? engText : engId);
          assertTrue( "" + expectedDept + " equals to " + deptDoc,
              expectedDept.equals(deptDoc));
        }
    }
  }
}