Example #1
0
  private SearchRequestBuilder standardSearchRequest(
      String query, Set<String> indices, int limit, int offset, TimeRange range, Sorting sort)
      throws IndexHelper.InvalidRangeFormatException {
    if (query == null || query.trim().isEmpty()) {
      query = "*";
    }

    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(indices.toArray(new String[] {}));

    if (query.trim().equals("*")) {
      srb.setQuery(matchAllQuery());
    } else {
      QueryStringQueryBuilder qs = queryString(query);
      qs.allowLeadingWildcard(server.getConfiguration().isAllowLeadingWildcardSearches());
      srb.setQuery(qs);
    }

    srb.setFrom(offset);

    if (limit > 0) {
      srb.setSize(limit);
    }

    if (range != null) {
      srb.setFilter(IndexHelper.getTimestampRangeFilter(range));
    }

    if (sort != null) {
      srb.addSort(sort.getField(), sort.asElastic());
    }

    return srb;
  }
Example #2
0
  public HistogramResult histogram(
      String query, Indexer.DateHistogramInterval interval, String filter, TimeRange range)
      throws IndexHelper.InvalidRangeFormatException {
    DateHistogramFacetBuilder fb =
        FacetBuilders.dateHistogramFacet("histogram")
            .field("timestamp")
            .interval(interval.toString().toLowerCase());

    fb.facetFilter(standardFilters(range, filter));

    QueryStringQueryBuilder qs = queryString(query);
    qs.allowLeadingWildcard(server.getConfiguration().isAllowLeadingWildcardSearches());

    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(IndexHelper.determineAffectedIndices(server, range).toArray(new String[] {}));
    srb.setQuery(qs);
    srb.addFacet(fb);

    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    return new DateHistogramResult(
        (DateHistogramFacet) r.getFacets().facet("histogram"),
        query,
        request.source(),
        interval,
        r.getTook());
  }