Search in sources :

Example 11 with SearchPhaseResult

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

the class SearchContextIdTests method testEncode.

public void testEncode() {
    final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Arrays.asList(new NamedWriteableRegistry.Entry(QueryBuilder.class, TermQueryBuilder.NAME, TermQueryBuilder::new), new NamedWriteableRegistry.Entry(QueryBuilder.class, MatchAllQueryBuilder.NAME, MatchAllQueryBuilder::new), new NamedWriteableRegistry.Entry(QueryBuilder.class, IdsQueryBuilder.NAME, IdsQueryBuilder::new)));
    final AtomicArray<SearchPhaseResult> queryResults = TransportSearchHelperTests.generateQueryResults();
    final Version version = Version.CURRENT;
    final Map<String, AliasFilter> aliasFilters = new HashMap<>();
    for (SearchPhaseResult result : queryResults.asList()) {
        final AliasFilter aliasFilter;
        if (randomBoolean()) {
            aliasFilter = new AliasFilter(randomQueryBuilder());
        } else if (randomBoolean()) {
            aliasFilter = new AliasFilter(randomQueryBuilder(), "alias-" + between(1, 10));
        } else {
            aliasFilter = AliasFilter.EMPTY;
        }
        if (randomBoolean()) {
            aliasFilters.put(result.getSearchShardTarget().getShardId().getIndex().getUUID(), aliasFilter);
        }
    }
    final String id = SearchContextId.encode(queryResults.asList(), aliasFilters, version);
    final SearchContextId context = SearchContextId.decode(namedWriteableRegistry, id);
    assertThat(context.shards().keySet(), hasSize(3));
    assertThat(context.aliasFilter(), equalTo(aliasFilters));
    SearchContextIdForNode node1 = context.shards().get(new ShardId("idx", "uuid1", 2));
    assertThat(node1.getClusterAlias(), equalTo("cluster_x"));
    assertThat(node1.getNode(), equalTo("node_1"));
    assertThat(node1.getSearchContextId().getId(), equalTo(1L));
    assertThat(node1.getSearchContextId().getSessionId(), equalTo("a"));
    SearchContextIdForNode node2 = context.shards().get(new ShardId("idy", "uuid2", 42));
    assertThat(node2.getClusterAlias(), equalTo("cluster_y"));
    assertThat(node2.getNode(), equalTo("node_2"));
    assertThat(node2.getSearchContextId().getId(), equalTo(12L));
    assertThat(node2.getSearchContextId().getSessionId(), equalTo("b"));
    SearchContextIdForNode node3 = context.shards().get(new ShardId("idy", "uuid2", 43));
    assertThat(node3.getClusterAlias(), nullValue());
    assertThat(node3.getNode(), equalTo("node_3"));
    assertThat(node3.getSearchContextId().getId(), equalTo(42L));
    assertThat(node3.getSearchContextId().getSessionId(), equalTo("c"));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) AliasFilter(org.opensearch.search.internal.AliasFilter) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) HashMap(java.util.HashMap) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) ShardId(org.opensearch.index.shard.ShardId) Version(org.opensearch.Version) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 12 with SearchPhaseResult

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

the class AbstractSearchAsyncActionTests method testShardNotAvailableWithDisallowPartialFailures.

public void testShardNotAvailableWithDisallowPartialFailures() {
    SearchRequest searchRequest = new SearchRequest().allowPartialSearchResults(false);
    AtomicReference<Exception> exception = new AtomicReference<>();
    ActionListener<SearchResponse> listener = ActionListener.wrap(response -> fail("onResponse should not be called"), exception::set);
    int numShards = randomIntBetween(2, 10);
    ArraySearchPhaseResults<SearchPhaseResult> phaseResults = new ArraySearchPhaseResults<>(numShards);
    AbstractSearchAsyncAction<SearchPhaseResult> action = createAction(searchRequest, phaseResults, listener, false, new AtomicLong());
    // skip one to avoid the "all shards failed" failure.
    SearchShardIterator skipIterator = new SearchShardIterator(null, null, Collections.emptyList(), null);
    skipIterator.resetAndSkip();
    action.skipShard(skipIterator);
    // expect at least 2 shards, so onPhaseDone should report failure.
    action.onPhaseDone();
    assertThat(exception.get(), instanceOf(SearchPhaseExecutionException.class));
    SearchPhaseExecutionException searchPhaseExecutionException = (SearchPhaseExecutionException) exception.get();
    assertEquals("Partial shards failure (" + (numShards - 1) + " shards unavailable)", searchPhaseExecutionException.getMessage());
    assertEquals("test", searchPhaseExecutionException.getPhaseName());
    assertEquals(0, searchPhaseExecutionException.shardFailures().length);
    assertEquals(0, searchPhaseExecutionException.getSuppressed().length);
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) AtomicLong(java.util.concurrent.atomic.AtomicLong) SearchPhaseResult(org.opensearch.search.SearchPhaseResult)

