Search in sources :

Example 11 with Scroll

use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.

the class RestSearchAction method parseSearchRequest.

/**
 * Parses the rest request on top of the SearchRequest, preserving values that are not overridden by the rest request.
 *
 * @param requestContentParser body of the request to read. This method does not attempt to read the body from the {@code request}
 *        parameter
 * @param setSize how the size url parameter is handled. {@code udpate_by_query} and regular search differ here.
 */
public static void parseSearchRequest(SearchRequest searchRequest, RestRequest request, XContentParser requestContentParser, NamedWriteableRegistry namedWriteableRegistry, IntConsumer setSize) throws IOException {
    if (searchRequest.source() == null) {
        searchRequest.source(new SearchSourceBuilder());
    }
    searchRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
    if (requestContentParser != null) {
        searchRequest.source().parseXContent(requestContentParser, true);
    }
    final int batchedReduceSize = request.paramAsInt("batched_reduce_size", searchRequest.getBatchedReduceSize());
    searchRequest.setBatchedReduceSize(batchedReduceSize);
    if (request.hasParam("pre_filter_shard_size")) {
        searchRequest.setPreFilterShardSize(request.paramAsInt("pre_filter_shard_size", SearchRequest.DEFAULT_PRE_FILTER_SHARD_SIZE));
    }
    if (request.hasParam("max_concurrent_shard_requests")) {
        // only set if we have the parameter since we auto adjust the max concurrency on the coordinator
        // based on the number of nodes in the cluster
        final int maxConcurrentShardRequests = request.paramAsInt("max_concurrent_shard_requests", searchRequest.getMaxConcurrentShardRequests());
        searchRequest.setMaxConcurrentShardRequests(maxConcurrentShardRequests);
    }
    if (request.hasParam("allow_partial_search_results")) {
        // only set if we have the parameter passed to override the cluster-level default
        searchRequest.allowPartialSearchResults(request.paramAsBoolean("allow_partial_search_results", null));
    }
    // do not allow 'query_and_fetch' or 'dfs_query_and_fetch' search types
    // from the REST layer. these modes are an internal optimization and should
    // not be specified explicitly by the user.
    String searchType = request.param("search_type");
    if ("query_and_fetch".equals(searchType) || "dfs_query_and_fetch".equals(searchType)) {
        throw new IllegalArgumentException("Unsupported search type [" + searchType + "]");
    } else {
        searchRequest.searchType(searchType);
    }
    parseSearchSource(searchRequest.source(), request, setSize);
    searchRequest.requestCache(request.paramAsBoolean("request_cache", searchRequest.requestCache()));
    String scroll = request.param("scroll");
    if (scroll != null) {
        searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }
    searchRequest.routing(request.param("routing"));
    searchRequest.preference(request.param("preference"));
    searchRequest.indicesOptions(IndicesOptions.fromRequest(request, searchRequest.indicesOptions()));
    checkRestTotalHits(request, searchRequest);
    if (searchRequest.pointInTimeBuilder() != null) {
        preparePointInTime(searchRequest, request, namedWriteableRegistry);
    } else {
        searchRequest.setCcsMinimizeRoundtrips(request.paramAsBoolean("ccs_minimize_roundtrips", searchRequest.isCcsMinimizeRoundtrips()));
    }
    searchRequest.setCancelAfterTimeInterval(request.paramAsTime("cancel_after_time_interval", null));
}
Also used : Scroll(org.opensearch.search.Scroll) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 12 with Scroll

use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.

the class RestSearchScrollAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String scrollId = request.param("scroll_id");
    SearchScrollRequest searchScrollRequest = new SearchScrollRequest();
    searchScrollRequest.scrollId(scrollId);
    String scroll = request.param("scroll");
    if (scroll != null) {
        searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }
    request.withContentOrSourceParamParserOrNull(xContentParser -> {
        if (xContentParser != null) {
            // NOTE: if rest request with xcontent body has request parameters, values parsed from request body have the precedence
            try {
                searchScrollRequest.fromXContent(xContentParser);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to parse request body", e);
            }
        }
    });
    return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) TimeValue.parseTimeValue(org.opensearch.common.unit.TimeValue.parseTimeValue) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) IOException(java.io.IOException) List(java.util.List) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Scroll(org.opensearch.search.Scroll) Collections(java.util.Collections) Scroll(org.opensearch.search.Scroll) IOException(java.io.IOException) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest)

Example 13 with Scroll

use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.

the class SearchScrollAsyncActionTests method testShardFailures.

