Search in sources :

Example 6 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class ScrollQueryFetchSearchResult method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    shardTarget = new SearchShardTarget(in);
    result = readQueryFetchSearchResult(in);
    result.shardTarget(shardTarget);
}
Also used : SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Example 7 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class SnapshotsService method endSnapshot.

/**
     * Finalizes the shard in repository and then removes it from cluster state
     * <p>
     * This is non-blocking method that runs on a thread from SNAPSHOT thread pool
     *
     * @param entry   snapshot
     * @param failure failure reason or null if snapshot was successful
     */
private void endSnapshot(final SnapshotsInProgress.Entry entry, final String failure) {
    threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(new Runnable() {

        @Override
        public void run() {
            final Snapshot snapshot = entry.snapshot();
            try {
                final Repository repository = repositoriesService.repository(snapshot.getRepository());
                logger.trace("[{}] finalizing snapshot in repository, state: [{}], failure[{}]", snapshot, entry.state(), failure);
                ArrayList<ShardSearchFailure> failures = new ArrayList<>();
                ArrayList<SnapshotShardFailure> shardFailures = new ArrayList<>();
                for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardStatus : entry.shards()) {
                    ShardId shardId = shardStatus.key;
                    ShardSnapshotStatus status = shardStatus.value;
                    if (status.state().failed()) {
                        failures.add(new ShardSearchFailure(status.reason(), new SearchShardTarget(status.nodeId(), shardId)));
                        shardFailures.add(new SnapshotShardFailure(status.nodeId(), shardId, status.reason()));
                    }
                }
                SnapshotInfo snapshotInfo = repository.finalizeSnapshot(snapshot.getSnapshotId(), entry.indices(), entry.startTime(), failure, entry.shards().size(), Collections.unmodifiableList(shardFailures), entry.getRepositoryStateId());
                removeSnapshotFromClusterState(snapshot, snapshotInfo, null);
            } catch (Exception e) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to finalize snapshot", snapshot), e);
                removeSnapshotFromClusterState(snapshot, null, e);
            }
        }
    });
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RepositoryMissingException(org.elasticsearch.repositories.RepositoryMissingException) IOException(java.io.IOException) ShardId(org.elasticsearch.index.shard.ShardId) Repository(org.elasticsearch.repositories.Repository) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure)

Example 8 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class SearchPhaseControllerTests method generateFetchResults.

private AtomicArray<QuerySearchResultProvider> generateFetchResults(int nShards, ScoreDoc[] mergedSearchDocs, Suggest mergedSuggest) {
    AtomicArray<QuerySearchResultProvider> fetchResults = new AtomicArray<>(nShards);
    for (int shardIndex = 0; shardIndex < nShards; shardIndex++) {
        float maxScore = -1F;
        SearchShardTarget shardTarget = new SearchShardTarget("", new Index("", ""), shardIndex);
        FetchSearchResult fetchSearchResult = new FetchSearchResult(shardIndex, shardTarget);
        List<SearchHit> searchHits = new ArrayList<>();
        for (ScoreDoc scoreDoc : mergedSearchDocs) {
            if (scoreDoc.shardIndex == shardIndex) {
                searchHits.add(new SearchHit(scoreDoc.doc, "", new Text(""), Collections.emptyMap()));
                if (scoreDoc.score > maxScore) {
                    maxScore = scoreDoc.score;
                }
            }
        }
        for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
            if (suggestion instanceof CompletionSuggestion) {
                for (CompletionSuggestion.Entry.Option option : ((CompletionSuggestion) suggestion).getOptions()) {
                    ScoreDoc doc = option.getDoc();
                    if (doc.shardIndex == shardIndex) {
                        searchHits.add(new SearchHit(doc.doc, "", new Text(""), Collections.emptyMap()));
                        if (doc.score > maxScore) {
                            maxScore = doc.score;
                        }
                    }
                }
            }
        }
        SearchHit[] hits = searchHits.toArray(new SearchHit[searchHits.size()]);
        fetchSearchResult.hits(new SearchHits(hits, hits.length, maxScore));
        fetchResults.set(shardIndex, fetchSearchResult);
    }
    return fetchResults;
}
Also used : AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) SearchHit(org.elasticsearch.search.SearchHit) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) Text(org.elasticsearch.common.text.Text) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) SearchHits(org.elasticsearch.search.SearchHits)

Example 9 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class SearchPhaseControllerTests method testConsumerConcurrently.

