Search in sources :

Example 6 with SearchRequest

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

the class RestCountAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    SearchRequest countRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index")));
    countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0);
    countRequest.source(searchSourceBuilder);
    request.withContentOrSourceParamParserOrNull(parser -> {
        if (parser == null) {
            QueryBuilder queryBuilder = RestActions.urlParamsToQueryBuilder(request);
            if (queryBuilder != null) {
                searchSourceBuilder.query(queryBuilder);
            }
        } else {
            searchSourceBuilder.query(RestActions.getQueryContent(parser));
        }
    });
    countRequest.routing(request.param("routing"));
    float minScore = request.paramAsFloat("min_score", -1f);
    if (minScore != -1f) {
        searchSourceBuilder.minScore(minScore);
    }
    countRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    countRequest.preference(request.param("preference"));
    final int terminateAfter = request.paramAsInt("terminate_after", DEFAULT_TERMINATE_AFTER);
    if (terminateAfter < 0) {
        throw new IllegalArgumentException("terminateAfter must be > 0");
    } else if (terminateAfter > 0) {
        searchSourceBuilder.terminateAfter(terminateAfter);
    }
    return channel -> client.search(countRequest, new RestBuilderListener<SearchResponse>(channel) {

        @Override
        public RestResponse buildResponse(SearchResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            if (terminateAfter != DEFAULT_TERMINATE_AFTER) {
                builder.field("terminated_early", response.isTerminatedEarly());
            }
            builder.field("count", response.getHits().getTotalHits());
            buildBroadcastShardsHeader(builder, request, response.getTotalShards(), response.getSuccessfulShards(), response.getFailedShards(), response.getShardFailures());
            builder.endObject();
            return new BytesRestResponse(response.status(), builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) DEFAULT_TERMINATE_AFTER(org.elasticsearch.search.internal.SearchContext.DEFAULT_TERMINATE_AFTER) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) NodeClient(org.elasticsearch.client.node.NodeClient) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 7 with SearchRequest

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

the class IndicesRequestIT method testSearchDfsQueryThenFetch.

public void testSearchDfsQueryThenFetch() throws Exception {
    interceptTransportActions(SearchTransportService.DFS_ACTION_NAME, SearchTransportService.QUERY_ID_ACTION_NAME, SearchTransportService.FETCH_ID_ACTION_NAME, SearchTransportService.FREE_CONTEXT_ACTION_NAME);
    String[] randomIndicesOrAliases = randomIndicesOrAliases();
    for (int i = 0; i < randomIndicesOrAliases.length; i++) {
        client().prepareIndex(randomIndicesOrAliases[i], "type", "id-" + i).setSource("field", "value").get();
    }
    refresh();
    SearchRequest searchRequest = new SearchRequest(randomIndicesOrAliases).searchType(SearchType.DFS_QUERY_THEN_FETCH);
    SearchResponse searchResponse = internalCluster().coordOnlyNodeClient().search(searchRequest).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), greaterThan(0L));
    clearInterceptedActions();
    assertSameIndices(searchRequest, SearchTransportService.DFS_ACTION_NAME, SearchTransportService.QUERY_ID_ACTION_NAME, SearchTransportService.FETCH_ID_ACTION_NAME);
    //free context messages are not necessarily sent, but if they are, check their indices
    assertSameIndicesOptionalRequests(searchRequest, SearchTransportService.FREE_CONTEXT_ACTION_NAME);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with SearchRequest

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

the class IndicesRequestIT method testSearchQueryThenFetch.

public void testSearchQueryThenFetch() throws Exception {
    interceptTransportActions(SearchTransportService.QUERY_ACTION_NAME, SearchTransportService.FETCH_ID_ACTION_NAME, SearchTransportService.FREE_CONTEXT_ACTION_NAME);
    String[] randomIndicesOrAliases = randomIndicesOrAliases();
    for (int i = 0; i < randomIndicesOrAliases.length; i++) {
        client().prepareIndex(randomIndicesOrAliases[i], "type", "id-" + i).setSource("field", "value").get();
    }
    refresh();
    SearchRequest searchRequest = new SearchRequest(randomIndicesOrAliases).searchType(SearchType.QUERY_THEN_FETCH);
    SearchResponse searchResponse = internalCluster().coordOnlyNodeClient().search(searchRequest).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), greaterThan(0L));
    clearInterceptedActions();
    assertSameIndices(searchRequest, SearchTransportService.QUERY_ACTION_NAME, SearchTransportService.FETCH_ID_ACTION_NAME);
    //free context messages are not necessarily sent, but if they are, check their indices
    assertSameIndicesOptionalRequests(searchRequest, SearchTransportService.FREE_CONTEXT_ACTION_NAME);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 9 with SearchRequest

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

the class DeleteByQueryRequestTests method testTypesSetter.

public void testTypesSetter() {
    int numTypes = between(1, 50);
    String[] types = new String[numTypes];
    for (int i = 0; i < numTypes; i++) {
        types[i] = randomSimpleString(random(), 1, 30);
    }
    SearchRequest searchRequest = new SearchRequest();
    DeleteByQueryRequest request = new DeleteByQueryRequest(searchRequest);
    request.types(types);
    assertArrayEquals(request.types(), types);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString)

Example 10 with SearchRequest

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

the class DeleteByQueryRequestTests method testDeleteteByQueryRequestImplementsIndicesRequestReplaceable.

public void testDeleteteByQueryRequestImplementsIndicesRequestReplaceable() {
    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);
    DeleteByQueryRequest request = new DeleteByQueryRequest(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)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