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"));
}
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);
}
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());
}
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;
}
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())));
}
}
Aggregations