Search in sources :

Example 1 with SearchRequest

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

the class AbstractBulkByScrollRequestTestCase method testForSlice.

public void testForSlice() {
    R original = newRequest();
    original.setAbortOnVersionConflict(randomBoolean());
    original.setRefresh(randomBoolean());
    original.setTimeout(parseTimeValue(randomPositiveTimeValue(), "timeout"));
    original.setWaitForActiveShards(randomFrom(ActiveShardCount.ALL, ActiveShardCount.NONE, ActiveShardCount.ONE, ActiveShardCount.DEFAULT));
    original.setRetryBackoffInitialTime(parseTimeValue(randomPositiveTimeValue(), "retry_backoff_initial_time"));
    original.setMaxRetries(between(0, 1000));
    original.setSlices(between(2, 1000));
    original.setRequestsPerSecond(randomBoolean() ? Float.POSITIVE_INFINITY : randomValueOtherThanMany(r -> r < 0, ESTestCase::randomFloat));
    original.setSize(randomBoolean() ? AbstractBulkByScrollRequest.SIZE_ALL_MATCHES : between(0, Integer.MAX_VALUE));
    TaskId slicingTask = new TaskId(randomAsciiOfLength(5), randomLong());
    SearchRequest sliceRequest = new SearchRequest();
    R forSliced = original.forSlice(slicingTask, sliceRequest);
    assertEquals(original.isAbortOnVersionConflict(), forSliced.isAbortOnVersionConflict());
    assertEquals(original.isRefresh(), forSliced.isRefresh());
    assertEquals(original.getTimeout(), forSliced.getTimeout());
    assertEquals(original.getWaitForActiveShards(), forSliced.getWaitForActiveShards());
    assertEquals(original.getRetryBackoffInitialTime(), forSliced.getRetryBackoffInitialTime());
    assertEquals(original.getMaxRetries(), forSliced.getMaxRetries());
    assertEquals("only the parent task should store results", false, forSliced.getShouldStoreResult());
    assertEquals("slice requests always have a single worker", 1, forSliced.getSlices());
    assertEquals("requests_per_second is split between all workers", original.getRequestsPerSecond() / original.getSlices(), forSliced.getRequestsPerSecond(), Float.MIN_NORMAL);
    assertEquals("size is split evenly between all workers", original.getSize() == AbstractBulkByScrollRequest.SIZE_ALL_MATCHES ? AbstractBulkByScrollRequest.SIZE_ALL_MATCHES : original.getSize() / original.getSlices(), forSliced.getSize());
    assertEquals(slicingTask, forSliced.getParentTask());
    extraForSliceAssertions(original, forSliced);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ESTestCase(org.elasticsearch.test.ESTestCase) TaskId(org.elasticsearch.tasks.TaskId)

Example 2 with SearchRequest

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

the class RestCountAction method doCatRequest.

@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    SearchRequest countRequest = new SearchRequest(indices);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0);
    countRequest.source(searchSourceBuilder);
    try {
        request.withContentOrSourceParamParserOrNull(parser -> {
            if (parser == null) {
                QueryBuilder queryBuilder = RestActions.urlParamsToQueryBuilder(request);
                if (queryBuilder != null) {
                    searchSourceBuilder.query(queryBuilder);
                }
            } else {
                searchSourceBuilder.query(RestActions.getQueryContent(parser));
            }
        });
    } catch (IOException e) {
        throw new ElasticsearchException("Couldn't parse query", e);
    }
    return channel -> client.search(countRequest, new RestResponseListener<SearchResponse>(channel) {

        @Override
        public RestResponse buildResponse(SearchResponse countResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, countResponse), channel);
        }
    });
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) Table(org.elasticsearch.common.Table) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) Settings(org.elasticsearch.common.settings.Settings) SearchResponse(org.elasticsearch.action.search.SearchResponse) RestRequest(org.elasticsearch.rest.RestRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) NodeClient(org.elasticsearch.client.node.NodeClient) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestResponse(org.elasticsearch.rest.RestResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with SearchRequest

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

the class BulkByScrollParallelizationHelper method sliceIntoSubRequests.

/**
     * Slice a search request into {@code times} separate search requests slicing on {@code field}. Note that the slices are *shallow*
     * copies of this request so don't change them.
     */
static SearchRequest[] sliceIntoSubRequests(SearchRequest request, String field, int times) {
    SearchRequest[] slices = new SearchRequest[times];
    for (int slice = 0; slice < times; slice++) {
        SliceBuilder sliceBuilder = new SliceBuilder(field, slice, times);
        SearchSourceBuilder slicedSource;
        if (request.source() == null) {
            slicedSource = new SearchSourceBuilder().slice(sliceBuilder);
        } else {
            if (request.source().slice() != null) {
                throw new IllegalStateException("Can't slice a request that already has a slice configuration");
            }
            slicedSource = request.source().copyWithNewSlice(sliceBuilder);
        }
        slices[slice] = new SearchRequest().source(slicedSource).searchType(request.searchType()).indices(request.indices()).types(request.types()).routing(request.routing()).preference(request.preference()).requestCache(request.requestCache()).scroll(request.scroll()).indicesOptions(request.indicesOptions());
    }
    return slices;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SliceBuilder(org.elasticsearch.search.slice.SliceBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 4 with SearchRequest

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

the class RestMultiSearchAction method parseMultiLineRequest.

/**
     * Parses a multi-line {@link RestRequest} body, instantiating a {@link SearchRequest} for each line and applying the given consumer.
     */
public static void parseMultiLineRequest(RestRequest request, IndicesOptions indicesOptions, boolean allowExplicitIndex, BiConsumer<SearchRequest, XContentParser> consumer) throws IOException {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    String[] types = Strings.splitStringByCommaToArray(request.param("type"));
    String searchType = request.param("search_type");
    String routing = request.param("routing");
    final Tuple<XContentType, BytesReference> sourceTuple = request.contentOrSourceParam();
    final XContent xContent = sourceTuple.v1().xContent();
    final BytesReference data = sourceTuple.v2();
    int from = 0;
    int length = data.length();
    byte marker = xContent.streamSeparator();
    while (true) {
        int nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        // support first line with \n
        if (nextMarker == 0) {
            from = nextMarker + 1;
            continue;
        }
        SearchRequest searchRequest = new SearchRequest();
        if (indices != null) {
            searchRequest.indices(indices);
        }
        if (indicesOptions != null) {
            searchRequest.indicesOptions(indicesOptions);
        }
        if (types != null && types.length > 0) {
            searchRequest.types(types);
        }
        if (routing != null) {
            searchRequest.routing(routing);
        }
        if (searchType != null) {
            searchRequest.searchType(searchType);
        }
        IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
        // now parse the action
        if (nextMarker - from > 0) {
            try (XContentParser parser = xContent.createParser(request.getXContentRegistry(), data.slice(from, nextMarker - from))) {
                Map<String, Object> source = parser.map();
                for (Map.Entry<String, Object> entry : source.entrySet()) {
                    Object value = entry.getValue();
                    if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
                        if (!allowExplicitIndex) {
                            throw new IllegalArgumentException("explicit index in multi search is not allowed");
                        }
                        searchRequest.indices(nodeStringArrayValue(value));
                    } else if ("type".equals(entry.getKey()) || "types".equals(entry.getKey())) {
                        searchRequest.types(nodeStringArrayValue(value));
                    } else if ("search_type".equals(entry.getKey()) || "searchType".equals(entry.getKey())) {
                        searchRequest.searchType(nodeStringValue(value, null));
                    } else if ("request_cache".equals(entry.getKey()) || "requestCache".equals(entry.getKey())) {
                        searchRequest.requestCache(nodeBooleanValue(value, entry.getKey()));
                    } else if ("preference".equals(entry.getKey())) {
                        searchRequest.preference(nodeStringValue(value, null));
                    } else if ("routing".equals(entry.getKey())) {
                        searchRequest.routing(nodeStringValue(value, null));
                    }
                }
                defaultOptions = IndicesOptions.fromMap(source, defaultOptions);
            }
        }
        searchRequest.indicesOptions(defaultOptions);
        // move pointers
        from = nextMarker + 1;
        // now for the body
        nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        BytesReference bytes = data.slice(from, nextMarker - from);
        try (XContentParser parser = xContent.createParser(request.getXContentRegistry(), bytes)) {
            consumer.accept(searchRequest, parser);
        }
        // move pointers
        from = nextMarker + 1;
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) XContentType(org.elasticsearch.common.xcontent.XContentType) XContent(org.elasticsearch.common.xcontent.XContent) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 5 with SearchRequest

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

the class RestSearchAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    request.withContentOrSourceParamParserOrNull(parser -> parseSearchRequest(searchRequest, request, parser));
    return channel -> client.search(searchRequest, new RestStatusToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Arrays(java.util.Arrays) GET(org.elasticsearch.rest.RestRequest.Method.GET) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchRequest(org.elasticsearch.action.search.SearchRequest) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) NodeClient(org.elasticsearch.client.node.NodeClient) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Set(java.util.Set) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) StoredFieldsContext(org.elasticsearch.search.fetch.StoredFieldsContext) SuggestBuilders.termSuggestion(org.elasticsearch.search.suggest.SuggestBuilders.termSuggestion) SuggestMode(org.elasticsearch.search.suggest.term.TermSuggestionBuilder.SuggestMode) SortOrder(org.elasticsearch.search.sort.SortOrder) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue) Collections(java.util.Collections) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) SearchRequest(org.elasticsearch.action.search.SearchRequest)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)128 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)54 SearchResponse (org.elasticsearch.action.search.SearchResponse)44 Test (org.junit.Test)35 IOException (java.io.IOException)27 SearchHit (org.elasticsearch.search.SearchHit)23 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)21 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)16 List (java.util.List)16 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)16 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)13 SearchHits (org.elasticsearch.search.SearchHits)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Map (java.util.Map)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)11 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)10 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)9 QueryBuilders.matchAllQuery (org.elasticsearch.index.query.QueryBuilders.matchAllQuery)8