Search in sources :

Example 1 with QuerySearchResultProvider

use of org.elasticsearch.search.query.QuerySearchResultProvider in project elasticsearch by elastic.

the class SearchPhaseControllerTests method testMerge.

public void testMerge() throws IOException {
    List<CompletionSuggestion> suggestions = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 5); i++) {
        suggestions.add(new CompletionSuggestion(randomAsciiOfLength(randomIntBetween(1, 5)), randomIntBetween(1, 20)));
    }
    int nShards = randomIntBetween(1, 20);
    int queryResultSize = randomBoolean() ? 0 : randomIntBetween(1, nShards * 2);
    AtomicArray<QuerySearchResultProvider> queryResults = generateQueryResults(nShards, suggestions, queryResultSize, false);
    // calculate offsets and score doc array
    List<ScoreDoc> mergedScoreDocs = new ArrayList<>();
    ScoreDoc[] mergedSearchDocs = getTopShardDocs(queryResults);
    mergedScoreDocs.addAll(Arrays.asList(mergedSearchDocs));
    Suggest mergedSuggest = reducedSuggest(queryResults);
    for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
        if (suggestion instanceof CompletionSuggestion) {
            CompletionSuggestion completionSuggestion = ((CompletionSuggestion) suggestion);
            mergedScoreDocs.addAll(completionSuggestion.getOptions().stream().map(CompletionSuggestion.Entry.Option::getDoc).collect(Collectors.toList()));
        }
    }
    ScoreDoc[] sortedDocs = mergedScoreDocs.toArray(new ScoreDoc[mergedScoreDocs.size()]);
    InternalSearchResponse mergedResponse = searchPhaseController.merge(true, sortedDocs, searchPhaseController.reducedQueryPhase(queryResults.asList()), generateFetchResults(nShards, mergedSearchDocs, mergedSuggest));
    assertThat(mergedResponse.hits().getHits().length, equalTo(mergedSearchDocs.length));
    Suggest suggestResult = mergedResponse.suggest();
    for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
        assertThat(suggestion, instanceOf(CompletionSuggestion.class));
        if (suggestion.getEntries().get(0).getOptions().size() > 0) {
            CompletionSuggestion suggestionResult = suggestResult.getSuggestion(suggestion.getName());
            assertNotNull(suggestionResult);
            List<CompletionSuggestion.Entry.Option> options = suggestionResult.getEntries().get(0).getOptions();
            assertThat(options.size(), equalTo(suggestion.getEntries().get(0).getOptions().size()));
            for (CompletionSuggestion.Entry.Option option : options) {
                assertNotNull(option.getHit());
            }
        }
    }
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) ArrayList(java.util.ArrayList) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 2 with QuerySearchResultProvider

use of org.elasticsearch.search.query.QuerySearchResultProvider 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 3 with QuerySearchResultProvider

use of org.elasticsearch.search.query.QuerySearchResultProvider in project elasticsearch by elastic.

the class SearchPhaseControllerTests method testSort.

public void testSort() throws Exception {
    List<CompletionSuggestion> suggestions = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 5); i++) {
        suggestions.add(new CompletionSuggestion(randomAsciiOfLength(randomIntBetween(1, 5)), randomIntBetween(1, 20)));
    }
    int nShards = randomIntBetween(1, 20);
    int queryResultSize = randomBoolean() ? 0 : randomIntBetween(1, nShards * 2);
    AtomicArray<QuerySearchResultProvider> results = generateQueryResults(nShards, suggestions, queryResultSize, false);
    ScoreDoc[] sortedDocs = searchPhaseController.sortDocs(true, results);
    int accumulatedLength = Math.min(queryResultSize, getTotalQueryHits(results));
    for (Suggest.Suggestion<?> suggestion : reducedSuggest(results)) {
        int suggestionSize = suggestion.getEntries().get(0).getOptions().size();
        accumulatedLength += suggestionSize;
    }
    assertThat(sortedDocs.length, equalTo(accumulatedLength));
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) ArrayList(java.util.ArrayList) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 4 with QuerySearchResultProvider

use of org.elasticsearch.search.query.QuerySearchResultProvider 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 5 with QuerySearchResultProvider

use of org.elasticsearch.search.query.QuerySearchResultProvider in project elasticsearch by elastic.

the class DfsQueryPhase method run.

@Override
public void run() throws IOException {
    // TODO we can potentially also consume the actual per shard results from the initial phase here in the aggregateDfs
    // to free up memory early
    final AggregatedDfs dfs = searchPhaseController.aggregateDfs(dfsSearchResults);
    final CountedCollector<QuerySearchResultProvider> counter = new CountedCollector<>(queryResult::consumeResult, dfsSearchResults.asList().size(), () -> {
        context.executeNextPhase(this, nextPhaseFactory.apply(queryResult));
    }, context);
    for (final AtomicArray.Entry<DfsSearchResult> entry : dfsSearchResults.asList()) {
        DfsSearchResult dfsResult = entry.value;
        final int shardIndex = entry.index;
        final SearchShardTarget searchShardTarget = dfsResult.shardTarget();
        Transport.Connection connection = context.getConnection(searchShardTarget.getNodeId());
        QuerySearchRequest querySearchRequest = new QuerySearchRequest(context.getRequest(), dfsResult.id(), dfs);
        searchTransportService.sendExecuteQuery(connection, querySearchRequest, context.getTask(), ActionListener.wrap(result -> counter.onResult(shardIndex, result, searchShardTarget), exception -> {
            try {
                if (context.getLogger().isDebugEnabled()) {
                    context.getLogger().debug((Supplier<?>) () -> new ParameterizedMessage("[{}] Failed to execute query phase", querySearchRequest.id()), exception);
                }
                counter.onFailure(shardIndex, searchShardTarget, exception);
            } finally {
                context.sendReleaseSearchContext(querySearchRequest.id(), connection);
            }
        }));
    }
}
Also used : SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Transport(org.elasticsearch.transport.Transport) Supplier(org.apache.logging.log4j.util.Supplier) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) AggregatedDfs(org.elasticsearch.search.dfs.AggregatedDfs) IOException(java.io.IOException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) QuerySearchRequest(org.elasticsearch.search.query.QuerySearchRequest) Function(java.util.function.Function) ActionListener(org.elasticsearch.action.ActionListener) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) AggregatedDfs(org.elasticsearch.search.dfs.AggregatedDfs) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) QuerySearchRequest(org.elasticsearch.search.query.QuerySearchRequest) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Transport(org.elasticsearch.transport.Transport)

Aggregations

QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)17 ScoreDoc (org.apache.lucene.search.ScoreDoc)15 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)11 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)10 IOException (java.io.IOException)9 TopDocs (org.apache.lucene.search.TopDocs)9 Index (org.elasticsearch.index.Index)9 SearchHit (org.elasticsearch.search.SearchHit)9 SearchHits (org.elasticsearch.search.SearchHits)9 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)9 ActionListener (org.elasticsearch.action.ActionListener)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 AtomicArray (org.elasticsearch.common.util.concurrent.AtomicArray)6 QueryFetchSearchResult (org.elasticsearch.search.fetch.QueryFetchSearchResult)6 ShardFetchSearchRequest (org.elasticsearch.search.fetch.ShardFetchSearchRequest)6 ArrayList (java.util.ArrayList)5 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)5 Suggest (org.elasticsearch.search.suggest.Suggest)4 IntArrayList (com.carrotsearch.hppc.IntArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3