Search in sources :

Example 1 with SearchTimeProvider

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);
    }
}
Also used : SearchTimeProvider(org.opensearch.action.search.TransportSearchAction.SearchTimeProvider) PriorityQueue(java.util.PriorityQueue) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ShardId(org.opensearch.index.shard.ShardId) OpenSearchException(org.opensearch.OpenSearchException) Tuple(org.opensearch.common.collect.Tuple)

Example 2 with SearchTimeProvider

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)));
}
Also used : SearchTimeProvider(org.opensearch.action.search.TransportSearchAction.SearchTimeProvider) ArrayList(java.util.ArrayList) OpenSearchException(org.opensearch.OpenSearchException) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 3 with SearchTimeProvider

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);
    }
}
Also used : RemoteClusterAware(org.opensearch.transport.RemoteClusterAware) Max(org.opensearch.search.aggregations.metrics.Max) Arrays(java.util.Arrays) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult) PriorityQueue(java.util.PriorityQueue) SearchContext(org.opensearch.search.internal.SearchContext) OpenSearchException(org.opensearch.OpenSearchException) Collections.singletonList(java.util.Collections.singletonList) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) Map(java.util.Map) SortField(org.apache.lucene.search.SortField) Range(org.opensearch.search.aggregations.bucket.range.Range) InternalAggregationTestCase.emptyReduceContextBuilder(org.opensearch.test.InternalAggregationTestCase.emptyReduceContextBuilder) Index(org.opensearch.index.Index) SearchHit(org.opensearch.search.SearchHit) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) OriginalIndices(org.opensearch.action.OriginalIndices) Executors(java.util.concurrent.Executors) Tuple(org.opensearch.common.collect.Tuple) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) SearchProfileShardResultsTests(org.opensearch.search.profile.SearchProfileShardResultsTests) InternalDateRange(org.opensearch.search.aggregations.bucket.range.InternalDateRange) SearchProfileShardResults(org.opensearch.search.profile.SearchProfileShardResults) DocValueFormat(org.opensearch.search.DocValueFormat) HashMap(java.util.HashMap) SearchHits(org.opensearch.search.SearchHits) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) RemoteClusterService(org.opensearch.transport.RemoteClusterService) Iterator(java.util.Iterator) SearchTimeProvider(org.opensearch.action.search.TransportSearchAction.SearchTimeProvider) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) TotalHits(org.apache.lucene.search.TotalHits) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) SearchShardTarget(org.opensearch.search.SearchShardTarget) TreeMap(java.util.TreeMap) Suggest(org.opensearch.search.suggest.Suggest) Comparator(java.util.Comparator) Collections(java.util.Collections) Text(org.opensearch.common.text.Text) SearchTimeProvider(org.opensearch.action.search.TransportSearchAction.SearchTimeProvider) Index(org.opensearch.index.Index) PriorityQueue(java.util.PriorityQueue) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ShardId(org.opensearch.index.shard.ShardId) SearchShardTarget(org.opensearch.search.SearchShardTarget) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) TreeMap(java.util.TreeMap) Tuple(org.opensearch.common.collect.Tuple)

Example 4 with SearchTimeProvider

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);
}
Also used : SearchTimeProvider(org.opensearch.action.search.TransportSearchAction.SearchTimeProvider) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 5 with SearchTimeProvider

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);
    }
}
Also used : CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) SearchTimeProvider(org.opensearch.action.search.TransportSearchAction.SearchTimeProvider) SearchHit(org.opensearch.search.SearchHit) ArrayList(java.util.ArrayList) Text(org.opensearch.common.text.Text) Suggest(org.opensearch.search.suggest.Suggest) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ShardId(org.opensearch.index.shard.ShardId) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) SearchShardTarget(org.opensearch.search.SearchShardTarget) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Aggregations

SearchTimeProvider (org.opensearch.action.search.TransportSearchAction.SearchTimeProvider)12 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)12 SearchHits (org.opensearch.search.SearchHits)8 TotalHits (org.apache.lucene.search.TotalHits)5 SearchHit (org.opensearch.search.SearchHit)5 ArrayList (java.util.ArrayList)4 PriorityQueue (java.util.PriorityQueue)4 ShardId (org.opensearch.index.shard.ShardId)4 HashMap (java.util.HashMap)3 SortField (org.apache.lucene.search.SortField)3 OpenSearchException (org.opensearch.OpenSearchException)3 Text (org.opensearch.common.text.Text)3 Index (org.opensearch.index.Index)3 SearchShardTarget (org.opensearch.search.SearchShardTarget)3 Collections.emptyMap (java.util.Collections.emptyMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Tuple (org.opensearch.common.collect.Tuple)2 InternalAggregations (org.opensearch.search.aggregations.InternalAggregations)2 InternalDateRange (org.opensearch.search.aggregations.bucket.range.InternalDateRange)2