Java Code Examples for org.apache.solr.common.SolrDocumentList#isEmpty()

The following examples show how to use org.apache.solr.common.SolrDocumentList#isEmpty() . 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: ManagerBase.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
protected SEARCH_RESPONSE getLogAsPaginationProvided(SolrQuery solrQuery, SolrDaoBase solrDaoBase, String event) {
  QueryResponse response = solrDaoBase.process(solrQuery, event);
  SEARCH_RESPONSE logResponse = createLogSearchResponse();
  SolrDocumentList docList = response.getResults();
  logResponse.setTotalCount(docList.getNumFound());
  List<LOG_DATA_TYPE> serviceLogDataList = convertToSolrBeans(response);
  if (!docList.isEmpty()) {
    logResponse.setLogList(serviceLogDataList);
    logResponse.setStartIndex((int) docList.getStart());
    Integer rowNumber = solrQuery.getRows();
    if (rowNumber == null) {
      logger.error("No RowNumber was set in solrQuery");
      return createLogSearchResponse();
    }
    logResponse.setPageSize(rowNumber);
  }
  return logResponse;
}
 
Example 2
Source File: MCROAISolrSearcher.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Optional<Header> getHeader(String mcrId) {
    SolrQuery query = getBaseQuery(CommonParams.FQ);
    query.set(CommonParams.Q, "id:" + MCRSolrUtils.escapeSearchValue(mcrId));
    query.setRows(1);
    // do the query
    SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
    try {
        QueryResponse response = solrClient.query(query);
        SolrDocumentList results = response.getResults();
        if (!results.isEmpty()) {
            return Optional.of(toHeader(results.get(0), getSetResolver(results)));
        }
    } catch (Exception exc) {
        LOGGER.error("Unable to handle solr request", exc);
    }
    return Optional.empty();
}
 
Example 3
Source File: DistributedVersionInfoTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected long getVersionFromIndex(Replica replica, String docId) throws IOException, SolrServerException {
  Long vers = null;
  String queryStr = (docId != null) ? "id:" + docId : "_version_:[0 TO *]";
  SolrQuery query = new SolrQuery(queryStr);
  query.setRows(1);
  query.setFields("id", "_version_");
  query.addSort(new SolrQuery.SortClause("_version_", SolrQuery.ORDER.desc));
  query.setParam("distrib", false);

  try (SolrClient client = getHttpSolrClient(replica.getCoreUrl())) {
    QueryResponse qr = client.query(query);
    SolrDocumentList hits = qr.getResults();
    if (hits.isEmpty())
      fail("No results returned from query: "+query);

    vers = (Long) hits.get(0).getFirstValue("_version_");
  }

  if (vers == null)
    fail("Failed to get version using query " + query + " from " + replica.getCoreUrl());

  return vers.longValue();
}
 
Example 4
Source File: ServiceLogsManager.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private Date getDocDateFromNextOrLastPage(ServiceLogRequest request, String keyword, boolean isNext, int currentPageNumber, int maxRows) {
  int lastOrFirstLogIndex;
  if (isNext) {
    lastOrFirstLogIndex = ((currentPageNumber + 1) * maxRows);
  } else {
    if (currentPageNumber == 0) {
      throw new NotFoundException("This is the first Page");
    }
    lastOrFirstLogIndex = (currentPageNumber * maxRows) - 1;
  }
  SimpleQuery sq = conversionService.convert(request, SimpleQuery.class);
  SolrQuery nextPageLogTimeQuery = new DefaultQueryParser().doConstructSolrQuery(sq);
  nextPageLogTimeQuery.remove("start");
  nextPageLogTimeQuery.remove("rows");
  nextPageLogTimeQuery.setStart(lastOrFirstLogIndex);
  nextPageLogTimeQuery.setRows(1);

  QueryResponse queryResponse = serviceLogsSolrDao.process(nextPageLogTimeQuery);
  if (queryResponse == null) {
    throw new MalformedInputException(String.format("Cannot process next page query for \"%s\" ", keyword));
  }
  SolrDocumentList docList = queryResponse.getResults();
  if (docList == null || docList.isEmpty()) {
    throw new MalformedInputException(String.format("Next page element for \"%s\" is not found", keyword));
  }

  SolrDocument solrDoc = docList.get(0);
  return (Date) solrDoc.get(LOGTIME);
}
 
Example 5
Source File: ServiceLogsManager.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private <T extends LogData> GroupListResponse getFields(String field, String clusters, Class<T> clazz) {
  SolrQuery solrQuery = new SolrQuery();
  solrQuery.setQuery("*:*");
  SolrUtil.addListFilterToSolrQuery(solrQuery, CLUSTER, clusters);
  GroupListResponse collection = new GroupListResponse();
  SolrUtil.setFacetField(solrQuery, field);
  SolrUtil.setFacetSort(solrQuery, LogSearchConstants.FACET_INDEX);
  QueryResponse response = serviceLogsSolrDao.process(solrQuery);
  if (response == null) {
    return collection;
  }
  FacetField facetField = response.getFacetField(field);
  if (facetField == null) {
    return collection;
  }
  List<Count> fieldList = facetField.getValues();
  if (fieldList == null) {
    return collection;
  }
  SolrDocumentList docList = response.getResults();
  if (docList == null) {
    return collection;
  }
  List<LogData> groupList = new ArrayList<>(getLogDataListByFieldType(clazz, response, fieldList));

  collection.setGroupList(groupList);
  if (!docList.isEmpty()) {
    collection.setStartIndex((int) docList.getStart());
    collection.setTotalCount(docList.getNumFound());
  }
  return collection;
}
 
