Search in sources :

Example 6 with Scroll

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

the class SearchSliceIT method testInvalidFields.

public void testInvalidFields() throws Exception {
    setupIndex(0, 1);
    SearchPhaseExecutionException exc = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).slice(new SliceBuilder("invalid_random_int", 0, 10)).get());
    Throwable rootCause = findRootCause(exc);
    assertThat(rootCause.getClass(), equalTo(IllegalArgumentException.class));
    assertThat(rootCause.getMessage(), startsWith("cannot load numeric doc values"));
    exc = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).slice(new SliceBuilder("invalid_random_kw", 0, 10)).get());
    rootCause = findRootCause(exc);
    assertThat(rootCause.getClass(), equalTo(IllegalArgumentException.class));
    assertThat(rootCause.getMessage(), startsWith("cannot load numeric doc values"));
}
Also used : SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) Scroll(org.opensearch.search.Scroll)

Example 7 with Scroll

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

the class SearchSliceIT method testSearchSort.

public void testSearchSort() throws Exception {
    int numShards = randomIntBetween(1, 7);
    int numDocs = randomIntBetween(100, 1000);
    setupIndex(numDocs, numShards);
    int max = randomIntBetween(2, numShards * 3);
    for (String field : new String[] { "_id", "random_int", "static_int" }) {
        int fetchSize = randomIntBetween(10, 100);
        // test _doc sort
        SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).addSort(SortBuilders.fieldSort("_doc"));
        assertSearchSlicesWithScroll(request, field, max, numDocs);
        // test numeric sort
        request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).addSort(SortBuilders.fieldSort("random_int")).setSize(fetchSize);
        assertSearchSlicesWithScroll(request, field, max, numDocs);
    }
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) Scroll(org.opensearch.search.Scroll)

Example 8 with Scroll

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

the class SearchSliceIT method testWithPreferenceAndRoutings.

public void testWithPreferenceAndRoutings() throws Exception {
    int numShards = 10;
    int totalDocs = randomIntBetween(100, 1000);
    setupIndex(totalDocs, numShards);
    {
        SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setPreference("_shards:1,4").setSize(0).get();
        int numDocs = (int) sr.getHits().getTotalHits().value;
        int max = randomIntBetween(2, numShards * 3);
        int fetchSize = randomIntBetween(10, 100);
        SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).setPreference("_shards:1,4").addSort(SortBuilders.fieldSort("_doc"));
        assertSearchSlicesWithScroll(request, "_id", max, numDocs);
    }
    {
        SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setRouting("foo", "bar").setSize(0).get();
        int numDocs = (int) sr.getHits().getTotalHits().value;
        int max = randomIntBetween(2, numShards * 3);
        int fetchSize = randomIntBetween(10, 100);
        SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).setRouting("foo", "bar").addSort(SortBuilders.fieldSort("_doc"));
        assertSearchSlicesWithScroll(request, "_id", max, numDocs);
    }
    {
        assertAcked(client().admin().indices().prepareAliases().addAliasAction(IndicesAliasesRequest.AliasActions.add().index("test").alias("alias1").routing("foo")).addAliasAction(IndicesAliasesRequest.AliasActions.add().index("test").alias("alias2").routing("bar")).addAliasAction(IndicesAliasesRequest.AliasActions.add().index("test").alias("alias3").routing("baz")).get());
        SearchResponse sr = client().prepareSearch("alias1", "alias3").setQuery(matchAllQuery()).setSize(0).get();
        int numDocs = (int) sr.getHits().getTotalHits().value;
        int max = randomIntBetween(2, numShards * 3);
        int fetchSize = randomIntBetween(10, 100);
        SearchRequestBuilder request = client().prepareSearch("alias1", "alias3").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).addSort(SortBuilders.fieldSort("_doc"));
        assertSearchSlicesWithScroll(request, "_id", max, numDocs);
    }
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) Scroll(org.opensearch.search.Scroll) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 9 with Scroll

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

the class SearchSliceIT method assertSearchSlicesWithScroll.

private void assertSearchSlicesWithScroll(SearchRequestBuilder request, String field, int numSlice, int numDocs) {
    int totalResults = 0;
    List<String> keys = new ArrayList<>();
    for (int id = 0; id < numSlice; id++) {
        SliceBuilder sliceBuilder = new SliceBuilder(field, id, numSlice);
        SearchResponse searchResponse = request.slice(sliceBuilder).get();
        totalResults += searchResponse.getHits().getHits().length;
        int expectedSliceResults = (int) searchResponse.getHits().getTotalHits().value;
        int numSliceResults = searchResponse.getHits().getHits().length;
        String scrollId = searchResponse.getScrollId();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            assertTrue(keys.add(hit.getId()));
        }
        while (searchResponse.getHits().getHits().length > 0) {
            searchResponse = client().prepareSearchScroll("test").setScrollId(scrollId).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).get();
            scrollId = searchResponse.getScrollId();
            totalResults += searchResponse.getHits().getHits().length;
            numSliceResults += searchResponse.getHits().getHits().length;
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                assertTrue(keys.add(hit.getId()));
            }
        }
        assertThat(numSliceResults, equalTo(expectedSliceResults));
        clearScroll(scrollId);
    }
    assertThat(totalResults, equalTo(numDocs));
    assertThat(keys.size(), equalTo(numDocs));
    assertThat(new HashSet(keys).size(), equalTo(numDocs));
}
Also used : SearchHit(org.opensearch.search.SearchHit) Scroll(org.opensearch.search.Scroll) ArrayList(java.util.ArrayList) SearchResponse(org.opensearch.action.search.SearchResponse) HashSet(java.util.HashSet)

Example 10 with Scroll

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

the class SearchScrollRequest method fromXContent.

/**
 * Parse a search scroll request from a request body provided through the REST layer.
 * Values that are already be set and are also found while parsing will be overridden.
 */
public void fromXContent(XContentParser parser) throws IOException {
    if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
        throw new IllegalArgumentException("Malformed content, must start with an object");
    } else {
        XContentParser.Token token;
        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
                scrollId(parser.text());
            } else if ("scroll".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
                scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null, "scroll")));
            } else {
                throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] ");
            }
        }
    }
}
Also used : Scroll(org.opensearch.search.Scroll) XContentParser(org.opensearch.common.xcontent.XContentParser)

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