Java Code Examples for org.apache.solr.client.solrj.impl.HttpSolrClient#request()

The following examples show how to use org.apache.solr.client.solrj.impl.HttpSolrClient#request() . 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: SolrProductSearch.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
public static Map<String, Object> reloadSolrSecurityAuthorizations(DispatchContext dctx, Map<String, Object> context) {
    if (!SolrUtil.isSystemInitialized()) { // NOTE: this must NOT use SolrUtil.isSolrLocalWebappPresent() anymore
        return ServiceUtil.returnFailure("Solr not enabled or system not ready");
    }
    try {
        HttpSolrClient client = SolrUtil.getAdminHttpSolrClientFromUrl(SolrUtil.getSolrWebappUrl());
        //ModifiableSolrParams params = new ModifiableSolrParams();
        //// this is very sketchy, I don't think ModifiableSolrParams were meant for this
        //params.set("set-user-role", (String) null);
        //SolrRequest<?> request = new GenericSolrRequest(METHOD.POST, CommonParams.AUTHZ_PATH, params);
        SolrRequest<?> request = new DirectJsonRequest(CommonParams.AUTHZ_PATH,
                "{\"set-user-role\":{}}"); // "{\"set-user-role\":{\"dummy\":\"dummy\"}}"
        client.request(request);
        Debug.logInfo("Solr: reloadSolrSecurityAuthorizations: invoked reload", module);
        return ServiceUtil.returnSuccess();
    } catch (Exception e) {
        Debug.logError("Solr: reloadSolrSecurityAuthorizations: error: " + e.getMessage(), module);
        return ServiceUtil.returnError("Error reloading Solr security authorizations: " + e.getMessage());
    }
}
 
Example 2
Source File: DistributedVersionInfoTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Query the real-time get handler for a specific doc by ID to verify it
 * exists in the provided server, using distrib=false so it doesn't route to another replica.
 */
@SuppressWarnings("rawtypes")
protected Long assertDocExists(HttpSolrClient solr, String coll, String docId, Long expVers) throws Exception {
  QueryRequest qr = new QueryRequest(params("qt", "/get", "id", docId, "distrib", "false", "fl", "id,_version_"));
  NamedList rsp = solr.request(qr);
  SolrDocument doc = (SolrDocument)rsp.get("doc");
  String match = JSONTestUtil.matchObj("/id", doc, docId);
  assertTrue("Doc with id=" + docId + " not found in " + solr.getBaseURL() +
      " due to: " + match + "; rsp=" + rsp, match == null);

  Long vers = (Long)doc.getFirstValue("_version_");
  assertNotNull(vers);
  if (expVers != null)
    assertEquals("expected version of doc "+docId+" to be "+expVers, expVers, vers);

  return vers;
}
 
Example 3
Source File: ExportTool.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static long getDocCount(String coreName, HttpSolrClient client) throws SolrServerException, IOException {
  SolrQuery q = new SolrQuery("*:*");
  q.setRows(0);
  q.add("distrib", "false");
  GenericSolrRequest request = new GenericSolrRequest(SolrRequest.METHOD.GET,
      "/" + coreName + "/select", q);
  NamedList<Object> res = client.request(request);
  SolrDocumentList sdl = (SolrDocumentList) res.get("response");
  return sdl.getNumFound();
}
 
Example 4
Source File: TestSolrCloudWithDelegationTokens.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doSolrRequest(HttpSolrClient client,
                           @SuppressWarnings({"rawtypes"})SolrRequest request,
    int expectedStatusCode) throws Exception {
  try {
    client.request(request);
    assertEquals(HttpStatus.SC_OK, expectedStatusCode);
  } catch (BaseHttpSolrClient.RemoteSolrException ex) {
    assertEquals(expectedStatusCode, ex.code());
  }
}
 
Example 5
Source File: TestSolrCloudWithDelegationTokens.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doSolrRequest(HttpSolrClient client,
                           @SuppressWarnings({"rawtypes"})SolrRequest request, String collectionName,
    int expectedStatusCode) throws Exception {
  try {
    client.request(request, collectionName);
    assertEquals(HttpStatus.SC_OK, expectedStatusCode);
  } catch (BaseHttpSolrClient.RemoteSolrException ex) {
    assertEquals(expectedStatusCode, ex.code());
  }
}
 
