private CacheQuery createRequestLogsQuery(CapedwarfLogQuery logQuery) {
    LogQuery query = logQuery.getQuery();
    QueryBuilder queryBuilder =
        searchManager.buildQueryBuilderForClass(CapedwarfRequestLogs.class).get();
    List<Query> queries = new ArrayList<Query>();
    if (query.getStartTimeUsec() != null) {
      queries.add(
          queryBuilder
              .range()
              .onField(CapedwarfRequestLogs.END_TIME_USEC)
              .above(query.getStartTimeUsec())
              .createQuery());
    }
    if (query.getEndTimeUsec() != null) {
      queries.add(
          queryBuilder
              .range()
              .onField(CapedwarfRequestLogs.END_TIME_USEC)
              .below(query.getEndTimeUsec())
              .createQuery());
    }
    if (query.getMinLogLevel() != null) {
      queries.add(
          queryBuilder
              .range()
              .onField(CapedwarfRequestLogs.MAX_LOG_LEVEL)
              .above(query.getMinLogLevel().ordinal())
              .createQuery());
    }

    boolean onlyCompleteRequests = !Boolean.TRUE.equals(query.getIncludeIncomplete());
    if (onlyCompleteRequests) {
      queries.add(
          queryBuilder
              .keyword()
              .onField(CapedwarfRequestLogs.FINISHED)
              .matching(Boolean.TRUE)
              .createQuery());
    }

    Query luceneQuery = getQuery(queryBuilder, queries);
    CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, CapedwarfRequestLogs.class);
    cacheQuery.sort(
        new Sort(new SortField(CapedwarfRequestLogs.END_TIME_USEC, SortField.LONG, true)));
    cacheQuery.firstResult(logQuery.getOptions().getOffset());
    if (logQuery.getOptions().getLimit() != null) {
      cacheQuery.maxResults(logQuery.getOptions().getLimit());
    }
    return cacheQuery;
  }
Example #2
0
  /** Execute Lucene index query. */
  private QueryResponse executeQuery(
      AdvancedCache<byte[], byte[]> cache, SerializationContext serCtx, QueryRequest request) {
    final SearchManager searchManager = Search.getSearchManager(cache);
    final SearchIntegrator searchFactory = searchManager.getSearchFactory();
    final QueryCache queryCache = ComponentRegistryUtils.getQueryCache(cache); // optional component

    LuceneQueryParsingResult parsingResult;
    Query luceneQuery;

    if (queryCache != null) {
      KeyValuePair<String, Class> queryCacheKey =
          new KeyValuePair<String, Class>(request.getJpqlString(), LuceneQueryParsingResult.class);
      parsingResult = queryCache.get(queryCacheKey);
      if (parsingResult == null) {
        parsingResult = parseQuery(cache, serCtx, request.getJpqlString(), searchFactory);
        queryCache.put(queryCacheKey, parsingResult);
      }
    } else {
      parsingResult = parseQuery(cache, serCtx, request.getJpqlString(), searchFactory);
    }

    luceneQuery = parsingResult.getQuery();

    if (!cache.getCacheConfiguration().compatibility().enabled()) {
      // restrict on entity type
      QueryBuilder qb =
          searchFactory.buildQueryBuilder().forEntity(parsingResult.getTargetEntity()).get();
      luceneQuery =
          qb.bool()
              .must(
                  qb.keyword()
                      .onField(TYPE_FIELD_NAME)
                      .ignoreFieldBridge()
                      .ignoreAnalyzer()
                      .matching(parsingResult.getTargetEntityName())
                      .createQuery())
              .must(luceneQuery)
              .createQuery();
    }

    CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, parsingResult.getTargetEntity());

    if (parsingResult.getSort() != null) {
      cacheQuery = cacheQuery.sort(parsingResult.getSort());
    }

    int projSize = 0;
    if (parsingResult.getProjections() != null && !parsingResult.getProjections().isEmpty()) {
      projSize = parsingResult.getProjections().size();
      cacheQuery =
          cacheQuery.projection(parsingResult.getProjections().toArray(new String[projSize]));
    }
    if (request.getStartOffset() > 0) {
      cacheQuery = cacheQuery.firstResult((int) request.getStartOffset());
    }
    if (request.getMaxResults() > 0) {
      cacheQuery = cacheQuery.maxResults(request.getMaxResults());
    }

    List<?> list = cacheQuery.list();
    List<WrappedMessage> results =
        new ArrayList<WrappedMessage>(projSize == 0 ? list.size() : list.size() * projSize);
    for (Object o : list) {
      if (projSize == 0) {
        results.add(new WrappedMessage(o));
      } else {
        Object[] row = (Object[]) o;
        for (int j = 0; j < projSize; j++) {
          results.add(new WrappedMessage(row[j]));
        }
      }
    }

    QueryResponse response = new QueryResponse();
    response.setTotalResults(cacheQuery.getResultSize());
    response.setNumResults(list.size());
    response.setProjectionSize(projSize);
    response.setResults(results);

    return response;
  }