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

The following examples show how to use org.apache.solr.client.solrj.impl.HttpSolrClient#query() . 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: SolrExampleJettyTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testUtf8PerfDegradation() throws Exception {
  SolrInputDocument doc = new SolrInputDocument();

  doc.addField("id", "1");
  doc.addField("b_is", IntStream.range(0, 30000).boxed().collect(Collectors.toList()));

  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.add(doc);
  client.commit();
  long start = System.nanoTime();
  QueryResponse rsp = client.query(new SolrQuery("*:*"));
  System.out.println("time taken : " + ((System.nanoTime() - start)) / (1000 * 1000));
  assertEquals(1, rsp.getResults().getNumFound());

}
 
Example 2
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void waitForNon403or404or503(HttpSolrClient collectionClient)
    throws Exception {
  SolrException exp = null;
  final TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);

  while (! timeout.hasTimedOut()) {
    boolean missing = false;

    try {
      collectionClient.query(new SolrQuery("*:*"));
    } catch (SolrException e) {
      if (!(e.code() == 403 || e.code() == 503 || e.code() == 404)) {
        throw e;
      }
      exp = e;
      missing = true;
    }
    if (!missing) {
      return;
    }
    Thread.sleep(50);
  }

  fail("Could not find the new collection - " + exp.code() + " : " + collectionClient.getBaseURL());
}
 
Example 3
Source File: ShardSplitTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private int assertConsistentReplicas(Slice shard) throws SolrServerException, IOException {
  long numFound = Long.MIN_VALUE;
  int count = 0;
  for (Replica replica : shard.getReplicas()) {
    HttpSolrClient client = new HttpSolrClient.Builder(replica.getCoreUrl())
        .withHttpClient(cloudClient.getLbClient().getHttpClient()).build();
    QueryResponse response = client.query(new SolrQuery("q", "*:*", "distrib", "false"));
    if (log.isInfoEnabled()) {
      log.info("Found numFound={} on replica: {}", response.getResults().getNumFound(), replica.getCoreUrl());
    }
    if (numFound == Long.MIN_VALUE)  {
      numFound = response.getResults().getNumFound();
    } else  {
      assertEquals("Shard " + shard.getName() + " replicas do not have same number of documents", numFound, response.getResults().getNumFound());
    }
    count++;
  }
  return count;
}
 
Example 4
Source File: TestLeaderElectionWithEmptyReplica.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static int assertConsistentReplicas(CloudSolrClient cloudClient, Slice shard) throws SolrServerException, IOException {
    long numFound = Long.MIN_VALUE;
    int count = 0;
    for (Replica replica : shard.getReplicas()) {
      HttpSolrClient client = new HttpSolrClient.Builder(replica.getCoreUrl())
          .withHttpClient(cloudClient.getLbClient().getHttpClient()).build();
      QueryResponse response = client.query(new SolrQuery("q", "*:*", "distrib", "false"));
//      log.info("Found numFound={} on replica: {}", response.getResults().getNumFound(), replica.getCoreUrl());
      if (numFound == Long.MIN_VALUE)  {
        numFound = response.getResults().getNumFound();
      } else  {
        assertEquals("Shard " + shard.getName() + " replicas do not have same number of documents", numFound, response.getResults().getNumFound());
      }
      count++;
    }
    return count;
  }
 
Example 5
Source File: CaseController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@GetMapping("/healthcheck")
public String healthcheck() throws Exception {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(CommonParams.Q, "*:*");
    params.set(CommonParams.OMIT_HEADER, true);

    HttpSolrClient client = getClient();
    try {
        QueryResponse response = client.query(collection, params);
        if (response.getStatus() == 0) {
            return "Success";
        }
        throw new Exception(response.toString());
    } catch (Exception e) {
        throw e;
    }
}
 
Example 6
Source File: SolrExampleJettyTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void runQueries(HttpSolrClient client, int count, boolean warmup) throws SolrServerException, IOException {
  long start = System.nanoTime();
  for (int i = 0; i < count; i++) {
    client.query(new SolrQuery("*:*"));
  }
  if (warmup) return;
  System.out.println("time taken : " + ((System.nanoTime() - start)) / (1000 * 1000));
}
 
