use of org.opensearch.action.search.TransportSearchAction.SearchTimeProvider in project OpenSearch by opensearch-project.
the class SearchResponseMergerTests method testMergeShardFailuresNullShardTarget.
public void testMergeShardFailuresNullShardTarget() throws InterruptedException {
SearchTimeProvider searchTimeProvider = new SearchTimeProvider(0, 0, () -> 0);
SearchResponseMerger merger = new SearchResponseMerger(0, 0, SearchContext.TRACK_TOTAL_HITS_ACCURATE, searchTimeProvider, emptyReduceContextBuilder());
PriorityQueue<Tuple<ShardId, ShardSearchFailure>> priorityQueue = new PriorityQueue<>(Comparator.comparing(Tuple::v1));
for (int i = 0; i < numResponses; i++) {
int numFailures = randomIntBetween(1, 10);
ShardSearchFailure[] shardSearchFailures = new ShardSearchFailure[numFailures];
for (int j = 0; j < numFailures; j++) {
String index = "index-" + i;
ShardId shardId = new ShardId(index, index + "-uuid", j);
OpenSearchException openSearchException = new OpenSearchException(new IllegalArgumentException());
openSearchException.setShard(shardId);
ShardSearchFailure failure = new ShardSearchFailure(openSearchException);
shardSearchFailures[j] = failure;
priorityQueue.add(Tuple.tuple(shardId, failure));
}
SearchResponse searchResponse = new SearchResponse(InternalSearchResponse.empty(), null, 1, 1, 0, 100L, shardSearchFailures, SearchResponse.Clusters.EMPTY);
addResponse(merger, searchResponse);
}
awaitResponsesAdded();
assertEquals(numResponses, merger.numResponses());
SearchResponse.Clusters clusters = SearchResponseTests.randomClusters();
SearchResponse mergedResponse = merger.getMergedResponse(clusters);
assertSame(clusters, mergedResponse.getClusters());
assertEquals(numResponses, mergedResponse.getTotalShards());
assertEquals(numResponses, mergedResponse.getSuccessfulShards());
assertEquals(0, mergedResponse.getSkippedShards());
assertEquals(priorityQueue.size(), mergedResponse.getFailedShards());
ShardSearchFailure[] shardFailures = mergedResponse.getShardFailures();
assertEquals(priorityQueue.size(), shardFailures.length);
for (ShardSearchFailure shardFailure : shardFailures) {
ShardSearchFailure expected = priorityQueue.poll().v2();
assertSame(expected, shardFailure);
}
}
use of org.opensearch.action.search.TransportSearchAction.SearchTimeProvider in project OpenSearch by opensearch-project.
the class SearchResponseMergerTests method testMergeShardFailuresNullShardId.
public void testMergeShardFailuresNullShardId() throws InterruptedException {
SearchTimeProvider searchTimeProvider = new SearchTimeProvider(0, 0, () -> 0);
SearchResponseMerger merger = new SearchResponseMerger(0, 0, SearchContext.TRACK_TOTAL_HITS_ACCURATE, searchTimeProvider, emptyReduceContextBuilder());
List<ShardSearchFailure> expectedFailures = new ArrayList<>();
for (int i = 0; i < numResponses; i++) {
int numFailures = randomIntBetween(1, 50);
ShardSearchFailure[] shardSearchFailures = new ShardSearchFailure[numFailures];
for (int j = 0; j < numFailures; j++) {
ShardSearchFailure shardSearchFailure = new ShardSearchFailure(new OpenSearchException(new IllegalArgumentException()));
shardSearchFailures[j] = shardSearchFailure;
expectedFailures.add(shardSearchFailure);
}
SearchResponse searchResponse = new SearchResponse(InternalSearchResponse.empty(), null, 1, 1, 0, 100L, shardSearchFailures, SearchResponse.Clusters.EMPTY);
addResponse(merger, searchResponse);
}
awaitResponsesAdded();
assertEquals(numResponses, merger.numResponses());
ShardSearchFailure[] shardFailures = merger.getMergedResponse(SearchResponse.Clusters.EMPTY).getShardFailures();
assertThat(Arrays.asList(shardFailures), containsInAnyOrder(expectedFailures.toArray(ShardSearchFailure.EMPTY_ARRAY)));
}
use of org.opensearch.action.search.TransportSearchAction.SearchTimeProvider in project OpenSearch by opensearch-project.
the class SearchResponseMergerTests method testMergeShardFailures.
public void testMergeShardFailures() throws InterruptedException {
SearchTimeProvider searchTimeProvider = new SearchTimeProvider(0, 0, () -> 0);
SearchResponseMerger merger = new SearchResponseMerger(0, 0, SearchContext.TRACK_TOTAL_HITS_ACCURATE, searchTimeProvider, emptyReduceContextBuilder());
PriorityQueue<Tuple<SearchShardTarget, ShardSearchFailure>> priorityQueue = new PriorityQueue<>(Comparator.comparing(Tuple::v1, (o1, o2) -> {
int compareTo = o1.getShardId().compareTo(o2.getShardId());
if (compareTo != 0) {
return compareTo;
}
return o1.getClusterAlias().compareTo(o2.getClusterAlias());
}));
int numIndices = numResponses * randomIntBetween(1, 3);
Iterator<Map.Entry<String, Index[]>> indicesPerCluster = randomRealisticIndices(numIndices, numResponses).entrySet().iterator();
for (int i = 0; i < numResponses; i++) {
Map.Entry<String, Index[]> entry = indicesPerCluster.next();
String clusterAlias = entry.getKey();
Index[] indices = entry.getValue();
int numFailures = randomIntBetween(1, 10);
ShardSearchFailure[] shardSearchFailures = new ShardSearchFailure[numFailures];
for (int j = 0; j < numFailures; j++) {
ShardId shardId = new ShardId(randomFrom(indices), j);
SearchShardTarget searchShardTarget = new SearchShardTarget(randomAlphaOfLength(6), shardId, clusterAlias, null);
ShardSearchFailure failure = new ShardSearchFailure(new IllegalArgumentException(), searchShardTarget);
shardSearchFailures[j] = failure;
priorityQueue.add(Tuple.tuple(searchShardTarget, failure));
}
SearchResponse searchResponse = new SearchResponse(InternalSearchResponse.empty(), null, 1, 1, 0, 100L, shardSearchFailures, SearchResponse.Clusters.EMPTY);
addResponse(merger, searchResponse);
}
awaitResponsesAdded();
assertEquals(numResponses, merger.numResponses());
SearchResponse.Clusters clusters = SearchResponseTests.randomClusters();
SearchResponse mergedResponse = merger.getMergedResponse(clusters);
assertSame(clusters, mergedResponse.getClusters());
assertEquals(numResponses, mergedResponse.getTotalShards());
assertEquals(numResponses, mergedResponse.getSuccessfulShards());
assertEquals(0, mergedResponse.getSkippedShards());
assertEquals(priorityQueue.size(), mergedResponse.getFailedShards());
ShardSearchFailure[] shardFailures = mergedResponse.getShardFailures();
assertEquals(priorityQueue.size(), shardFailures.length);
for (ShardSearchFailure shardFailure : shardFailures) {
ShardSearchFailure expected = priorityQueue.poll().v2();
assertSame(expected, shardFailure);
}
}
use of org.opensearch.action.search.TransportSearchAction.SearchTimeProvider in project OpenSearch by opensearch-project.
the class SearchResponseMergerTests method testMergeNoResponsesAdded.
public void testMergeNoResponsesAdded() {
long currentRelativeTime = randomNonNegativeLong();
final SearchTimeProvider timeProvider = new SearchTimeProvider(randomLong(), 0, () -> currentRelativeTime);
SearchResponseMerger merger = new SearchResponseMerger(0, 10, Integer.MAX_VALUE, timeProvider, emptyReduceContextBuilder());
SearchResponse.Clusters clusters = SearchResponseTests.randomClusters();
assertEquals(0, merger.numResponses());
SearchResponse response = merger.getMergedResponse(clusters);
assertSame(clusters, response.getClusters());
assertEquals(TimeUnit.NANOSECONDS.toMillis(currentRelativeTime), response.getTook().millis());
assertEquals(0, response.getTotalShards());
assertEquals(0, response.getSuccessfulShards());
assertEquals(0, response.getSkippedShards());
assertEquals(0, response.getFailedShards());
assertEquals(0, response.getNumReducePhases());
assertFalse(response.isTimedOut());
assertNotNull(response.getHits().getTotalHits());
assertEquals(0, response.getHits().getTotalHits().value);
assertEquals(0, response.getHits().getHits().length);
assertEquals(TotalHits.Relation.EQUAL_TO, response.getHits().getTotalHits().relation);
assertNull(response.getScrollId());
assertSame(InternalAggregations.EMPTY, response.getAggregations());
assertNull(response.getSuggest());
assertEquals(0, response.getProfileResults().size());
assertNull(response.isTerminatedEarly());
assertEquals(0, response.getShardFailures().length);
}
use of org.opensearch.action.search.TransportSearchAction.SearchTimeProvider in project OpenSearch by opensearch-project.
the class SearchResponseMergerTests method testMergeCompletionSuggestions.
public void testMergeCompletionSuggestions() throws InterruptedException {
String suggestionName = randomAlphaOfLengthBetween(4, 8);
int size = randomIntBetween(1, 100);
SearchResponseMerger searchResponseMerger = new SearchResponseMerger(0, 0, 0, new SearchTimeProvider(0, 0, () -> 0), emptyReduceContextBuilder());
for (int i = 0; i < numResponses; i++) {
List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions = new ArrayList<>();
CompletionSuggestion completionSuggestion = new CompletionSuggestion(suggestionName, size, false);
CompletionSuggestion.Entry options = new CompletionSuggestion.Entry(new Text("suggest"), 0, 10);
int docId = randomIntBetween(0, Integer.MAX_VALUE);
CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(docId, new Text(randomAlphaOfLengthBetween(5, 10)), i, Collections.emptyMap());
SearchHit hit = new SearchHit(docId);
ShardId shardId = new ShardId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLength(10), randomIntBetween(0, Integer.MAX_VALUE));
String clusterAlias = randomBoolean() ? "" : randomAlphaOfLengthBetween(5, 10);
hit.shard(new SearchShardTarget("node", shardId, clusterAlias, OriginalIndices.NONE));
option.setHit(hit);
options.addOption(option);
completionSuggestion.addTerm(options);
suggestions.add(completionSuggestion);
Suggest suggest = new Suggest(suggestions);
SearchHits searchHits = new SearchHits(new SearchHit[0], null, Float.NaN);
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(searchHits, null, suggest, null, false, null, 1);
SearchResponse searchResponse = new SearchResponse(internalSearchResponse, null, 1, 1, 0, randomLong(), ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
addResponse(searchResponseMerger, searchResponse);
}
awaitResponsesAdded();
assertEquals(numResponses, searchResponseMerger.numResponses());
SearchResponse.Clusters clusters = SearchResponseTests.randomClusters();
SearchResponse mergedResponse = searchResponseMerger.getMergedResponse(clusters);
assertSame(clusters, mergedResponse.getClusters());
assertEquals(numResponses, mergedResponse.getTotalShards());
assertEquals(numResponses, mergedResponse.getSuccessfulShards());
assertEquals(0, mergedResponse.getSkippedShards());
assertEquals(0, mergedResponse.getFailedShards());
assertEquals(0, mergedResponse.getShardFailures().length);
Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestion = mergedResponse.getSuggest().getSuggestion(suggestionName);
assertEquals(1, suggestion.getEntries().size());
Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option> options = suggestion.getEntries().get(0);
assertEquals(Math.min(numResponses, size), options.getOptions().size());
int i = numResponses;
for (Suggest.Suggestion.Entry.Option option : options) {
assertEquals(--i, option.getScore(), 0f);
}
}
Aggregations