Example #1
0
  public FieldStatsResult fieldStats(String field, String query, String filter, TimeRange range)
      throws FieldTypeException, IndexHelper.InvalidRangeFormatException {
    SearchRequestBuilder srb;

    if (filter == null) {
      srb = standardSearchRequest(query, IndexHelper.determineAffectedIndices(server, range));
    } else {
      srb =
          filteredSearchRequest(query, filter, IndexHelper.determineAffectedIndices(server, range));
    }

    StatisticalFacetBuilder stats = new StatisticalFacetBuilder(STATS_FACET_NAME);
    stats.global(false);

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

    stats.field(field);

    srb.addFacet(stats);

    SearchResponse r;
    final SearchRequest request;
    try {
      request = srb.request();
      r = c.search(request).actionGet();
    } catch (org.elasticsearch.action.search.SearchPhaseExecutionException e) {
      throw new FieldTypeException(e);
    }

    return new FieldStatsResult(
        (StatisticalFacet) r.getFacets().facet(STATS_FACET_NAME),
        query,
        request.source(),
        r.getTook());
  }
Example #2
0
  public TermsResult terms(String field, int size, String query, String filter, TimeRange range)
      throws IndexHelper.InvalidRangeFormatException {
    if (size == 0) {
      size = 50;
    }

    SearchRequestBuilder srb;
    if (filter == null) {
      srb = standardSearchRequest(query, IndexHelper.determineAffectedIndices(server, range));
    } else {
      srb =
          filteredSearchRequest(query, filter, IndexHelper.determineAffectedIndices(server, range));
    }

    TermsFacetBuilder terms = new TermsFacetBuilder(TERMS_FACET_NAME);
    terms.global(false);
    terms.field(field);
    terms.size(size);

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

    srb.addFacet(terms);

    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();

    return new TermsResult(
        (TermsFacet) r.getFacets().facet(TERMS_FACET_NAME), query, request.source(), r.getTook());
  }
Example #3
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 #4
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());
  }
Example #5
0
  public CountResult count(String query, TimeRange range, String filter)
      throws IndexHelper.InvalidRangeFormatException {
    Set<String> indices = IndexHelper.determineAffectedIndices(server, range);

    SearchRequest request;
    if (filter == null) {
      request = standardSearchRequest(query, indices, range).request();
    } else {
      request = filteredSearchRequest(query, filter, indices, range).request();
    }
    request.searchType(SearchType.COUNT);

    SearchResponse r = c.search(request).actionGet();
    return new CountResult(r.getHits().getTotalHits(), r.getTookInMillis());
  }
Example #6
0
  public SearchResult search(
      String query, String filter, TimeRange range, int limit, int offset, Sorting sorting)
      throws IndexHelper.InvalidRangeFormatException {
    if (limit <= 0) {
      limit = LIMIT;
    }

    Set<String> indices = IndexHelper.determineAffectedIndices(server, range);

    SearchRequest request;

    if (filter == null) {
      request = standardSearchRequest(query, indices, limit, offset, range, sorting).request();
    } else {
      request =
          filteredSearchRequest(query, filter, indices, limit, offset, range, sorting).request();
    }

    SearchResponse r = c.search(request).actionGet();
    return new SearchResult(r.getHits(), indices, query, request.source(), r.getTook());
  }
Example #7
0
  private BoolFilterBuilder standardFilters(TimeRange range, String filter)
      throws IndexHelper.InvalidRangeFormatException {
    BoolFilterBuilder bfb = FilterBuilders.boolFilter();

    boolean set = false;

    if (range != null) {
      bfb.must(IndexHelper.getTimestampRangeFilter(range));
      set = true;
    }

    if (filter != null && !filter.isEmpty() && !filter.equals("*")) {
      bfb.must(FilterBuilders.queryFilter(QueryBuilders.queryString(filter)));
      set = true;
    }

    if (!set) {
      throw new RuntimeException("Either range or filter must be set.");
    }

    return bfb;
  }