Java Code Examples for org.apache.solr.client.solrj.request.QueryRequest#process()

The following examples show how to use org.apache.solr.client.solrj.request.QueryRequest#process() . 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: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected QueryResponse query(SolrClient solrClient, boolean andShards, String json, ModifiableSolrParams params) throws Exception
{
    params.set("distrib", "false");
    QueryRequest request = getAlfrescoRequest(json, params);
    QueryResponse controlRsp = request.process(solrClient);
    SOLR_RESPONSE_COMPARATOR.validateResponse(controlRsp);
    if (andShards)
    {
        params.remove("distrib");
        setDistributedParams(params);
        QueryResponse rsp = queryRandomShard(json, params);
        SOLR_RESPONSE_COMPARATOR.compareResponses(rsp, controlRsp);
        return rsp;
    }
    else
    {
        return controlRsp;
    }
}
 
Example 2
Source File: SolrUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
public QueryResponse runQuery(SolrClient solrClient, SolrQuery solrQuery) throws Throwable {
    if (solrQuery != null) {
        try {
            QueryRequest req = new QueryRequest(solrQuery, METHOD.POST);
            String username = PropertiesUtil.getProperty("ranger.solr.audit.user");
            String password = PropertiesUtil.getProperty("ranger.solr.audit.user.password");
            if (username != null && password != null) {
                req.setBasicAuthCredentials(username, password);
            }

            return req.process(solrClient);
        } catch (Throwable e) {
            logger.error("Error from Solr server. ", e);
            throw e;
        }
    }
    return null;
}
 
Example 3
Source File: TestStreamBody.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testQtUpdateFails() throws Exception {
  enableStreamBody(true);
  SolrQuery query = new SolrQuery();
  query.setQuery( "*:*" );//for anything
  query.add("echoHandler","true");
  //sneaky sneaky
  query.add("qt","/update");
  query.add(CommonParams.STREAM_BODY,"<delete><query>*:*</query></delete>");

  QueryRequest queryRequest = new QueryRequest(query) {
    @Override
    public String getPath() { //don't let superclass substitute qt for the path
      return "/select";
    }
  };
  try {
    queryRequest.process(getSolrClient());
    fail();
  } catch (SolrException se) {
    assertTrue(se.getMessage(), se.getMessage().contains("Bad contentType for search handler :text/xml"));
  }
}
 