Example 13 with SearchPhaseResult

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

the class AbstractSearchAsyncActionTests method testBuildSearchResponse.

public void testBuildSearchResponse() {
    SearchRequest searchRequest = new SearchRequest().allowPartialSearchResults(randomBoolean());
    ArraySearchPhaseResults<SearchPhaseResult> phaseResults = new ArraySearchPhaseResults<>(10);
    AbstractSearchAsyncAction<SearchPhaseResult> action = createAction(searchRequest, phaseResults, null, false, new AtomicLong());
    InternalSearchResponse internalSearchResponse = InternalSearchResponse.empty();
    SearchResponse searchResponse = action.buildSearchResponse(internalSearchResponse, action.buildShardFailures(), null, null);
    assertSame(searchResponse.getAggregations(), internalSearchResponse.aggregations());
    assertSame(searchResponse.getSuggest(), internalSearchResponse.suggest());
    assertSame(searchResponse.getProfileResults(), internalSearchResponse.profile());
    assertSame(searchResponse.getHits(), internalSearchResponse.hits());
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 14 with SearchPhaseResult

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

the class AbstractSearchAsyncActionTests method phaseResults.

private static ArraySearchPhaseResults<SearchPhaseResult> phaseResults(Set<ShardSearchContextId> contextIds, List<Tuple<String, String>> nodeLookups, int numFailures) {
    int numResults = randomIntBetween(1, 10);
    ArraySearchPhaseResults<SearchPhaseResult> phaseResults = new ArraySearchPhaseResults<>(numResults + numFailures);
    for (int i = 0; i < numResults; i++) {
        ShardSearchContextId contextId = new ShardSearchContextId(UUIDs.randomBase64UUID(), randomNonNegativeLong());
        contextIds.add(contextId);
        SearchPhaseResult phaseResult = new PhaseResult(contextId);
        String resultClusterAlias = randomBoolean() ? null : randomAlphaOfLengthBetween(5, 10);
        String resultNodeId = randomAlphaOfLengthBetween(5, 10);
        ShardId resultShardId = new ShardId("index", "index-uuid", i);
        nodeLookups.add(Tuple.tuple(resultClusterAlias, resultNodeId));
        phaseResult.setSearchShardTarget(new SearchShardTarget(resultNodeId, resultShardId, resultClusterAlias, OriginalIndices.NONE));
        phaseResult.setShardIndex(i);
        phaseResults.consumeResult(phaseResult, () -> {
        });
    }
    return phaseResults;
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) SearchShardTarget(org.opensearch.search.SearchShardTarget)

Example 15 with SearchPhaseResult

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

the class AbstractSearchAsyncActionTests method runTestTook.

private void runTestTook(final boolean controlled) {
    final AtomicLong expected = new AtomicLong();
    AbstractSearchAsyncAction<SearchPhaseResult> action = createAction(new SearchRequest(), new ArraySearchPhaseResults<>(10), null, controlled, expected);
    final long actual = action.buildTookInMillis();
    if (controlled) {
        // with a controlled clock, we can assert the exact took time
        assertThat(actual, equalTo(TimeUnit.NANOSECONDS.toMillis(expected.get())));
    } else {
        // with a real clock, the best we can say is that it took as long as we spun for
        assertThat(actual, greaterThanOrEqualTo(TimeUnit.NANOSECONDS.toMillis(expected.get())));
    }
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) SearchPhaseResult(org.opensearch.search.SearchPhaseResult)

Aggregations

SearchPhaseResult (org.opensearch.search.SearchPhaseResult)35 SearchShardTarget (org.opensearch.search.SearchShardTarget)17 ShardId (org.opensearch.index.shard.ShardId)16 ArrayList (java.util.ArrayList)15 ShardSearchContextId (org.opensearch.search.internal.ShardSearchContextId)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 ShardSearchRequest (org.opensearch.search.internal.ShardSearchRequest)9 QuerySearchResult (org.opensearch.search.query.QuerySearchResult)9 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 ScoreDoc (org.apache.lucene.search.ScoreDoc)8 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)8 TopDocs (org.apache.lucene.search.TopDocs)7 AtomicArray (org.opensearch.common.util.concurrent.AtomicArray)7 IOException (java.io.IOException)6 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 CompletionSuggestion (org.opensearch.search.suggest.completion.CompletionSuggestion)6 TotalHits (org.apache.lucene.search.TotalHits)5 ActionListener (org.opensearch.action.ActionListener)5