Search in sources :

Example 51 with SearchRequest

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);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) DeleteByQueryRequest(org.elasticsearch.action.bulk.byscroll.DeleteByQueryRequest) IOException(java.io.IOException)

Example 52 with SearchRequest

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) RemoteInfo(org.elasticsearch.index.reindex.remote.RemoteInfo) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) IndexRequest(org.elasticsearch.action.index.IndexRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue) IOException(java.io.IOException)

Example 53 with SearchRequest

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);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 54 with SearchRequest

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));
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) GET(org.elasticsearch.rest.RestRequest.Method.GET) ParsingException(org.elasticsearch.common.ParsingException) RestSearchAction(org.elasticsearch.rest.action.search.RestSearchAction) ScriptType(org.elasticsearch.script.ScriptType) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Set(java.util.Set) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ParseField(org.elasticsearch.common.ParseField) Collections(java.util.Collections) SearchRequest(org.elasticsearch.action.search.SearchRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 55 with SearchRequest

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);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ExecutableScript(org.elasticsearch.script.ExecutableScript) XContentParser(org.elasticsearch.common.xcontent.XContentParser) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)69 SearchResponse (org.elasticsearch.action.search.SearchResponse)21 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 Test (org.junit.Test)7 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)6 BytesArray (org.elasticsearch.common.bytes.BytesArray)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)6 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)6 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)6 ArrayList (java.util.ArrayList)5 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 List (java.util.List)4