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

The following examples show how to use org.apache.solr.client.solrj.SolrClient#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: UsingSolrJRefGuideExamplesTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void queryBeanValueTypeExample() throws Exception {
  expectLine("Found 3 documents");
  expectLine("id: 1; name: Fitbit Alta");
  expectLine("id: 2; name: Sony Walkman");
  expectLine("id: 3; name: Garmin GPS");
  
  // tag::solrj-query-bean-value-type[]
  final SolrClient client = getSolrClient();

  final SolrQuery query = new SolrQuery("*:*");
  query.addField("id");
  query.addField("name");
  query.setSort("id", ORDER.asc);

  final QueryResponse response = client.query("techproducts", query);
  final List<TechProduct> products = response.getBeans(TechProduct.class);
  // end::solrj-query-bean-value-type[]

  print("Found " + products.size() + " documents");
  for (TechProduct product : products) {
    print("id: " + product.id + "; name: " + product.name);
  }
}
 
Example 2
Source File: TestTolerantUpdateProcessorRandomCloud.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** uses a Cursor to iterate over every doc in the index, recording the 'id_i' value in a BitSet */
private static final BitSet allDocs(final SolrClient c, final int maxDocIdExpected) throws Exception {
  BitSet docs = new BitSet(maxDocIdExpected+1);
  String cursorMark = CURSOR_MARK_START;
  int docsOnThisPage = Integer.MAX_VALUE;
  while (0 < docsOnThisPage) {
    final SolrParams p = params("q","*:*",
                                "rows","100",
                                // note: not numeric, but we don't actual care about the order
                                "sort", "id asc",
                                CURSOR_MARK_PARAM, cursorMark);
    QueryResponse rsp = c.query(p);
    cursorMark = rsp.getNextCursorMark();
    docsOnThisPage = 0;
    for (SolrDocument doc : rsp.getResults()) {
      docsOnThisPage++;
      int id_i = ((Integer)doc.get("id_i")).intValue();
      assertTrue("found id_i bigger then expected "+maxDocIdExpected+": " + id_i,
                 id_i <= maxDocIdExpected);
      docs.set(id_i);
    }
    cursorMark = rsp.getNextCursorMark();
  }
  return docs;
}
 
Example 3
Source File: MCRRestAPIClassifications.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private void filterNonEmpty(String classId, Element e) {
    SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
    Element[] categories = e.getChildren("category").toArray(Element[]::new);
    for (Element cat : categories) {
        SolrQuery solrQquery = new SolrQuery();
        solrQquery.setQuery(
            "category:\"" + MCRSolrUtils.escapeSearchValue(classId + ":" + cat.getAttributeValue("ID")) + "\"");
        solrQquery.setRows(0);
        try {
            QueryResponse response = solrClient.query(solrQquery);
            SolrDocumentList solrResults = response.getResults();
            if (solrResults.getNumFound() == 0) {
                cat.detach();
            } else {
                filterNonEmpty(classId, cat);
            }
        } catch (SolrServerException | IOException exc) {
            LOGGER.error(exc);
        }

    }
}
 
Example 4
Source File: TestReplicationHandlerDiskOverFlow.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private long indexDocs(SolrClient client, int totalDocs, int start) throws Exception {
  for (int i = 0; i < totalDocs; i++)
    TestReplicationHandler.index(client, "id", i + start, "name", TestUtil.randomSimpleString(random(), 1000, 5000));
  client.commit(true, true);
  QueryResponse response = client.query(new SolrQuery()
      .add("qt", "/replication")
      .add("command", "filelist")
      .add("generation", "-1"));

  long totalSize = 0;
  for (Map map : (List<Map>) response.getResponse().get(CMD_GET_FILE_LIST)) {
    Long sz = (Long) map.get(ReplicationHandler.SIZE);
    totalSize += sz;
  }
  return totalSize;
}
 
Example 5
Source File: ShardRoutingTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void doAtomicUpdate() throws Exception {
  log.info("### STARTING doAtomicUpdate");
  int nClients = clients.size();
  assertEquals(8, nClients);

  int expectedVal = 0;
  for (SolrClient client : clients) {
    client.add(sdoc("id", "b!doc", "foo_i", map("inc",1)));
    expectedVal++;

    QueryResponse rsp = client.query(params("qt","/get", "id","b!doc"));
    Object val = ((Map)rsp.getResponse().get("doc")).get("foo_i");
    assertEquals((Integer)expectedVal, val);
  }

}
 