Example 4
Source File: SearchStream.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void open() throws IOException {
  if(cache != null) {
    cloudSolrClient = cache.getCloudSolrClient(zkHost);
  } else {
    final List<String> hosts = new ArrayList<>();
    hosts.add(zkHost);
    cloudSolrClient = new CloudSolrClient.Builder(hosts, Optional.empty()).build();
  }


  QueryRequest request = new QueryRequest(params, SolrRequest.METHOD.POST);
  try {
    QueryResponse response = request.process(cloudSolrClient, collection);
    SolrDocumentList docs = response.getResults();
    documentIterator = docs.iterator();
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example 5
Source File: LegacyAbstractAnalyticsCloudTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected NamedList<Object> queryLegacyCloudAnalytics(String[] testParams) throws SolrServerException, IOException, InterruptedException, TimeoutException {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("q", "*:*");
  params.set("indent", "true");
  params.set("olap", "true");
  params.set("rows", "0");
  for (int i = 0; i + 1 < testParams.length;) {
    params.add(testParams[i++], testParams[i++]);
  }
  cluster.waitForAllNodes(10000);
  QueryRequest qreq = new QueryRequest(params);
  QueryResponse resp = qreq.process(cluster.getSolrClient(), COLLECTIONORALIAS);
  final NamedList<Object> response = resp.getResponse();
  assertRequestTimeout(params);
  return response;
}
 
Example 6
Source File: TestSuggesterResponse.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptySuggesterResponse() throws Exception {
  getSolrClient();
  addSampleDocs();

  SolrQuery query = new SolrQuery("*:*");
  query.set(CommonParams.QT, "/suggest");
  query.set("suggest.dictionary", "mySuggester");
  query.set("suggest.q", "Empty");
  query.set("suggest.build", true);
  QueryRequest request = new QueryRequest(query);
  QueryResponse queryResponse = request.process(client);
  SuggesterResponse response = queryResponse.getSuggesterResponse();
  Map<String, List<String>> dictionary2suggestions = response.getSuggestedTerms();
  assertTrue(dictionary2suggestions.keySet().contains("mySuggester"));

  List<String> mySuggester = dictionary2suggestions.get("mySuggester");
  assertEquals(0, mySuggester.size());
}
 
Example 7
Source File: FeaturesSelectionStream.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
public NamedList<Double> call() throws Exception {
  ModifiableSolrParams params = new ModifiableSolrParams();
  HttpSolrClient solrClient = cache.getHttpSolrClient(baseUrl);

  params.add(DISTRIB, "false");
  params.add("fq","{!igain}");

  for(Map.Entry<String, String> entry : paramsMap.entrySet()) {
    params.add(entry.getKey(), entry.getValue());
  }

  params.add("outcome", outcome);
  params.add("positiveLabel", Integer.toString(positiveLabel));
  params.add("field", field);
  params.add("numTerms", String.valueOf(numTerms));

  QueryRequest request= new QueryRequest(params);
  QueryResponse response = request.process(solrClient);
  NamedList res = response.getResponse();
  return res;
}
 
Example 8
Source File: EmbeddedSolrNoSerializeTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void doTestAssertTagStreaming(BiFunction<ModifiableSolrParams,String,QueryRequest> newQueryRequest) throws IOException, SolrServerException {
  ModifiableSolrParams params = params();
  String input = "foo boston bar";//just one tag;
  QueryRequest req = newQueryRequest.apply(params, input);
  req.setPath("/tag");

  final AtomicReference<SolrDocument> refDoc = new AtomicReference<>();
  req.setStreamingResponseCallback(new StreamingResponseCallback() {
    @Override
    public void streamSolrDocument(SolrDocument doc) {
      refDoc.set(doc);
    }

    @Override
    public void streamDocListInfo(long numFound, long start, Float maxScore) {

    }
  });
  QueryResponse rsp = req.process(solrServer);
  assertNotNull(rsp.getResponse().get("tags"));
  assertNotNull(refDoc.get());
  assertEquals("Boston", ((Field)refDoc.get().getFieldValue("name")).stringValue());
}
 
Example 9
Source File: TestRealTimeGet.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private QueryResponse getIdsResponse(ExpectedResult expectedResult) throws Exception {
  StringBuilder builder = new StringBuilder();
  for (int i = 0; i < expectedResult.ids.length; ++i) {
    if (i != 0) {
      builder.append(",");
    }
    builder.append(expectedResult.ids[ i ]);
  }
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("ids", builder.toString());
  if (expectedResult.fl != null) {
    params.add("fl", expectedResult.fl);
  }
  QueryRequest request = getRealTimeGetRequest(params);
  return request.process(expectedResult.server);
}
 
Example 10
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void rebalanceLeaderUsingStandardRequest() throws IOException, SolrServerException {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("action", CollectionParams.CollectionAction.REBALANCELEADERS.toString());
  params.set("collection", COLLECTION_NAME);
  QueryRequest request = new QueryRequest(params);
  request.setPath("/admin/collections");
  QueryResponse resp = request.process(cluster.getSolrClient());
  assertTrue("All leaders should have been verified", resp.getResponse().get("Summary").toString().contains("Success"));
  assertEquals("Call to rebalanceLeaders failed ", 0, resp.getStatus());
}
 
Example 11
Source File: TestDynamicFieldNamesIndexCorrectly.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SolrDocumentList getSolrResponse(SolrQuery solrQuery, String collection)
    throws SolrServerException, IOException {
  final QueryResponse response;
  SolrDocumentList list = null;
  final QueryRequest req = new QueryRequest(solrQuery);
  cloudClient.setDefaultCollection(collection);
  response = req.process(cloudClient);
  list = response.getResults();
  return list;
}
 
Example 12
Source File: SolrAnalyticsTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Object queryCloudObject(SolrParams params) {
  QueryResponse resp;
  try {
    cluster.waitForAllNodes(10000);
    QueryRequest qreq = new QueryRequest(params);
    resp = qreq.process(cluster.getSolrClient(), COLLECTIONORALIAS);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
  return convertDatesToStrings(resp.getResponse().asShallowMap());
}
 
Example 13
Source File: RandomStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void open() throws IOException {
  if(cache != null) {
    cloudSolrClient = cache.getCloudSolrClient(zkHost);
  } else {
    final List<String> hosts = new ArrayList<>();
    hosts.add(zkHost);
    cloudSolrClient = new CloudSolrClient.Builder(hosts, Optional.empty()).withSocketTimeout(30000).withConnectionTimeout(15000).build();
  }

  ModifiableSolrParams params = getParams(this.props);

  params.remove(SORT); //Override any sort.

  Random rand = new Random();
  int seed = rand.nextInt();

  String sortField = "random_"+seed;
  params.add(SORT, sortField+" asc");

  QueryRequest request = new QueryRequest(params, SolrRequest.METHOD.POST);
  try {
    QueryResponse response = request.process(cloudSolrClient, collection);
    SolrDocumentList docs = response.getResults();
    documentIterator = docs.iterator();
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example 14
Source File: KnnStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void open() throws IOException {
  cloudSolrClient = cache.getCloudSolrClient(zkHost);
  ModifiableSolrParams params = getParams(this.props);

  StringBuilder builder = new StringBuilder();

  for(String key : mltParams) {
    if(params.get(key) != null) {
      builder.append(' ').append(key).append('=').append(params.get(key));
      params.remove(key);
    }
  }

  String k = params.get("k");

  if(k != null) {
    params.add(ROWS, k);
    params.remove(k);
  }

  params.add(Q, "{!mlt"+builder.toString()+"}"+id);

  QueryRequest request = new QueryRequest(params);
  try {
    QueryResponse response = request.process(cloudSolrClient, collection);
    SolrDocumentList docs = response.getResults();
    documentIterator = docs.iterator();
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example 15
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void rebalancePropUsingStandardRequest(String prop) throws IOException, SolrServerException {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("action", CollectionParams.CollectionAction.BALANCESHARDUNIQUE.toString());
  params.set("property", prop);

  params.set("collection", COLLECTION_NAME);
  if (prop.toLowerCase(Locale.ROOT).contains("preferredleader") == false) {
    params.set("shardUnique", true);
  }
  QueryRequest request = new QueryRequest(params);
  request.setPath("/admin/collections");
  QueryResponse resp = request.process(cluster.getSolrClient());
  assertEquals("Call to rebalanceLeaders failed ", 0, resp.getStatus());
}
 
Example 16
Source File: LBSolrClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void checkAZombieServer(ServerWrapper zombieServer) {
  try {
    QueryRequest queryRequest = new QueryRequest(solrQuery);
    queryRequest.setBasePath(zombieServer.baseUrl);
    QueryResponse resp = queryRequest.process(getClient(zombieServer.getBaseUrl()));
    if (resp.getStatus() == 0) {
      // server has come back up.
      // make sure to remove from zombies before adding to alive to avoid a race condition
      // where another thread could mark it down, move it back to zombie, and then we delete
      // from zombie and lose it forever.
      ServerWrapper wrapper = zombieServers.remove(zombieServer.getBaseUrl());
      if (wrapper != null) {
        wrapper.failedPings = 0;
        if (wrapper.standard) {
          addToAlive(wrapper);
        }
      } else {
        // something else already moved the server from zombie to alive
      }
    }
  } catch (Exception e) {
    //Expected. The server is still down.
    zombieServer.failedPings++;

    // If the server doesn't belong in the standard set belonging to this load balancer
    // then simply drop it after a certain number of failed pings.
    if (!zombieServer.standard && zombieServer.failedPings >= NONSTANDARD_PING_LIMIT) {
      zombieServers.remove(zombieServer.getBaseUrl());
    }
  }
}
 
Example 17
Source File: SolrClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private QueryResponse getQueryResponse(String collection, SolrParams params, ResponseParser parser) throws SolrServerException, IOException {
  QueryRequest req = new QueryRequest(params);
  if (parser instanceof StreamingBinaryResponseParser) {
    req.setStreamingResponseCallback(((StreamingBinaryResponseParser) parser).callback);
  }
  req.setResponseParser(parser);
  return req.process(this, collection);
}
 
Example 18
Source File: TestDocLevelOperations.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void checkDeleteById(QueryRequest request, CloudSolrServer server)
    throws Exception {
  QueryResponse rsp = request.process(server);
  long junitResults = rsp.getResults().getNumFound();
  assertEquals(0, junitResults);

  setAuthenticationUser("docLevel");
  rsp =  request.process(server);
  long docLevelResults = rsp.getResults().getNumFound();
  assertEquals(0, docLevelResults);
}
 
Example 19
Source File: SearchHandlerTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testRequireZkConnectedDistrib() throws Exception{
  MiniSolrCloudCluster miniCluster = new MiniSolrCloudCluster(2, createTempDir(), buildJettyConfig("/solr"));

  final CloudSolrClient cloudSolrClient = miniCluster.getSolrClient();

  try {
    assertNotNull(miniCluster.getZkServer());
    List<JettySolrRunner> jettys = miniCluster.getJettySolrRunners();
    assertEquals(2, jettys.size());
    for (JettySolrRunner jetty : jettys) {
      assertTrue(jetty.isRunning());
    }

    // create collection
    String collectionName = "testRequireZkConnectedDistribCollection";
    String configName = collectionName + "Config";
    miniCluster.uploadConfigSet(SolrTestCaseJ4.TEST_PATH().resolve("collection1/conf"), configName);

    CollectionAdminRequest.createCollection(collectionName, configName, 2, 1)
        .process(miniCluster.getSolrClient());

    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(ShardParams.SHARDS_TOLERANT, "requireZkConnected");
    QueryRequest req = new QueryRequest(params);
    QueryResponse rsp = req.process(cloudSolrClient, collectionName);
    assertTrue(rsp.getResponseHeader().getBooleanArg("zkConnected"));

    Collection<Slice> slices = cloudSolrClient.getZkStateReader().getClusterState().getCollection(collectionName).getSlices();
    Slice disconnectedSlice = getRandomEntry(slices);
    Replica disconnectedReplica = getRandomEntry(disconnectedSlice.getReplicas());

    // Query a coordinating replica that is connected to ZooKeeper
    Slice connectedSlice = getRandomEntry(slices);
    while (connectedSlice.getName().equals(disconnectedSlice.getName())) {
      connectedSlice = getRandomEntry(slices);
    }
    Replica connectedReplica = connectedSlice.getReplicas().iterator().next();
    try (HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(connectedReplica.getCoreUrl()).build()) {
      ignoreException("ZooKeeper is not connected");
      ignoreException("no servers hosting shard:");
      JettySolrRunner disconnectedJetty = miniCluster.getReplicaJetty(disconnectedReplica);
      disconnectedJetty.getCoreContainer().getZkController().getZkClient().close();
      req.process(httpSolrClient);
      fail("An exception should be thrown when ZooKeeper is not connected and shards.tolerant=requireZkConnected");
    } catch (Exception e) {
      assertTrue("Unrecognized exception message: " + e, 
          e.getMessage().contains("no servers hosting shard:") 
              || e.getMessage().contains("ZooKeeper is not connected"));
    }
  }
  finally {
    miniCluster.shutdown();
    unIgnoreException("no servers hosting shard:");
    unIgnoreException("ZooKeeper is not connected");
  }
}
 
Example 20
Source File: TextLogitStream.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public Tuple call() throws Exception {
  ModifiableSolrParams params = new ModifiableSolrParams();
  HttpSolrClient solrClient = cache.getHttpSolrClient(baseUrl);

  params.add(DISTRIB, "false");
  params.add("fq","{!tlogit}");
  params.add("feature", feature);
  params.add("terms", TextLogitStream.toString(terms));
  params.add("idfs", TextLogitStream.toString(idfs));

  for(Entry<String, String> entry : paramsMap.entrySet()) {
    params.add(entry.getKey(), entry.getValue());
  }

  if(weights != null) {
    params.add("weights", TextLogitStream.toString(weights));
  }

  params.add("iteration", Integer.toString(iteration));
  params.add("outcome", outcome);
  params.add("positiveLabel", Integer.toString(positiveLabel));
  params.add("threshold", Double.toString(threshold));
  params.add("alpha", Double.toString(learningRate));

  QueryRequest  request= new QueryRequest(params, SolrRequest.METHOD.POST);
  QueryResponse response = request.process(solrClient);
  @SuppressWarnings({"rawtypes"})
  NamedList res = response.getResponse();

  @SuppressWarnings({"rawtypes"})
  NamedList logit = (NamedList)res.get("logit");

  @SuppressWarnings({"unchecked"})
  List<Double> shardWeights = (List<Double>)logit.get("weights");
  double shardError = (double)logit.get("error");

  Tuple tuple = new Tuple();

  tuple.put("error", shardError);
  tuple.put("weights", shardWeights);
  tuple.put("evaluation", logit.get("evaluation"));

  return tuple;
}