Example 7
Source File: TestBatchUpdate.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBinaryBean()throws Exception{
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.setRequestWriter(new BinaryRequestWriter());
  client.deleteByQuery("*:*"); // delete everything!
  final int[] counter = new int[1];
  counter[0] = 0;
  client.addBeans(new Iterator<Bean>() {

    @Override
    public boolean hasNext() {
      return counter[0] < numdocs;
    }

    @Override
    public Bean next() {
      Bean bean = new Bean();
      bean.id = "" + (++counter[0]);
      bean.cat = "foocat";
      return bean;
    }

    @Override
    public void remove() {
      //do nothing
    }
  });
  client.commit();
  SolrQuery query = new SolrQuery("*:*");
  QueryResponse response = client.query(query);
  assertEquals(0, response.getStatus());
  assertEquals(numdocs, response.getResults().getNumFound());
}
 
Example 8
Source File: TestBatchUpdate.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doIt(HttpSolrClient client) throws SolrServerException, IOException {
  final int[] counter = new int[1];
  counter[0] = 0;
  client.add(new Iterator<SolrInputDocument>() {

    @Override
    public boolean hasNext() {
      return counter[0] < numdocs;
    }

    @Override
    public SolrInputDocument next() {
      SolrInputDocument doc = new SolrInputDocument();
      doc.addField("id", "" + (++counter[0]));
      doc.addField("cat", "foocat");
      return doc;
    }

    @Override
    public void remove() {
      //do nothing

    }
  });
  client.commit();
  SolrQuery query = new SolrQuery("*:*");
  QueryResponse response = client.query(query);
  assertEquals(0, response.getStatus());
  assertEquals(numdocs, response.getResults().getNumFound());
}
 
Example 9
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public String search(HttpSolrClient client) throws IOException, SolrServerException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(CommonParams.Q, "*:*");
    params.set(CommonParams.OMIT_HEADER, true);
    QueryResponse response = client.query(collection, params);
    return "Success";
}
 
Example 10
Source File: SolrProductSearch.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 * NOTE: This method is package-private for backward compat only and should not be made public; its interface is subject to change.
 * Client code should call the solrAvailableCategories or solrSideDeepCategory service instead.
 */
static Map<String, Object> getAvailableCategories(DispatchContext dctx, Map<String, Object> context,
        String catalogId, String categoryId, String productId, String facetPrefix, boolean displayProducts, int viewIndex, int viewSize) {
    Map<String, Object> result;

    try {
        HttpSolrClient client = SolrUtil.getQueryHttpSolrClient((String) context.get("core"));
        SolrQuery solrQuery = new SolrQuery();

        String query;
        if (categoryId != null) {
            query = "+cat:"+ SolrExprUtil.escapeTermFull(categoryId);
        } else if (productId != null) {
            query = "+productId:" + SolrExprUtil.escapeTermFull(productId);
        } else {
            query = "*:*";
        }
        solrQuery.setQuery(query);

        if (catalogId != null) {
            solrQuery.addFilterQuery("+catalog:" + SolrExprUtil.escapeTermFull(catalogId));
        }

        SolrQueryUtil.addDefaultQueryFilters(solrQuery, context);
        SolrQueryUtil.addFilterQueries(solrQuery, UtilGenerics.<String>checkList(context.get("queryFilters")));

        if (displayProducts) {
            if (viewSize > -1) {
                solrQuery.setRows(viewSize);
            } else
                solrQuery.setRows(50000);
            if (viewIndex > -1) {
                // 2016-04-01: This must be calculated
                //solrQuery.setStart(viewIndex);
                if (viewSize > 0) {
                    solrQuery.setStart(viewSize * viewIndex);
                }
            }
        } else {
            solrQuery.setFields("cat");
            solrQuery.setRows(0);
        }

        if(UtilValidate.isNotEmpty(facetPrefix)){
            solrQuery.setFacetPrefix(facetPrefix);
        }

        solrQuery.setFacetMinCount(0);
        solrQuery.setFacet(true);
        solrQuery.addFacetField("cat");
        solrQuery.setFacetLimit(-1);
        if (Debug.verboseOn()) Debug.logVerbose("solr: solrQuery: " + solrQuery, module);
        QueryResponse returnMap = client.query(solrQuery, METHOD.POST);
        result = ServiceUtil.returnSuccess();
        result.put("rows", returnMap);
        result.put("numFound", returnMap.getResults().getNumFound());
    } catch (Exception e) {
        Debug.logError(e.getMessage(), module);
        return ServiceUtil.returnError(e.getMessage());
    }
    return result;
}
 
