use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.
the class RoundTripTests method testDeleteByQueryRequest.
public void testDeleteByQueryRequest() throws IOException {
DeleteByQueryRequest delete = new DeleteByQueryRequest(new SearchRequest());
randomRequest(delete);
DeleteByQueryRequest tripped = new DeleteByQueryRequest();
roundTrip(delete, tripped);
assertRequestEquals(delete, tripped);
// Try slices with a version that doesn't support slices. That should fail.
delete.setSlices(between(2, 1000));
Exception e = expectThrows(IllegalArgumentException.class, () -> roundTrip(Version.V_5_0_0_rc1, delete, null));
assertEquals("Attempting to send sliced reindex-style request to a node that doesn't support it. " + "Version is [5.0.0-rc1] but must be [5.1.1]", e.getMessage());
// Try without slices with a version that doesn't support slices. That should work.
tripped = new DeleteByQueryRequest();
delete.setSlices(1);
roundTrip(Version.V_5_0_0_rc1, delete, tripped);
assertRequestEquals(delete, tripped);
}
use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.
the class RoundTripTests method testReindexRequest.
public void testReindexRequest() throws IOException {
ReindexRequest reindex = new ReindexRequest(new SearchRequest(), new IndexRequest());
randomRequest(reindex);
reindex.getDestination().version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, 12L, 1L, 123124L, 12L));
reindex.getDestination().index("test");
if (randomBoolean()) {
int port = between(1, Integer.MAX_VALUE);
BytesReference query = new BytesArray(randomAsciiOfLength(5));
String username = randomBoolean() ? randomAsciiOfLength(5) : null;
String password = username != null && randomBoolean() ? randomAsciiOfLength(5) : null;
int headersCount = randomBoolean() ? 0 : between(1, 10);
Map<String, String> headers = new HashMap<>(headersCount);
while (headers.size() < headersCount) {
headers.put(randomAsciiOfLength(5), randomAsciiOfLength(5));
}
TimeValue socketTimeout = parseTimeValue(randomPositiveTimeValue(), "socketTimeout");
TimeValue connectTimeout = parseTimeValue(randomPositiveTimeValue(), "connectTimeout");
reindex.setRemoteInfo(new RemoteInfo(randomAsciiOfLength(5), randomAsciiOfLength(5), port, query, username, password, headers, socketTimeout, connectTimeout));
}
ReindexRequest tripped = new ReindexRequest();
roundTrip(reindex, tripped);
assertRequestEquals(reindex, tripped);
// Try slices with a version that doesn't support slices. That should fail.
reindex.setSlices(between(2, 1000));
Exception e = expectThrows(IllegalArgumentException.class, () -> roundTrip(Version.V_5_0_0_rc1, reindex, null));
assertEquals("Attempting to send sliced reindex-style request to a node that doesn't support it. " + "Version is [5.0.0-rc1] but must be [5.1.1]", e.getMessage());
// Try without slices with a version that doesn't support slices. That should work.
tripped = new ReindexRequest();
reindex.setSlices(1);
roundTrip(Version.V_5_0_0_rc1, reindex, tripped);
assertRequestEquals(Version.V_5_0_0_rc1, reindex, tripped);
}
use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.
the class MinDocCountIT method testMinDocCountOnTerms.
private void testMinDocCountOnTerms(String field, Script script, Terms.Order order, String include, boolean retry) throws Exception {
// all terms
final SearchResponse allTermsResponse = client().prepareSearch("idx").setTypes("type").setSize(0).setQuery(QUERY).addAggregation(script.apply(terms("terms"), field).collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).order(order).size(cardinality + randomInt(10)).minDocCount(0)).execute().actionGet();
assertAllSuccessful(allTermsResponse);
final Terms allTerms = allTermsResponse.getAggregations().get("terms");
assertEquals(cardinality, allTerms.getBuckets().size());
for (long minDocCount = 0; minDocCount < 20; ++minDocCount) {
final int size = randomIntBetween(1, cardinality + 2);
final SearchRequest request = client().prepareSearch("idx").setTypes("type").setSize(0).setQuery(QUERY).addAggregation(script.apply(terms("terms"), field).collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).order(order).size(size).includeExclude(include == null ? null : new IncludeExclude(include, null)).shardSize(cardinality + randomInt(10)).minDocCount(minDocCount)).request();
final SearchResponse response = client().search(request).get();
assertAllSuccessful(response);
assertSubset(allTerms, (Terms) response.getAggregations().get("terms"), minDocCount, size, include);
}
}
use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.
the class RestSearchTemplateAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
if (request.hasContentOrSourceParam() == false) {
throw new ElasticsearchException("request body is required");
}
// Creates the search request with all required params
SearchRequest searchRequest = new SearchRequest();
RestSearchAction.parseSearchRequest(searchRequest, request, null);
// Creates the search template request
SearchTemplateRequest searchTemplateRequest;
try (XContentParser parser = request.contentOrSourceParamParser()) {
searchTemplateRequest = PARSER.parse(parser, new SearchTemplateRequest(), null);
}
searchTemplateRequest.setRequest(searchRequest);
return channel -> client.execute(SearchTemplateAction.INSTANCE, searchTemplateRequest, new RestStatusToXContentListener<>(channel));
}
use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.
the class TransportSearchTemplateAction method doExecute.
@Override
protected void doExecute(SearchTemplateRequest request, ActionListener<SearchTemplateResponse> listener) {
final SearchTemplateResponse response = new SearchTemplateResponse();
try {
Script script = new Script(request.getScriptType(), TEMPLATE_LANG, request.getScript(), request.getScriptParams() == null ? Collections.emptyMap() : request.getScriptParams());
ExecutableScript executable = scriptService.executable(script, SEARCH);
BytesReference source = (BytesReference) executable.run();
response.setSource(source);
if (request.isSimulate()) {
listener.onResponse(response);
return;
}
// Executes the search
SearchRequest searchRequest = request.getRequest();
//we can assume the template is always json as we convert it before compiling it
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContentRegistry, source)) {
SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
builder.parseXContent(new QueryParseContext(parser));
builder.explain(request.isExplain());
builder.profile(request.isProfile());
searchRequest.source(builder);
searchAction.execute(searchRequest, new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
try {
response.setResponse(searchResponse);
listener.onResponse(response);
} catch (Exception t) {
listener.onFailure(t);
}
}
@Override
public void onFailure(Exception t) {
listener.onFailure(t);
}
});
}
} catch (Exception t) {
listener.onFailure(t);
}
}
Aggregations