public void testConsumerConcurrently() throws InterruptedException {
    int expectedNumResults = randomIntBetween(1, 100);
    int bufferSize = randomIntBetween(2, 200);
    SearchRequest request = new SearchRequest();
    request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
    request.setBatchedReduceSize(bufferSize);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer = searchPhaseController.newSearchPhaseResults(request, expectedNumResults);
    AtomicInteger max = new AtomicInteger();
    CountDownLatch latch = new CountDownLatch(expectedNumResults);
    for (int i = 0; i < expectedNumResults; i++) {
        int id = i;
        Thread t = new Thread(() -> {
            int number = randomIntBetween(1, 1000);
            max.updateAndGet(prev -> Math.max(prev, number));
            QuerySearchResult result = new QuerySearchResult(id, new SearchShardTarget("node", new Index("a", "b"), id));
            result.topDocs(new TopDocs(id, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
            InternalAggregations aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", (double) number, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
            result.aggregations(aggs);
            consumer.consumeResult(id, result);
            latch.countDown();
        });
        t.start();
    }
    latch.await();
    SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
    InternalMax internalMax = (InternalMax) reduce.aggregations.asList().get(0);
    assertEquals(max.get(), internalMax.getValue(), 0.0D);
}
Also used : QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) InternalMax(org.elasticsearch.search.aggregations.metrics.max.InternalMax) Index(org.elasticsearch.index.Index) CountDownLatch(java.util.concurrent.CountDownLatch) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Example 10 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class DfsQueryPhaseTests method testFailPhaseOnException.

public void testFailPhaseOnException() throws IOException {
    AtomicArray<DfsSearchResult> results = new AtomicArray<>(2);
    AtomicReference<AtomicArray<QuerySearchResultProvider>> responseRef = new AtomicReference<>();
    results.set(0, new DfsSearchResult(1, new SearchShardTarget("node1", new Index("test", "na"), 0)));
    results.set(1, new DfsSearchResult(2, new SearchShardTarget("node2", new Index("test", "na"), 0)));
    results.get(0).termsStatistics(new Term[0], new TermStatistics[0]);
    results.get(1).termsStatistics(new Term[0], new TermStatistics[0]);
    SearchPhaseController controller = new SearchPhaseController(Settings.EMPTY, BigArrays.NON_RECYCLING_INSTANCE, null);
    SearchTransportService searchTransportService = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        public void sendExecuteQuery(Transport.Connection connection, QuerySearchRequest request, SearchTask task, ActionListener<QuerySearchResult> listener) {
            if (request.id() == 1) {
                QuerySearchResult queryResult = new QuerySearchResult(123, new SearchShardTarget("node1", new Index("test", "na"), 0));
                queryResult.topDocs(new TopDocs(1, new ScoreDoc[] { new ScoreDoc(42, 1.0F) }, 2.0F), new DocValueFormat[0]);
                // the size of the result set
                queryResult.size(2);
                listener.onResponse(queryResult);
            } else if (request.id() == 2) {
                throw new UncheckedIOException(new MockDirectoryWrapper.FakeIOException());
            } else {
                fail("no such request ID: " + request.id());
            }
        }
    };
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(2);
    mockSearchPhaseContext.searchTransport = searchTransportService;
    DfsQueryPhase phase = new DfsQueryPhase(results, controller, (response) -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            responseRef.set(response.results);
        }
    }, mockSearchPhaseContext);
    assertEquals("dfs_query", phase.getName());
    expectThrows(UncheckedIOException.class, () -> phase.run());
    // phase execution will clean up on the contexts
    assertTrue(mockSearchPhaseContext.releasedSearchContexts.isEmpty());
}
Also used : AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Index(org.elasticsearch.index.Index) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) ActionListener(org.elasticsearch.action.ActionListener) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) QuerySearchRequest(org.elasticsearch.search.query.QuerySearchRequest)

Aggregations

SearchShardTarget (org.elasticsearch.search.SearchShardTarget)29 Index (org.elasticsearch.index.Index)22 IOException (java.io.IOException)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)12 TopDocs (org.apache.lucene.search.TopDocs)11 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)11 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)10 ActionListener (org.elasticsearch.action.ActionListener)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ParsingException (org.elasticsearch.common.ParsingException)7 AtomicArray (org.elasticsearch.common.util.concurrent.AtomicArray)7 ShardId (org.elasticsearch.index.shard.ShardId)6 ArrayList (java.util.ArrayList)5 SearchHit (org.elasticsearch.search.SearchHit)5 SearchHits (org.elasticsearch.search.SearchHits)5 DfsSearchResult (org.elasticsearch.search.dfs.DfsSearchResult)5 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)5 QueryFetchSearchResult (org.elasticsearch.search.fetch.QueryFetchSearchResult)5 ShardFetchSearchRequest (org.elasticsearch.search.fetch.ShardFetchSearchRequest)5 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)4