Example 11
Source File: SolrExampleJettyTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Ignore
public void testUtf8QueryPerf() throws Exception {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.deleteByQuery("*:*");
  client.commit();
  List<SolrInputDocument> docs = new ArrayList<>();
  for (int i = 0; i < 10; i++) {
    SolrInputDocument doc2 = new SolrInputDocument();
    doc2.addField("id", "" + i);
    doc2.addField("fld1_s", "1 value 1 value 1 value 1 value 1 value 1 value 1 value ");
    doc2.addField("fld2_s", "2 value 2 value 2 value 2 value 2 value 2 value 2 value 2 value 2 value 2 value ");
    doc2.addField("fld3_s", "3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value ");
    doc2.addField("fld4_s", "4 value 4 value 4 value 4 value 4 value 4 value 4 value 4 value 4 value ");
    doc2.addField("fld5_s", "5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value ");
    docs.add(doc2);
  }
  client.add(docs);
  client.commit();
  QueryResponse rsp = client.query(new SolrQuery("*:*"));
  assertEquals(10, rsp.getResults().getNumFound());


  client.setParser(new BinaryResponseParser() {
    @Override
    public NamedList<Object> processResponse(InputStream body, String encoding) {
      try {
        IOUtils.skip(body, 1024 * 1000);
      } catch (IOException e) {
        e.printStackTrace();
      }
      return rsp.getResponse();
    }
  });


  runQueries(client, 1000, true);
  /*BinaryResponseWriter.useUtf8CharSeq = false;
  System.out.println("BinaryResponseWriter.useUtf8CharSeq = " + BinaryResponseWriter.useUtf8CharSeq);
  runQueries(client, 10000, false);
  BinaryResponseWriter.useUtf8CharSeq = true;
  System.out.println("BinaryResponseWriter.useUtf8CharSeq = " + BinaryResponseWriter.useUtf8CharSeq);*/
  runQueries(client, 10000, false);
}
 
Example 12
Source File: TestSolrJErrorHandling.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
int getCount(HttpSolrClient client) throws IOException, SolrServerException {
  client.commit();
  QueryResponse rsp = client.query(params("q", "id:test", "fl", "count_i", "wt", "json"));
  int count = ((Number)rsp.getResults().get(0).get("count_i")).intValue();
  return count;
}
 
Example 13
Source File: BasicAuthStandaloneTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasicAuth() throws Exception {

  String authcPrefix = "/admin/authentication";
  String authzPrefix = "/admin/authorization";

  HttpClient cl = null;
  HttpSolrClient httpSolrClient = null;
  try {
    cl = HttpClientUtil.createClient(null);
    String baseUrl = buildUrl(jetty.getLocalPort(), "/solr"); 
    httpSolrClient = getHttpSolrClient(baseUrl);
    
    verifySecurityStatus(cl, baseUrl + authcPrefix, "/errorMessages", null, 20);

    // Write security.json locally. Should cause security to be initialized
    securityConfHandler.persistConf(new SecurityConfHandler.SecurityConfig()
        .setData(Utils.fromJSONString(STD_CONF.replaceAll("'", "\""))));
    securityConfHandler.securityConfEdited();
    verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);

    String command = "{\n" +
        "'set-user': {'harry':'HarryIsCool'}\n" +
        "}";

    doHttpPost(cl, baseUrl + authcPrefix, command, null, null, 401);
    verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication.enabled", "true", 20);

    command = "{\n" +
        "'set-user': {'harry':'HarryIsUberCool'}\n" +
        "}";


    doHttpPost(cl, baseUrl + authcPrefix, command, "solr", "SolrRocks");
    verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/credentials/harry", NOT_NULL_PREDICATE, 20);

    // Read file from SOLR_HOME and verify that it contains our new user
    assertTrue(new String(Utils.toJSON(securityConfHandler.getSecurityConfig(false).getData()), 
        Charset.forName("UTF-8")).contains("harry"));

    // Edit authorization
    verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[1]/role", null, 20);
    doHttpPost(cl, baseUrl + authzPrefix, "{'set-permission': {'name': 'update', 'role':'updaterole'}}", "solr", "SolrRocks");
    command = "{\n" +
        "'set-permission': {'name': 'read', 'role':'solr'}\n" +
        "}";
    doHttpPost(cl, baseUrl + authzPrefix, command, "solr", "SolrRocks");
    try {
      httpSolrClient.query("collection1", new MapSolrParams(Collections.singletonMap("q", "foo")));
      fail("Should return a 401 response");
    } catch (Exception e) {
      // Test that the second doPost request to /security/authorization went through
      verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[2]/role", "solr", 20);
    }
  } finally {
    if (cl != null) {
      HttpClientUtil.close(cl);
      httpSolrClient.close();
    }
  }
}
 
