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()); }
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()); }
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()); }
public static SearchRequest randomSearch(Client client, String[] indices) { final SearchRequestBuilder builder = new SearchRequestBuilder(client); builder.setIndices(indices); builder.setTypes(INDEX_TYPE); builder.setQuery(randomFrom(TestQueryType.values()).getQuery()); return builder.request(); }
private SearchHit oneOfIndex(String index, QueryBuilder q, SortOrder sort) { SearchRequestBuilder srb = c.prepareSearch(); srb.setIndices(index); srb.setQuery(q); srb.setSize(1); srb.addSort("timestamp", sort); SearchResponse r = c.search(srb.request()).actionGet(); if (r.getHits() != null && r.getHits().totalHits() > 0) { return r.getHits().getAt(0); } else { return null; } }
/** Sets the search request to warm. */ public PutWarmerRequest searchRequest(SearchRequestBuilder searchRequest) { this.searchRequest = searchRequest.request(); return this; }
/** * Add a search request to execute. Note, the order is important, the search response will be * returned in the same order as the search requests. */ public MultiSearchRequest add(SearchRequestBuilder request) { requests.add(request.request()); return this; }