Search in sources :

Example 21 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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 22 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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 23 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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)

Example 24 with SearchRequest

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

the class RemoteRequestBuildersTests method testInitialSearchParamsSort.

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"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Version(org.elasticsearch.Version) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 25 with SearchRequest

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

the class RemoteRequestBuildersTests method testInitialSearchEntity.

public void testInitialSearchEntity() throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(new SearchSourceBuilder());
    String query = "{\"match_all\":{}}";
    HttpEntity entity = initialSearchEntity(searchRequest, new BytesArray(query));
    assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
    assertEquals("{\"query\":" + query + ",\"_source\":true}", Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
    // Source filtering is included if set up
    searchRequest.source().fetchSource(new String[] { "in1", "in2" }, new String[] { "out" });
    entity = initialSearchEntity(searchRequest, new BytesArray(query));
    assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
    assertEquals("{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}", Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
    // Invalid XContent fails
    RuntimeException e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{}, \"trailing\": {}")));
    assertThat(e.getCause().getMessage(), containsString("Unexpected character (',' (code 44))"));
    e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{")));
    assertThat(e.getCause().getMessage(), containsString("Unexpected end-of-input"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)156 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)81 SearchResponse (org.elasticsearch.action.search.SearchResponse)69 Test (org.junit.Test)37 IOException (java.io.IOException)31 SearchHit (org.elasticsearch.search.SearchHit)25 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)22 ArrayList (java.util.ArrayList)21 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)21 HashMap (java.util.HashMap)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 List (java.util.List)16 SearchHits (org.elasticsearch.search.SearchHits)15 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 Map (java.util.Map)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)11 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)10