Example 14
Source File: DistributedQueryElevationComponentTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@ShardsFixed(num = 3)
public void test() throws Exception {


  del("*:*");
  indexr(id,"1", "int_i", "1", "text", "XXXX XXXX", "field_t", "anything");
  indexr(id,"2", "int_i", "2", "text", "YYYY YYYY", "plow_t", "rake");
  indexr(id,"3", "int_i", "3", "text", "ZZZZ ZZZZ");
  indexr(id,"4", "int_i", "4", "text", "XXXX XXXX");
  indexr(id,"5", "int_i", "5", "text", "ZZZZ ZZZZ ZZZZ");
  indexr(id,"6", "int_i", "6", "text", "ZZZZ");

  index_specific(2, id, "7", "int_i", "7", "text", "solr");
  commit();

  handle.put("explain", SKIPVAL);
  handle.put("debug", SKIPVAL);
  handle.put("maxScore", SKIPVAL);
  handle.put("timestamp", SKIPVAL);
  handle.put("score", SKIPVAL);
  handle.put("wt", SKIP);
  handle.put("distrib", SKIP);
  handle.put("shards.qt", SKIP);
  handle.put("shards", SKIP);
  handle.put("q", SKIP);
  handle.put("qt", SKIP);
  query("q", "*:*", "qt", "/elevate", "shards.qt", "/elevate", "rows", "500", "sort", "id desc", CommonParams.FL, "id, score, [elevated]");

  query("q", "ZZZZ", "qt", "/elevate", "shards.qt", "/elevate", "rows", "500", CommonParams.FL, "*, [elevated]", "forceElevation", "true", "sort", "int_i desc");

  query("q", "solr", "qt", "/elevate", "shards.qt", "/elevate", "rows", "500", CommonParams.FL, "*, [elevated]", "forceElevation", "true", "sort", "int_i asc");

  query("q", "ZZZZ", "qt", "/elevate", "shards.qt", "/elevate", "rows", "500", CommonParams.FL, "*, [elevated]", "forceElevation", "true", "sort", "id desc");

  // See SOLR-4854 for background on following test code

  // Uses XML response format by default
  QueryResponse response = query("q", "XXXX", "qt", "/elevate", "shards.qt", "/elevate", "rows", "500", CommonParams.FL, "id, [elevated]", "enableElevation", "true",
      "forceElevation", "true", "elevateIds", "6", "sort", "id desc");

  assertTrue(response.getResults().getNumFound() > 0);
  SolrDocument document = response.getResults().get(0);
  assertEquals("6", document.getFieldValue("id"));
  assertEquals(true, document.getFieldValue("[elevated]"));

  // Force javabin format
  final String clientUrl = ((HttpSolrClient)clients.get(0)).getBaseURL();
  HttpSolrClient client = getHttpSolrClient(clientUrl);
  client.setParser(new BinaryResponseParser());
  SolrQuery solrQuery = new SolrQuery("XXXX").setParam("qt", "/elevate").setParam("shards.qt", "/elevate").setRows(500).setFields("id,[elevated]")
      .setParam("enableElevation", "true").setParam("forceElevation", "true").setParam("elevateIds", "6", "wt", "javabin")
      .setSort("id", SolrQuery.ORDER.desc);
  setDistributedParams(solrQuery);
  response = client.query(solrQuery);
  client.close();

  assertTrue(response.getResults().getNumFound() > 0);
  document = response.getResults().get(0);
  assertEquals("6", document.getFieldValue("id"));
  assertEquals(true, document.getFieldValue("[elevated]"));
}
 
