Search in sources :

Example 16 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class RestDeleteByQueryAction method buildRequest.

@Override
protected DeleteByQueryRequest buildRequest(RestRequest request) throws IOException {
    if (false == request.hasContent()) {
        throw new ElasticsearchException("_delete_by_query requires a request body");
    }
    /*
         * Passing the search request through DeleteByQueryRequest first allows
         * it to set its own defaults which differ from SearchRequest's
         * defaults. Then the parseInternalRequest can override them.
         */
    DeleteByQueryRequest internal = new DeleteByQueryRequest(new SearchRequest());
    Map<String, Consumer<Object>> consumers = new HashMap<>();
    consumers.put("conflicts", o -> internal.setConflicts((String) o));
    parseInternalRequest(internal, request, consumers);
    return internal;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Consumer(java.util.function.Consumer) HashMap(java.util.HashMap) DeleteByQueryRequest(org.elasticsearch.action.bulk.byscroll.DeleteByQueryRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 17 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class RestUpdateByQueryAction method buildRequest.

@Override
@SuppressWarnings("unchecked")
protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOException {
    /*
         * Passing the search request through UpdateByQueryRequest first allows
         * it to set its own defaults which differ from SearchRequest's
         * defaults. Then the parse can override them.
         */
    UpdateByQueryRequest internal = new UpdateByQueryRequest(new SearchRequest());
    Map<String, Consumer<Object>> consumers = new HashMap<>();
    consumers.put("conflicts", o -> internal.setConflicts((String) o));
    consumers.put("script", o -> internal.setScript(parseScript((Map<String, Object>) o)));
    parseInternalRequest(internal, request, consumers);
    internal.setPipeline(request.param("pipeline"));
    return internal;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Consumer(java.util.function.Consumer) HashMap(java.util.HashMap)

Example 18 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class AbstractBulkByQueryRestHandler method parseInternalRequest.

protected void parseInternalRequest(Request internal, RestRequest restRequest, Map<String, Consumer<Object>> bodyConsumers) throws IOException {
    assert internal != null : "Request should not be null";
    assert restRequest != null : "RestRequest should not be null";
    SearchRequest searchRequest = internal.getSearchRequest();
    int scrollSize = searchRequest.source().size();
    searchRequest.source().size(SIZE_ALL_MATCHES);
    restRequest.withContentOrSourceParamParserOrNull(parser -> {
        XContentParser searchRequestParser = extractRequestSpecificFieldsAndReturnSearchCompatibleParser(parser, bodyConsumers);
        try {
            RestSearchAction.parseSearchRequest(searchRequest, restRequest, searchRequestParser);
        } finally {
            IOUtils.close(searchRequestParser);
        }
    });
    internal.setSize(searchRequest.source().size());
    searchRequest.source().size(restRequest.paramAsInt("scroll_size", scrollSize));
    String conflicts = restRequest.param("conflicts");
    if (conflicts != null) {
        internal.setConflicts(conflicts);
    }
    // Let the requester set search timeout. It is probably only going to be useful for testing but who knows.
    if (restRequest.hasParam("search_timeout")) {
        searchRequest.source().timeout(restRequest.paramAsTime("search_timeout", null));
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 19 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class RoundTripTests method testUpdateByQueryRequest.

public void testUpdateByQueryRequest() throws IOException {
    UpdateByQueryRequest update = new UpdateByQueryRequest(new SearchRequest());
    randomRequest(update);
    if (randomBoolean()) {
        update.setPipeline(randomAsciiOfLength(5));
    }
    UpdateByQueryRequest tripped = new UpdateByQueryRequest();
    roundTrip(update, tripped);
    assertRequestEquals(update, tripped);
    assertEquals(update.getPipeline(), tripped.getPipeline());
    // Try slices with a version that doesn't support slices. That should fail.
    update.setSlices(between(2, 1000));
    Exception e = expectThrows(IllegalArgumentException.class, () -> roundTrip(Version.V_5_0_0_rc1, update, 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 UpdateByQueryRequest();
    update.setSlices(1);
    roundTrip(Version.V_5_0_0_rc1, update, tripped);
    assertRequestEquals(update, tripped);
    assertEquals(update.getPipeline(), tripped.getPipeline());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IOException(java.io.IOException)

Example 20 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class UpdateByQueryRequestTests method testUpdateByQueryRequestImplementsIndicesRequestReplaceable.

public void testUpdateByQueryRequestImplementsIndicesRequestReplaceable() {
    int numIndices = between(1, 100);
    String[] indices = new String[numIndices];
    for (int i = 0; i < numIndices; i++) {
        indices[i] = randomSimpleString(random(), 1, 30);
    }
    SearchRequest searchRequest = new SearchRequest(indices);
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
    searchRequest.indicesOptions(indicesOptions);
    UpdateByQueryRequest request = new UpdateByQueryRequest(searchRequest);
    for (int i = 0; i < numIndices; i++) {
        assertEquals(indices[i], request.indices()[i]);
    }
    assertSame(indicesOptions, request.indicesOptions());
    assertSame(request.indicesOptions(), request.getSearchRequest().indicesOptions());
    int numNewIndices = between(1, 100);
    String[] newIndices = new String[numNewIndices];
    for (int i = 0; i < numNewIndices; i++) {
        newIndices[i] = randomSimpleString(random(), 1, 30);
    }
    request.indices(newIndices);
    for (int i = 0; i < numNewIndices; i++) {
        ;
        assertEquals(newIndices[i], request.indices()[i]);
    }
    for (int i = 0; i < numNewIndices; i++) {
        ;
        assertEquals(newIndices[i], request.getSearchRequest().indices()[i]);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)66 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 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 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 Test (org.junit.Test)5 List (java.util.List)4