Example 6
Source File: LogLevelFilterManagerSolr.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LogLevelFilterMap getLogLevelFilters(String clusterName) {
  LogLevelFilterMap logLevelFilterMap = new LogLevelFilterMap();
  TreeMap<String, LogLevelFilter> logLevelFilterTreeMap = new TreeMap<>();
  try {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("*:*");
    if (useClusterParam) {
      solrQuery.addFilterQuery("cluster_string:" + clusterName);
    }
    solrQuery.addFilterQuery("type:log_level_filter");
    solrQuery.setFields("value", "name");

    final QueryResponse response = solrClient.query(solrQuery);
    if (response != null) {
      final SolrDocumentList documents = response.getResults();
      if (documents != null && !documents.isEmpty()) {
        for(SolrDocument document : documents) {
          String jsons = (String) document.getFieldValue("value");
          String logId = (String) document.getFieldValue("name");
          if (jsons != null) {
            LogLevelFilter logLevelFilter = gson.fromJson(jsons, LogLevelFilter.class);
            logLevelFilterTreeMap.put(logId,logLevelFilter);
          }
        }
      }
    }
  } catch (Exception e) {
    logger.error("Error during getting log level filters: {}", e.getMessage());
  }
  logLevelFilterMap.setFilter(logLevelFilterTreeMap);
  return logLevelFilterMap;
}
 
Example 7
Source File: SolrRrdBackendFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
SolrRrdBackend.SyncData getData(String path) throws IOException {
  if (!persistent) {
    return null;
  }
  try {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add(CommonParams.Q, "{!term f=id}" + ID_PREFIX + ID_SEP + path);
    params.add(CommonParams.FQ, CommonParams.TYPE + ":" + DOC_TYPE);
    QueryResponse rsp = solrClient.query(collection, params);
    SolrDocumentList docs = rsp.getResults();
    if (docs == null || docs.isEmpty()) {
      return null;
    }
    if (docs.size() > 1) {
      throw new SolrServerException("Expected at most 1 doc with id '" + path + "' but got " + docs);
    }
    SolrDocument doc = docs.get(0);
    Object o = doc.getFieldValue(DATA_FIELD);
    if (o == null) {
      return null;
    }
    if (o instanceof byte[]) {
      Object timeObj = doc.getFieldValue("timestamp_l");
      Long time = timeObj instanceof Number ? ((Number)timeObj).longValue() : Long.parseLong(String.valueOf(timeObj));
      return new SolrRrdBackend.SyncData((byte[])o, time);
    } else {
      throw new SolrServerException("Unexpected value of '" + DATA_FIELD + "' field: " + o.getClass().getName() + ": " + o);
    }
  } catch (SolrServerException e) {
    throw new IOException(e);
  }
}
 
Example 8
Source File: SolrRrdBackendFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Check for existence of a backend.
 * @param path backend path, without the URI scheme
 * @return true when a backend exists. Note that a backend may exist only
 * in memory if it was created recently within {@link #syncPeriod}.
 * @throws IOException on Solr exception
 */
@Override
public boolean exists(String path) throws IOException {
  // check in-memory backends first
  if (backends.containsKey(path)) {
    return true;
  }
  if (!persistent) {
    return false;
  }
  try {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add(CommonParams.Q, "{!term f=id}" + ID_PREFIX + ID_SEP + path);
    params.add(CommonParams.FQ, CommonParams.TYPE + ":" + DOC_TYPE);
    params.add(CommonParams.FL, "id");
    QueryResponse rsp = solrClient.query(collection, params);
    SolrDocumentList docs = rsp.getResults();
    if (docs == null || docs.isEmpty()) {
      return false;
    }
    if (docs.size() > 1) {
      throw new SolrServerException("Expected at most 1 doc with id '" + path + "' but got " + docs);
    }
    return true;
  } catch (SolrServerException e) {
    throw new IOException(e);
  }
}
 
Example 9
Source File: SolrClient.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves the SolrDocument associated with the given identifier and uses
 * the SolrParams to execute the request.
 *
 * @param collection the Solr collection to query
 * @param id the id
 * @param params additional parameters to add to the query
 *
 * @return retrieved SolrDocument, or null if no document is found.
 *
 * @throws IOException If there is a low-level I/O error.
 * @throws SolrServerException if there is an error on the server
 */
public SolrDocument getById(String collection, String id, SolrParams params) throws SolrServerException, IOException {
  SolrDocumentList docs = getById(collection, Collections.singletonList(id), params);
  if (!docs.isEmpty()) {
    return docs.get(0);
  }
  return null;
}