Example 15
Source File: TestSolrCLIRunExample.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected void testExample(String exampleName) throws Exception {
  File solrHomeDir = new File(ExternalPaths.SERVER_HOME);
  if (!solrHomeDir.isDirectory())
    fail(solrHomeDir.getAbsolutePath()+" not found and is required to run this test!");

  Path tmpDir = createTempDir();
  File solrExampleDir = tmpDir.toFile();
  File solrServerDir = solrHomeDir.getParentFile();

  for (int pass = 0; pass<2; pass++){
    // need a port to start the example server on
    int bindPort = -1;
    try (ServerSocket socket = new ServerSocket(0)) {
      bindPort = socket.getLocalPort();
    }

    log.info("Selected port {} to start {} example Solr instance on ...", bindPort, exampleName);

    String[] toolArgs = new String[] {
        "-e", exampleName,
        "-serverDir", solrServerDir.getAbsolutePath(),
        "-exampleDir", solrExampleDir.getAbsolutePath(),
        "-p", String.valueOf(bindPort)
    };

    // capture tool output to stdout
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream stdoutSim = new PrintStream(baos, true, StandardCharsets.UTF_8.name());

    RunExampleExecutor executor = new RunExampleExecutor(stdoutSim);
    closeables.add(executor);

    SolrCLI.RunExampleTool tool = new SolrCLI.RunExampleTool(executor, System.in, stdoutSim);
    try {
      int status = tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));
      
      if (status == -1) {
        // maybe it's the port, try again
        try (ServerSocket socket = new ServerSocket(0)) {
          bindPort = socket.getLocalPort();
        }
        Thread.sleep(100);
        status = tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));  
      }
      
      assertEquals("it should be ok "+tool+" "+Arrays.toString(toolArgs),0, status);
    } catch (Exception e) {
      log.error("RunExampleTool failed due to: {}; stdout from tool prior to failure: {}"
          , e , baos.toString(StandardCharsets.UTF_8.name())); // logOk
      throw e;
    }

    String toolOutput = baos.toString(StandardCharsets.UTF_8.name());

    // dump all the output written by the SolrCLI commands to stdout
    //System.out.println("\n\n"+toolOutput+"\n\n");

    File exampleSolrHomeDir = new File(solrExampleDir, exampleName+"/solr");
    assertTrue(exampleSolrHomeDir.getAbsolutePath() + " not found! run " +
            exampleName + " example failed; output: " + toolOutput,
        exampleSolrHomeDir.isDirectory());

    if ("techproducts".equals(exampleName)) {
      HttpSolrClient solrClient = getHttpSolrClient("http://localhost:" + bindPort + "/solr/" + exampleName);
      try{
        SolrQuery query = new SolrQuery("*:*");
        QueryResponse qr = solrClient.query(query);
        long numFound = qr.getResults().getNumFound();
        if (numFound == 0) {
          // brief wait in case of timing issue in getting the new docs committed
          log.warn("Going to wait for 1 second before re-trying query for techproduct example docs ...");
          try {
            Thread.sleep(1000);
          } catch (InterruptedException ignore) {
            Thread.interrupted();
          }
          numFound = solrClient.query(query).getResults().getNumFound();
        }
        assertTrue("expected 32 docs in the " + exampleName + " example but found " + numFound + ", output: " + toolOutput,
            numFound == 32);
      }finally{
        solrClient.close();
      }
    }

    // stop the test instance
    executor.execute(org.apache.commons.exec.CommandLine.parse("bin/solr stop -p " + bindPort));
  }
}