Example 6
Source File: TestCloudManagedSchema.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set(CoreAdminParams.ACTION, CoreAdminParams.CoreAdminAction.STATUS.toString());
  QueryRequest request = new QueryRequest(params);
  request.setPath("/admin/cores");
  int which = r.nextInt(clients.size());
  HttpSolrClient client = (HttpSolrClient)clients.get(which);
  String previousBaseURL = client.getBaseURL();
  // Strip /collection1 step from baseURL - requests fail otherwise
  client.setBaseURL(previousBaseURL.substring(0, previousBaseURL.lastIndexOf("/")));
  @SuppressWarnings({"rawtypes"})
  NamedList namedListResponse = client.request(request);
  client.setBaseURL(previousBaseURL); // Restore baseURL 
  @SuppressWarnings({"rawtypes"})
  NamedList status = (NamedList)namedListResponse.get("status");
  @SuppressWarnings({"rawtypes"})
  NamedList collectionStatus = (NamedList)status.getVal(0);
  String collectionSchema = (String)collectionStatus.get(CoreAdminParams.SCHEMA);
  // Make sure the upgrade to managed schema happened
  assertEquals("Schema resource name differs from expected name", "managed-schema", collectionSchema);

  SolrZkClient zkClient = new SolrZkClient(zkServer.getZkHost(), 30000);
  try {
    // Make sure "DO NOT EDIT" is in the content of the managed schema
    String fileContent = getFileContentFromZooKeeper(zkClient, "/solr/configs/conf1/managed-schema");
    assertTrue("Managed schema is missing", fileContent.contains("DO NOT EDIT"));

    // Make sure the original non-managed schema is no longer in ZooKeeper
    assertFileNotInZooKeeper(zkClient, "/solr/configs/conf1", "schema.xml");

    // Make sure the renamed non-managed schema is present in ZooKeeper
    fileContent = getFileContentFromZooKeeper(zkClient, "/solr/configs/conf1/schema.xml.bak");
    assertTrue("schema file doesn't contain '<schema'", fileContent.contains("<schema"));
  } finally {
    if (zkClient != null) {
      zkClient.close();
    }
  }
}
 
Example 7
Source File: GraphExpressionTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testGraphHandler() throws Exception {


  new UpdateRequest()
      .add(id, "0", "from_s", "bill", "to_s", "jim", "message_t", "Hello jim")
      .add(id, "1", "from_s", "bill", "to_s", "sam", "message_t", "Hello sam")
      .add(id, "2", "from_s", "bill", "to_s", "max", "message_t", "Hello max")
      .add(id, "3", "from_s", "max",  "to_s", "kip", "message_t", "Hello kip")
      .add(id, "4", "from_s", "sam",  "to_s", "steve", "message_t", "Hello steve")
      .add(id, "5", "from_s", "jim",  "to_s", "ann", "message_t", "Hello steve")
      .commit(cluster.getSolrClient(), COLLECTION);

  commit();

  List<JettySolrRunner> runners = cluster.getJettySolrRunners();
  JettySolrRunner runner = runners.get(0);
  String url = runner.getBaseUrl().toString();

  HttpSolrClient client = getHttpSolrClient(url);
  ModifiableSolrParams params = new ModifiableSolrParams();


  String expr = "sort(by=\"node asc\", gatherNodes(collection1, " +
      "walk=\"bill->from_s\"," +
      "trackTraversal=\"true\"," +
      "gather=\"to_s\"))";

  params.add("expr", expr);
  QueryRequest query = new QueryRequest(params);
  query.setPath("/collection1/graph");

  query.setResponseParser(new InputStreamResponseParser("xml"));
  query.setMethod(SolrRequest.METHOD.POST);

  NamedList<Object> genericResponse = client.request(query);


  InputStream stream = (InputStream)genericResponse.get("stream");
  InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
  String xml = readString(reader);
  //Validate the nodes
  String error = BaseTestHarness.validateXPath(xml,
      "//graph/node[1][@id ='jim']",
      "//graph/node[2][@id ='max']",
      "//graph/node[3][@id ='sam']");
  if(error != null) {
    throw new Exception(error);
  }
  //Validate the edges
  error = BaseTestHarness.validateXPath(xml,
      "//graph/edge[1][@source ='bill']",
      "//graph/edge[1][@target ='jim']",
      "//graph/edge[2][@source ='bill']",
      "//graph/edge[2][@target ='max']",
      "//graph/edge[3][@source ='bill']",
      "//graph/edge[3][@target ='sam']");

  if(error != null) {
    throw new Exception(error);
  }

  client.close();
}
 
Example 8
Source File: HttpPartitionTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private NamedList realTimeGetDocId(HttpSolrClient solr, String docId) throws SolrServerException, IOException {
  QueryRequest qr = new QueryRequest(params("qt", "/get", "id", docId, "distrib", "false"));
  return solr.request(qr);
}
 
Example 9
Source File: LeaderVoteWaitTimeoutTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private NamedList realTimeGetDocId(HttpSolrClient solr, String docId) throws SolrServerException, IOException {
  QueryRequest qr = new QueryRequest(params("qt", "/get", "id", docId, "distrib", "false"));
  return solr.request(qr);
}
 
Example 10
Source File: TestCloudConsistency.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private NamedList realTimeGetDocId(HttpSolrClient solr, String docId) throws SolrServerException, IOException {
  QueryRequest qr = new QueryRequest(params("qt", "/get", "id", docId, "distrib", "false"));
  return solr.request(qr);
}