public void testShardFailures() throws InterruptedException {
    ParsedScrollId scrollId = getParsedScrollId(new SearchContextIdForNode(null, "node1", new ShardSearchContextId("", 1)), new SearchContextIdForNode(null, "node2", new ShardSearchContextId("", 2)), new SearchContextIdForNode(null, "node3", new ShardSearchContextId("", 17)), new SearchContextIdForNode(null, "node1", new ShardSearchContextId("", 0)), new SearchContextIdForNode(null, "node3", new ShardSearchContextId("", 0)));
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(new DiscoveryNode("node1", buildNewFakeTransportAddress(), Version.CURRENT)).add(new DiscoveryNode("node2", buildNewFakeTransportAddress(), Version.CURRENT)).add(new DiscoveryNode("node3", buildNewFakeTransportAddress(), Version.CURRENT)).build();
    AtomicArray<SearchAsyncActionTests.TestSearchPhaseResult> results = new AtomicArray<>(scrollId.getContext().length);
    SearchScrollRequest request = new SearchScrollRequest();
    request.scroll(new Scroll(TimeValue.timeValueMinutes(1)));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger movedCounter = new AtomicInteger(0);
    SearchScrollAsyncAction<SearchAsyncActionTests.TestSearchPhaseResult> action = new SearchScrollAsyncAction<SearchAsyncActionTests.TestSearchPhaseResult>(scrollId, logger, discoveryNodes, dummyListener(), null, request, null) {

        @Override
        protected void executeInitialPhase(Transport.Connection connection, InternalScrollSearchRequest internalRequest, SearchActionListener<SearchAsyncActionTests.TestSearchPhaseResult> searchActionListener) {
            new Thread(() -> {
                if (internalRequest.contextId().getId() == 17) {
                    searchActionListener.onFailure(new IllegalArgumentException("BOOM on shard"));
                } else {
                    SearchAsyncActionTests.TestSearchPhaseResult testSearchPhaseResult = new SearchAsyncActionTests.TestSearchPhaseResult(internalRequest.contextId(), connection.getNode());
                    testSearchPhaseResult.setSearchShardTarget(new SearchShardTarget(connection.getNode().getId(), new ShardId("test", "_na_", 1), null, OriginalIndices.NONE));
                    searchActionListener.onResponse(testSearchPhaseResult);
                }
            }).start();
        }

        @Override
        protected Transport.Connection getConnection(String clusterAlias, DiscoveryNode node) {
            return new SearchAsyncActionTests.MockConnection(node);
        }

        @Override
        protected SearchPhase moveToNextPhase(BiFunction<String, String, DiscoveryNode> clusterNodeLookup) {
            assertEquals(1, movedCounter.incrementAndGet());
            return new SearchPhase("test") {

                @Override
                public void run() throws IOException {
                    latch.countDown();
                }
            };
        }

        @Override
        protected void onFirstPhaseResult(int shardId, SearchAsyncActionTests.TestSearchPhaseResult result) {
            results.setOnce(shardId, result);
        }
    };
    action.run();
    latch.await();
    ShardSearchFailure[] shardSearchFailures = action.buildShardFailures();
    assertEquals(1, shardSearchFailures.length);
    assertEquals("IllegalArgumentException[BOOM on shard]", shardSearchFailures[0].reason());
    SearchContextIdForNode[] context = scrollId.getContext();
    for (int i = 0; i < results.length(); i++) {
        if (context[i].getSearchContextId().getId() == 17) {
            assertNull(results.get(i));
        } else {
            assertNotNull(results.get(i));
            assertEquals(context[i].getSearchContextId(), results.get(i).getContextId());
            assertEquals(context[i].getNode(), results.get(i).node.getId());
        }
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) InternalScrollSearchRequest(org.opensearch.search.internal.InternalScrollSearchRequest) ShardId(org.opensearch.index.shard.ShardId) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Scroll(org.opensearch.search.Scroll) CountDownLatch(java.util.concurrent.CountDownLatch) ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiFunction(java.util.function.BiFunction) SearchShardTarget(org.opensearch.search.SearchShardTarget) Transport(org.opensearch.transport.Transport)

Example 14 with Scroll

use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.

the class SearchRequestTests method mutate.

private SearchRequest mutate(SearchRequest searchRequest) {
    SearchRequest mutation = new SearchRequest(searchRequest);
    List<Runnable> mutators = new ArrayList<>();
    mutators.add(() -> mutation.indices(ArrayUtils.concat(searchRequest.indices(), new String[] { randomAlphaOfLength(10) })));
    mutators.add(() -> mutation.indicesOptions(randomValueOtherThan(searchRequest.indicesOptions(), () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()))));
    mutators.add(() -> mutation.preference(randomValueOtherThan(searchRequest.preference(), () -> randomAlphaOfLengthBetween(3, 10))));
    mutators.add(() -> mutation.routing(randomValueOtherThan(searchRequest.routing(), () -> randomAlphaOfLengthBetween(3, 10))));
    mutators.add(() -> mutation.requestCache((randomValueOtherThan(searchRequest.requestCache(), OpenSearchTestCase::randomBoolean))));
    mutators.add(() -> mutation.scroll(randomValueOtherThan(searchRequest.scroll(), () -> new Scroll(new TimeValue(randomNonNegativeLong() % 100000)))));
    mutators.add(() -> mutation.searchType(randomValueOtherThan(searchRequest.searchType(), () -> randomFrom(SearchType.DFS_QUERY_THEN_FETCH, SearchType.QUERY_THEN_FETCH))));
    mutators.add(() -> mutation.source(randomValueOtherThan(searchRequest.source(), this::createSearchSourceBuilder)));
    mutators.add(() -> mutation.setCcsMinimizeRoundtrips(searchRequest.isCcsMinimizeRoundtrips() == false));
    mutators.add(() -> mutation.setCancelAfterTimeInterval(searchRequest.getCancelAfterTimeInterval() != null ? null : TimeValue.parseTimeValue(randomTimeValue(), null, "cancel_after_time_interval")));
    randomFrom(mutators).run();
    return mutation;
}
Also used : Scroll(org.opensearch.search.Scroll) ArrayList(java.util.ArrayList) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

Scroll (org.opensearch.search.Scroll)14 BiFunction (java.util.function.BiFunction)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)5 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)5 AtomicArray (org.opensearch.common.util.concurrent.AtomicArray)5 InternalScrollSearchRequest (org.opensearch.search.internal.InternalScrollSearchRequest)5 ShardSearchContextId (org.opensearch.search.internal.ShardSearchContextId)5 Transport (org.opensearch.transport.Transport)5 IOException (java.io.IOException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ShardId (org.opensearch.index.shard.ShardId)4 SearchShardTarget (org.opensearch.search.SearchShardTarget)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ActionListener (org.opensearch.action.ActionListener)2 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)2 SearchResponse (org.opensearch.action.search.SearchResponse)2