public void testInitialSearchParamsMisc() { SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder()); Version remoteVersion = Version.fromId(between(0, Version.CURRENT.id)); TimeValue scroll = null; if (randomBoolean()) { scroll = TimeValue.parseTimeValue(randomPositiveTimeValue(), "test"); searchRequest.scroll(scroll); } int size = between(0, Integer.MAX_VALUE); searchRequest.source().size(size); Boolean fetchVersion = null; if (randomBoolean()) { fetchVersion = randomBoolean(); searchRequest.source().version(fetchVersion); } Map<String, String> params = initialSearchParams(searchRequest, remoteVersion); assertThat( params, scroll == null ? not(hasKey("scroll")) : hasEntry("scroll", scroll.toString())); assertThat(params, hasEntry("size", Integer.toString(size))); assertThat( params, fetchVersion == null || fetchVersion == true ? hasEntry("version", null) : not(hasEntry("version", null))); assertThat(params, hasEntry("_source", "true")); }
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()); }
@RequestMapping( value = "/reports/eventSearch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) public void eventSearch(final HttpEntity<String> httpEntity, final HttpServletResponse response) throws IOException { long start = System.currentTimeMillis(); String jsonRequest = httpEntity.getBody(); final SearchRequest searchRequest = new SearchRequest(indexName); try { searchRequest.source(jsonRequest); searchRequest.types("event"); SearchResponse r = clientTpl.executeWithClient( new ClientCallback<SearchResponse>() { @Override public SearchResponse execute(final Client client) { return client.search(searchRequest).actionGet(); } }); response.setContentType(MediaType.APPLICATION_JSON_VALUE); OutputStream responseStream = response.getOutputStream(); XContentBuilder builder = XContentFactory.jsonBuilder(responseStream); builder.startObject(); r.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); builder.close(); responseStream.close(); } finally { logger.debug("Executed search in {}ms: {}", System.currentTimeMillis() - start, jsonRequest); } }
/** * Build a random search request. * * @param randomSearchSourceBuilder builds a random {@link SearchSourceBuilder}. You can use * {@link #randomSearchSourceBuilder(Supplier, Supplier, Supplier, Supplier)}. */ public static SearchRequest randomSearchRequest( Supplier<SearchSourceBuilder> randomSearchSourceBuilder) throws IOException { SearchRequest searchRequest = new SearchRequest(); if (randomBoolean()) { searchRequest.indices(generateRandomStringArray(10, 10, false, false)); } if (randomBoolean()) { searchRequest.indicesOptions( IndicesOptions.fromOptions( randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); } if (randomBoolean()) { searchRequest.types(generateRandomStringArray(10, 10, false, false)); } if (randomBoolean()) { searchRequest.preference(randomAsciiOfLengthBetween(3, 10)); } if (randomBoolean()) { searchRequest.requestCache(randomBoolean()); } if (randomBoolean()) { searchRequest.routing(randomAsciiOfLengthBetween(3, 10)); } if (randomBoolean()) { searchRequest.scroll(randomPositiveTimeValue()); } if (randomBoolean()) { searchRequest.searchType(randomFrom(SearchType.values())); } if (randomBoolean()) { searchRequest.source(randomSearchSourceBuilder.get()); } return searchRequest; }
public void testInitialSearchParamsSort() { SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder()); // Test sort:_doc for versions that support it. Version remoteVersion = Version.fromId(between(Version.V_2_1_0_ID, Version.CURRENT.id)); searchRequest.source().sort("_doc"); assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("sort", "_doc:asc")); // Test search_type scan for versions that don't support sort:_doc. remoteVersion = Version.fromId(between(0, Version.V_2_1_0_ID - 1)); assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("search_type", "scan")); // Test sorting by some field. Version doesn't matter. remoteVersion = Version.fromId(between(0, Version.CURRENT.id)); searchRequest.source().sorts().clear(); searchRequest.source().sort("foo"); assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("sort", "foo:asc")); }
@Override public void handleRequest( final RestRequest request, final RestChannel channel, final Client client) throws IOException { final SearchRequest searchRequest = new SearchRequest( Strings.splitStringByCommaToArray(request.param("index")), new SearchSourceBuilder()); searchRequest.indicesOptions( IndicesOptions.fromRequest(request, searchRequest.indicesOptions())); if (RestActions.hasBodyContent(request)) { final BytesReference sourceBytes = RestActions.getRestContent(request); try (XContentParser parser = XContentFactory.xContent(sourceBytes).createParser(sourceBytes)) { final QueryParseContext context = new QueryParseContext(queryRegistry, parser, parseFieldMatcher); searchRequest.source().suggest(SuggestBuilder.fromXContent(context, suggesters)); } } else { throw new IllegalArgumentException("no content or source provided to execute suggestion"); } searchRequest.routing(request.param("routing")); searchRequest.preference(request.param("preference")); client.search( searchRequest, new RestBuilderListener<SearchResponse>(channel) { @Override public RestResponse buildResponse(SearchResponse response, XContentBuilder builder) throws Exception { RestStatus restStatus = RestStatus.status( response.getSuccessfulShards(), response.getTotalShards(), response.getShardFailures()); builder.startObject(); buildBroadcastShardsHeader( builder, request, response.getTotalShards(), response.getSuccessfulShards(), response.getFailedShards(), response.getShardFailures()); Suggest suggest = response.getSuggest(); if (suggest != null) { suggest.toInnerXContent(builder, request); } builder.endObject(); return new BytesRestResponse(restStatus, builder); } }); }
public static InternalSearchRequest internalSearchRequest( ShardRouting shardRouting, int numberOfShards, SearchRequest request, String[] filteringAliases, long nowInMillis) { InternalSearchRequest internalRequest = new InternalSearchRequest(shardRouting, numberOfShards, request.searchType()); internalRequest.source(request.source(), request.sourceOffset(), request.sourceLength()); internalRequest.extraSource( request.extraSource(), request.extraSourceOffset(), request.extraSourceLength()); internalRequest.scroll(request.scroll()); internalRequest.filteringAliases(filteringAliases); internalRequest.types(request.types()); internalRequest.nowInMillis(nowInMillis); return internalRequest; }
public static SearchRequest parseSearchRequest(RestRequest request) { String[] indices = Strings.splitStringByCommaToArray(request.param("index")); SearchRequest searchRequest = new SearchRequest(indices); // get the content, and put it in the body // add content/source as template if template flag is set boolean isTemplateRequest = request.path().endsWith("/template"); if (RestActions.hasBodyContent(request)) { if (isTemplateRequest) { searchRequest.templateSource(RestActions.getRestContent(request)); } else { searchRequest.source(RestActions.getRestContent(request)); } } // do not allow 'query_and_fetch' or 'dfs_query_and_fetch' search types // from the REST layer. these modes are an internal optimization and should // not be specified explicitly by the user. String searchType = request.param("search_type"); if (SearchType.fromString(searchType).equals(SearchType.QUERY_AND_FETCH) || SearchType.fromString(searchType).equals(SearchType.DFS_QUERY_AND_FETCH)) { throw new IllegalArgumentException("Unsupported search type [" + searchType + "]"); } else { searchRequest.searchType(searchType); } searchRequest.extraSource(parseSearchSource(request)); searchRequest.queryCache(request.paramAsBoolean("query_cache", null)); String scroll = request.param("scroll"); if (scroll != null) { searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll"))); } searchRequest.types(Strings.splitStringByCommaToArray(request.param("type"))); searchRequest.routing(request.param("routing")); searchRequest.preference(request.param("preference")); searchRequest.indicesOptions( IndicesOptions.fromRequest(request, searchRequest.indicesOptions())); return searchRequest; }
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()); }
@Before @Override public void setUp() throws Exception { super.setUp(); threadPool = new TestThreadPool(getTestName()) { @Override public Executor executor(String name) { return Runnable::run; } @Override public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) { command.run(); return null; } }; retries = 0; searchRequest = new SearchRequest(); searchRequest.scroll(timeValueMinutes(5)); searchRequest.source(new SearchSourceBuilder().size(10).version(true).sort("_doc").size(123)); retriesAllowed = 0; }
public void testInitialSearchParamsFields() { SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder()); // Test request without any fields Version remoteVersion = VersionUtils.randomVersion(random()); assertThat( initialSearchParams(searchRequest, remoteVersion), not(either(hasKey("stored_fields")).or(hasKey("fields")))); // Setup some fields for the next two tests searchRequest.source().storedField("_source").storedField("_id"); // Test stored_fields for versions that support it remoteVersion = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0_alpha4, null); assertThat( initialSearchParams(searchRequest, remoteVersion), hasEntry("stored_fields", "_source,_id")); // Test fields for versions that support it remoteVersion = VersionUtils.randomVersionBetween(random(), null, Version.V_5_0_0_alpha3); assertThat( initialSearchParams(searchRequest, remoteVersion), hasEntry("fields", "_source,_id")); }