Example 6
Source File: TestShortCircuitedRequests.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
@ShardsFixed(num = 4)
public void test() throws Exception {
  waitForRecoveriesToFinish(false);
  assertEquals(4, cloudClient.getZkStateReader().getClusterState().getCollection(DEFAULT_COLLECTION).getSlices().size());
  index("id", "a!doc1");  // shard3
  index("id", "b!doc1");  // shard1
  index("id", "c!doc1");  // shard2
  index("id", "e!doc1");  // shard4
  commit();

  doQuery("a!doc1", "q", "*:*", ShardParams._ROUTE_, "a!"); // can go to any random node

  // query shard3 directly with _route_=a! so that we trigger the short circuited request path
  Replica shard3 = cloudClient.getZkStateReader().getClusterState().getCollection(DEFAULT_COLLECTION).getLeader("shard3");
  String nodeName = shard3.getNodeName();
  SolrClient shard3Client = getClient(nodeName);
  QueryResponse response = shard3Client.query(new SolrQuery("*:*").add(ShardParams._ROUTE_, "a!").add(ShardParams.SHARDS_INFO, "true"));

  assertEquals("Could not find doc", 1, response.getResults().getNumFound());
  NamedList<?> sinfo = (NamedList<?>) response.getResponse().get(ShardParams.SHARDS_INFO);
  assertNotNull("missing shard info for short circuited request", sinfo);
}
 
Example 7
Source File: UsingSolrJRefGuideExamplesTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void queryWithSolrQueryExample() throws Exception {
  final int numResultsToReturn = 3;
  expectLine("Found 3 documents");
  expectLine("id: 1; name: Fitbit Alta");
  expectLine("id: 2; name: Sony Walkman");
  expectLine("id: 3; name: Garmin GPS");
  final SolrClient client = getSolrClient();

  // tag::solrj-query-with-solrquery[]
  final SolrQuery query = new SolrQuery("*:*");
  query.addField("id");
  query.addField("name");
  query.setSort("id", ORDER.asc);
  query.setRows(numResultsToReturn);
  // end::solrj-query-with-solrquery[]

  final QueryResponse response = client.query("techproducts", query);
  final SolrDocumentList documents = response.getResults();

  print("Found " + documents.getNumFound() + " documents");
  assertEquals(numResultsToReturn, documents.size());
  for(SolrDocument document : documents) {
    final String id = (String) document.getFirstValue("id");
    final String name = (String) document.getFirstValue("name");
    
    print("id: "+ id + "; name: " + name);
  }
}
 
Example 8
Source File: AbstractSolrSearchService.java    From aem-solr-search with Apache License 2.0 5 votes vote down vote up
/**
 * Query a particular instance of SolrServer identified by the core name
 * with a given query.
 */
public QueryResponse query(String solrCore, SolrQuery solrQuery) throws SolrServerException {
    SolrClient server = getSolrQueryClient();
    LOG.info("Quering {} with '{}'", getSolrServerURI(solrCore), solrQuery);
    QueryResponse solrResponse;
    try {
        solrResponse = server.query(solrCore, solrQuery);
        return solrResponse;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new QueryResponse();
}
 
Example 9
Source File: RetryingSolrServerTest.java    From kite with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoRetries() throws Exception {
  SolrQuery query = getDefaultQuery();
  solrServer.query(query);
  FailingSolrServer failingSolrServer = new FailingSolrServer(solrServer);
  SolrClient solr = new RetryingSolrServer(failingSolrServer, getNoRetryPolicyFactory(), getMetricsFacade());
  try {
    solr.query(query);
    fail();
  } catch (RetriesExhaustedException e) {
    assertTrue(e.getCause() instanceof FailingSolrServer.InjectedSolrServerException);
    Assert.assertEquals(1, failingSolrServer.getNumRequests());
    Assert.assertEquals(1, failingSolrServer.getNumInjectedFailures());
  }    
}
 
Example 10
Source File: AbstractLogSearchSteps.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
protected void waitUntilSolrHasAnyData() throws InterruptedException {
  boolean solrHasData = false;
  int maxTries = 60;
  String lastExceptionMessage = null;

  for (int tries = 1; tries < maxTries; tries++) {
    try {
      SolrClient solrClient = StoryDataRegistry.INSTANCE.getSolrClient();
      SolrQuery solrQuery = new SolrQuery();
      solrQuery.setQuery("*:*");
      QueryResponse queryResponse = solrClient.query(solrQuery);
      SolrDocumentList list = queryResponse.getResults();
      if (list.size() > 0) {
        solrHasData = true;
        break;
      } else {
        Thread.sleep(2000);
        LOG.info("Solr has no data yet. Retrying... ({} tries)", tries);
      }
    } catch (Exception e) {
      LOG.info("Error occurred during checking solr. Retrying... ({} tries)", tries);
      lastExceptionMessage = e.getMessage();
      Thread.sleep(2000);
    }
  }
  if (!solrHasData) {
    throw new IllegalStateException(String.format("Solr has no data after %d tries. Exception: %s", maxTries, lastExceptionMessage));
  }
}
 
Example 11
Source File: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static int assertNodesPerShardGreaterThan(int count, boolean ignoreZero) throws Exception
{
    int shardHit = 0;
    List<SolrClient> clients = getShardedClients();
    SolrQuery query = luceneToSolrQuery(new TermQuery(new Term(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_NODE)));
    StringBuilder error = new StringBuilder();
    for (SolrClient client : clients)
    {
        QueryResponse response = client.query(query);
        int totalHits = (int) response.getResults().getNumFound();


        if (totalHits > 0)
        {
            shardHit++;
        }

        if (totalHits < count)
        {
            if (ignoreZero && totalHits == 0) {
                log.info(client + ": have zero hits ");
            } else {
                error.append(" " + client + ": ");
                error.append("Expected nodes per shard greater than " + count + " found " + totalHits + " : " + query.toString());
            }
        }
        log.info(client + ": Hits " + totalHits);

    }

    if (error.length() > 0)
    {
        throw new Exception(error.toString());
    }
    return shardHit;
}
 
Example 12
Source File: AutoscalingHistoryHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private SolrDocumentList queryAndAssertDocs(ModifiableSolrParams query, SolrClient client, int expected) throws Exception {
  QueryResponse rsp = client.query(query);
  SolrDocumentList docs = rsp.getResults();
  if (docs.size() != expected) {
    log.info("History query: {}", query);
    log.info("Wrong response: {}", rsp);
    ModifiableSolrParams fullQuery = params(CommonParams.QT, CommonParams.AUTOSCALING_HISTORY_PATH);
    if (log.isInfoEnabled()) {
      log.info("Full response: {}", client.query(fullQuery));
    }
  }
  assertEquals("Wrong number of documents", expected, docs.size());
  return docs;
}
 
Example 13
Source File: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void assertShardSequence(int shard, SolrQuery query, int count) throws Exception
{
    List<SolrClient> clients = getShardedClients();
    int totalCount;
    SolrClient client = clients.get(shard);

    QueryResponse response = client.query(query);
    totalCount = (int) response.getResults().getNumFound();

    if(totalCount != count) {
        throw new Exception(totalCount+" docs found for query: "+query.toString()+" expecting "+count);
    }
}
 
Example 14
Source File: SolrDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new SearchResult.
 * @param client Solr client instance.
 * @param query to perform.
 * @throws SolrServerException Solr client exception.
 * @throws IOException network exception.
 */
public IterableSearchResult(SolrClient client, SolrQuery query)
      throws SolrServerException, IOException
{
   Objects.requireNonNull(client);
   Objects.requireNonNull(query);

   this.client = client;
   this.query  = query;

   this.query.setRows(FETCH_SIZE);

   rsp = client.query(this.query, SolrRequest.METHOD.POST);
}
 
Example 15
Source File: BackupRestoreUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void verifyDocs(int nDocs, SolrClient masterClient, String collectionName) throws SolrServerException, IOException {
  ModifiableSolrParams queryParams = new ModifiableSolrParams();
  queryParams.set("q", "*:*");
  QueryResponse response = masterClient.query(collectionName, queryParams);

  assertEquals(0, response.getStatus());
  assertEquals(nDocs, response.getResults().getNumFound());
}
 
Example 16
Source File: BaseDistributedSearchTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected QueryResponse queryServer(ModifiableSolrParams params) throws SolrServerException, IOException {
  // query a random server
  int which = r.nextInt(clients.size());
  SolrClient client = clients.get(which);
  QueryResponse rsp = client.query(params);
  return rsp;
}
 
Example 17
Source File: SolrAssociativeDatasetContainer.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Set<Tuple> getTupleOfValues(List<String> columnNames) throws ClassNotFoundException, NamingException, SQLException, DataBaseException, IOException, SolrServerException {
	Assert.assertTrue(columnNames.size() == 1, "Apache Solr cannot manage distinct values as tuple");
	String columnName = columnNames.get(0);

	SolrDataSet solrDataSet = dataSet.getImplementation(SolrDataSet.class);
	SolrQuery query = getSolrQuery(solrDataSet, columnName);
	SolrClient client = getSolrClient(solrDataSet.getSolrUrl());

	QueryResponse response = client.query(solrDataSet.getSolrCollection(), query);
	return AssociativeLogicUtils.getTupleOfValues(response.getFacetField(columnName));
}
 
Example 18
Source File: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns the QueryResponse from {@link #queryRandomShard}
 */
protected QueryResponse query(SolrClient solrClient, boolean setDistribParams, SolrParams p) throws Exception
{
    Random r = SOLR_RANDOM_SUPPLIER.getRandomGenerator();
    final ModifiableSolrParams params = new ModifiableSolrParams(p);

    // TODO: look into why passing true causes fails
    params.set("distrib", "false");
    final QueryResponse controlRsp = solrClient.query(params);
    SOLR_RESPONSE_COMPARATOR.validateResponse(controlRsp);

    params.remove("distrib");
    if (setDistribParams)
        setDistributedParams(params);

    QueryResponse rsp = queryRandomShard(params);

    SOLR_RESPONSE_COMPARATOR.compareResponses(rsp, controlRsp);

    if (stress > 0)
    {
        log.info("starting stress...");
        Thread[] threads = new Thread[nThreads];
        for (int i = 0; i < threads.length; i++)
        {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < stress; j++)
                {
                    int which = r.nextInt(clientShards.size());
                    SolrClient client = clientShards.get(which);
                    try
                    {
                        QueryResponse rsp1 = client.query(new ModifiableSolrParams(params));
                        if (verifyStress)
                        {
                            SOLR_RESPONSE_COMPARATOR.compareResponses(rsp1, controlRsp);
                        }
                    } catch (SolrServerException | IOException e)
                    {
                        throw new RuntimeException(e);
                    }
                }
            });
            threads[i].start();
        }

        for (Thread thread : threads)
        {
            thread.join();
        }
    }
    return rsp;
}
 
Example 19
Source File: DistributedDebugComponentTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void verifyDebugSections(SolrQuery query, SolrClient client) throws SolrServerException, IOException {
  query.set("debugQuery", "true");
  query.remove("debug");
  QueryResponse response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertInDebug(response, "track");
  assertInDebug(response, "rawquerystring");
  assertInDebug(response, "querystring");
  assertInDebug(response, "parsedquery");
  assertInDebug(response, "parsedquery_toString");
  assertInDebug(response, "QParser");
  assertInDebug(response, "explain");
  assertInDebug(response, "timing");
  
  query.set("debug", "true");
  query.remove("debugQuery");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertInDebug(response, "track");
  assertInDebug(response, "rawquerystring");
  assertInDebug(response, "querystring");
  assertInDebug(response, "parsedquery");
  assertInDebug(response, "parsedquery_toString");
  assertInDebug(response, "QParser");
  assertInDebug(response, "explain");
  assertInDebug(response, "timing");
  
  query.set("debug", "track");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertInDebug(response, "track");
  assertNotInDebug(response, "rawquerystring");
  assertNotInDebug(response, "querystring");
  assertNotInDebug(response, "parsedquery");
  assertNotInDebug(response, "parsedquery_toString");
  assertNotInDebug(response, "QParser");
  assertNotInDebug(response, "explain");
  assertNotInDebug(response, "timing");
  
  query.set("debug", "query");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertNotInDebug(response, "track");
  assertInDebug(response, "rawquerystring");
  assertInDebug(response, "querystring");
  assertInDebug(response, "parsedquery");
  assertInDebug(response, "parsedquery_toString");
  assertInDebug(response, "QParser");
  assertNotInDebug(response, "explain");
  assertNotInDebug(response, "timing");
  
  query.set("debug", "results");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertNotInDebug(response, "track");
  assertNotInDebug(response, "rawquerystring");
  assertNotInDebug(response, "querystring");
  assertNotInDebug(response, "parsedquery");
  assertNotInDebug(response, "parsedquery_toString");
  assertNotInDebug(response, "QParser");
  assertInDebug(response, "explain");
  assertNotInDebug(response, "timing");
  
  query.set("debug", "timing");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertNotInDebug(response, "track");
  assertNotInDebug(response, "rawquerystring");
  assertNotInDebug(response, "querystring");
  assertNotInDebug(response, "parsedquery");
  assertNotInDebug(response, "parsedquery_toString");
  assertNotInDebug(response, "QParser");
  assertNotInDebug(response, "explain");
  assertInDebug(response, "timing");
  
  query.set("debug", "false");
  response = client.query(query);
  assertNull(response.getDebugMap());
}
 
Example 20
Source File: MCRSolrSearchUtils.java    From mycore with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Returns the first document.
 * 
 * @param solrClient solr server connection
 * @param query solr query
 * @return first solr document or null
 * @throws SolrServerException communication with the solr server failed in any way
 */
public static SolrDocument first(SolrClient solrClient, String query) throws SolrServerException, IOException {
    ModifiableSolrParams p = new ModifiableSolrParams();
    p.set("q", query);
    p.set("rows", 1);
    QueryResponse response = solrClient.query(p);
    return response.getResults().isEmpty() ? null : response.getResults().get(0);
}