org.elasticsearch.search.internal.InternalSearchResponse Java Examples

The following examples show how to use org.elasticsearch.search.internal.InternalSearchResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static SearchResponse createEmptySearchResponse() throws IOException {
    return new SearchResponse(
        new InternalSearchResponse(
            new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 1.0f),
            new InternalAggregations(Collections.emptyList()),
            new Suggest(Collections.emptyList()),
            new SearchProfileShardResults(Collections.emptyMap()),
            false,
            false,
            1
        ),
        "",
        5,
        5,
        0,
        100,
        ShardSearchFailure.EMPTY_ARRAY,
        SearchResponse.Clusters.EMPTY
    );
}
 
Example #2
Source File: AbstractSearchAsyncAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void start() {
    if (expectedSuccessfulOps == 0) {
        // no search shards to search on, bail with empty response (it happens with search across _all with no indices around and consistent with broadcast operations)
        listener.onResponse(new SearchResponse(InternalSearchResponse.empty(), null, 0, 0, buildTookInMillis(), ShardSearchFailure.EMPTY_ARRAY));
        return;
    }
    int shardIndex = -1;
    for (final ShardIterator shardIt : shardsIts) {
        shardIndex++;
        final ShardRouting shard = shardIt.nextOrNull();
        if (shard != null) {
            performFirstPhase(shardIndex, shardIt, shard);
        } else {
            // really, no shards active in this group
            onFirstPhaseResult(shardIndex, null, null, shardIt, new NoShardAvailableActionException(shardIt.shardId()));
        }
    }
}
 
Example #3
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static SearchResponse createSearchResponse(ToXContentObject o) throws IOException {
    XContentBuilder content = o.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);

    SearchHit[] hits = new SearchHit[1];
    hits[0] = new SearchHit(0).sourceRef(BytesReference.bytes(content));

    return new SearchResponse(
        new InternalSearchResponse(
            new SearchHits(hits, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f),
            new InternalAggregations(Collections.emptyList()),
            new Suggest(Collections.emptyList()),
            new SearchProfileShardResults(Collections.emptyMap()),
            false,
            false,
            1
        ),
        "",
        5,
        5,
        0,
        100,
        ShardSearchFailure.EMPTY_ARRAY,
        SearchResponse.Clusters.EMPTY
    );
}
 
Example #4
Source File: SearchDfsQueryAndFetchAsyncAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void finishHim() {
    threadPool.executor(ThreadPool.Names.SEARCH).execute(new ActionRunnable<SearchResponse>(listener) {
        @Override
        public void doRun() throws IOException {
            sortedShardList = searchPhaseController.sortDocs(true, queryFetchResults);
            final InternalSearchResponse internalResponse = searchPhaseController.merge(sortedShardList, queryFetchResults,
                queryFetchResults, request);
            String scrollId = null;
            if (request.scroll() != null) {
                scrollId = TransportSearchHelper.buildScrollId(request.searchType(), firstResults, null);
            }
            listener.onResponse(new SearchResponse(internalResponse, scrollId, expectedSuccessfulOps, successfulOps.get(),
                buildTookInMillis(), buildShardFailures()));
        }

        @Override
        public void onFailure(Throwable t) {
            ReduceSearchPhaseException failure = new ReduceSearchPhaseException("query_fetch", "", t, buildShardFailures());
            if (logger.isDebugEnabled()) {
                logger.debug("failed to reduce search", failure);
            }
            super.onFailure(t);
        }
    });

}
 
Example #5
Source File: HttpSearchAction.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected SearchResponse createResponse(HttpInvocationContext<SearchRequest,SearchResponse> httpInvocationContext) throws IOException {
    if (httpInvocationContext == null) {
        throw new IllegalStateException("no http context");
    }
    HttpResponse httpResponse = httpInvocationContext.getHttpResponse();
    logger.info("{}", httpResponse.getContent().toString(CharsetUtil.UTF_8));
    BytesReference ref = new ChannelBufferBytesReference(httpResponse.getContent());
    Map<String,Object> map = JsonXContent.jsonXContent.createParser(ref).map();

    logger.info("{}", map);

    InternalSearchResponse internalSearchResponse = parseInternalSearchResponse(map);
    String scrollId = (String)map.get(SCROLL_ID);
    int totalShards = 0;
    int successfulShards = 0;
    if (map.containsKey(SHARDS)) {
        Map<String,?> shards = (Map<String,?>)map.get(SHARDS);
        totalShards =  shards.containsKey(TOTAL) ? (Integer)shards.get(TOTAL) : -1;
        successfulShards =  shards.containsKey(SUCCESSFUL) ? (Integer)shards.get(SUCCESSFUL) : -1;
    }
    int tookInMillis = map.containsKey(TOOK) ? (Integer)map.get(TOOK) : -1;
    ShardSearchFailure[] shardFailures = parseShardFailures(map);
    return new SearchResponse(internalSearchResponse, scrollId, totalShards, successfulShards, tookInMillis, shardFailures);
}
 
Example #6
Source File: HttpSearchAction.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
private InternalSearchResponse parseInternalSearchResponse(Map<String,?> map) {
    InternalSearchHits internalSearchHits = parseInternalSearchHits(map);
    InternalAggregations internalAggregations = parseInternalAggregations(map);
    Suggest suggest = parseSuggest(map);
    InternalProfileShardResults internalProfileShardResults = null;
    Boolean timeout = false;
    Boolean terminatedEarly = false;
    return new InternalSearchResponse(internalSearchHits,
            internalAggregations,
            suggest,
            internalProfileShardResults,
            timeout,
            terminatedEarly);
}
 
Example #7
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInfromation) {
    return new SearchResponse(
            new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 0, 0.0f), new InternalFacets(Collections.EMPTY_LIST), new InternalAggregations(Collections.EMPTY_LIST), new Suggest(), false, false),
            "no-op",
            1,
            1,
            1,
            new ShardSearchFailure[0]
    );
}
 
Example #8
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInfromation) {
    return new SearchResponse(
            new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 0, 0.0f), new InternalFacets(Collections.EMPTY_LIST), new InternalAggregations(Collections.EMPTY_LIST), new Suggest(), false, false),
            "no-op",
            1,
            1,
            1,
            new ShardSearchFailure[0]
    );
}
 
Example #9
Source File: SearchQueryAndFetchAsyncAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void moveToSecondPhase() throws Exception {
    threadPool.executor(ThreadPool.Names.SEARCH).execute(new ActionRunnable<SearchResponse>(listener) {
        @Override
        public void doRun() throws IOException {
            boolean useScroll = request.scroll() != null;
            sortedShardList = searchPhaseController.sortDocs(useScroll, firstResults);
            final InternalSearchResponse internalResponse = searchPhaseController.merge(sortedShardList, firstResults,
                firstResults, request);
            String scrollId = null;
            if (request.scroll() != null) {
                scrollId = TransportSearchHelper.buildScrollId(request.searchType(), firstResults, null);
            }
            listener.onResponse(new SearchResponse(internalResponse, scrollId, expectedSuccessfulOps, successfulOps.get(),
                buildTookInMillis(), buildShardFailures()));
        }

        @Override
        public void onFailure(Throwable t) {
            ReduceSearchPhaseException failure = new ReduceSearchPhaseException("merge", "", t, buildShardFailures());
            if (logger.isDebugEnabled()) {
                logger.debug("failed to reduce search", failure);
            }
            super.onFailure(failure);
        }
    });
}
 
Example #10
Source File: SearchQueryThenFetchAsyncAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void finishHim() {
    threadPool.executor(ThreadPool.Names.SEARCH).execute(new ActionRunnable<SearchResponse>(listener) {
        @Override
        public void doRun() throws IOException {
            final InternalSearchResponse internalResponse = searchPhaseController.merge(sortedShardList, firstResults,
                fetchResults, request);
            String scrollId = null;
            if (request.scroll() != null) {
                scrollId = TransportSearchHelper.buildScrollId(request.searchType(), firstResults, null);
            }
            listener.onResponse(new SearchResponse(internalResponse, scrollId, expectedSuccessfulOps,
                successfulOps.get(), buildTookInMillis(), buildShardFailures()));
            releaseIrrelevantSearchContexts(firstResults, docIdsToLoad);
        }

        @Override
        public void onFailure(Throwable t) {
            try {
                ReduceSearchPhaseException failure = new ReduceSearchPhaseException("fetch", "", t, buildShardFailures());
                if (logger.isDebugEnabled()) {
                    logger.debug("failed to reduce search", failure);
                }
                super.onFailure(failure);
            } finally {
                releaseIrrelevantSearchContexts(firstResults, docIdsToLoad);
            }
        }
    });
}
 
Example #11
Source File: SearchDfsQueryThenFetchAsyncAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void finishHim() {
    threadPool.executor(ThreadPool.Names.SEARCH).execute(new ActionRunnable<SearchResponse>(listener) {
        @Override
        public void doRun() throws IOException {
            final InternalSearchResponse internalResponse = searchPhaseController.merge(sortedShardList, queryResults,
                fetchResults, request);
            String scrollId = null;
            if (request.scroll() != null) {
                scrollId = TransportSearchHelper.buildScrollId(request.searchType(), firstResults, null);
            }
            listener.onResponse(new SearchResponse(internalResponse, scrollId, expectedSuccessfulOps, successfulOps.get(),
                buildTookInMillis(), buildShardFailures()));
            releaseIrrelevantSearchContexts(queryResults, docIdsToLoad);
        }

        @Override
        public void onFailure(Throwable t) {
            try {
                ReduceSearchPhaseException failure = new ReduceSearchPhaseException("merge", "", t, buildShardFailures());
                if (logger.isDebugEnabled()) {
                    logger.debug("failed to reduce search", failure);
                }
                super.onFailure(failure);
            } finally {
                releaseIrrelevantSearchContexts(queryResults, docIdsToLoad);
            }
        }
    });
}
 
Example #12
Source File: SearchScrollQueryThenFetchAsyncAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void innerFinishHim() {
    InternalSearchResponse internalResponse = searchPhaseController.merge(sortedShardList, queryResults, fetchResults, request);
    String scrollId = null;
    if (request.scroll() != null) {
        scrollId = request.scrollId();
    }
    listener.onResponse(new SearchResponse(internalResponse, scrollId, this.scrollId.getContext().length, successfulOps.get(),
        buildTookInMillis(), buildShardFailures()));
}
 
Example #13
Source File: SearchCountAsyncAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void moveToSecondPhase() throws Exception {
    // no need to sort, since we know we have no hits back
    final InternalSearchResponse internalResponse = searchPhaseController.merge(SearchPhaseController.EMPTY_DOCS, firstResults,
            (AtomicArray<? extends FetchSearchResultProvider>) AtomicArray.empty(), request);
    String scrollId = null;
    if (request.scroll() != null) {
        scrollId = TransportSearchHelper.buildScrollId(request.searchType(), firstResults, null);
    }
    listener.onResponse(new SearchResponse(internalResponse, scrollId, expectedSuccessfulOps, successfulOps.get(), buildTookInMillis(), buildShardFailures()));
}
 
Example #14
Source File: SearchScrollQueryAndFetchAsyncAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void innerFinishHim() throws Exception {
    ScoreDoc[] sortedShardList = searchPhaseController.sortDocs(true, queryFetchResults);
    final InternalSearchResponse internalResponse = searchPhaseController.merge(sortedShardList, queryFetchResults,
        queryFetchResults, request);
    String scrollId = null;
    if (request.scroll() != null) {
        scrollId = request.scrollId();
    }
    listener.onResponse(new SearchResponse(internalResponse, scrollId, this.scrollId.getContext().length, successfulOps.get(),
        buildTookInMillis(), buildShardFailures()));
}
 
Example #15
Source File: SearchResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public SearchResponse(InternalSearchResponse internalResponse, String scrollId, int totalShards, int successfulShards, long tookInMillis, ShardSearchFailure[] shardFailures) {
    this.internalResponse = internalResponse;
    this.scrollId = scrollId;
    this.totalShards = totalShards;
    this.successfulShards = successfulShards;
    this.tookInMillis = tookInMillis;
    this.shardFailures = shardFailures;
}
 
Example #16
Source File: SearchScanAsyncAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void moveToSecondPhase() throws Exception {
    final InternalSearchResponse internalResponse = searchPhaseController.merge(SearchPhaseController.EMPTY_DOCS, firstResults,
            (AtomicArray<? extends FetchSearchResultProvider>) AtomicArray.empty(), request);
    String scrollId = null;
    if (request.scroll() != null) {
        scrollId = TransportSearchHelper.buildScrollId(request.searchType(), firstResults, ImmutableMap.of("total_hits", Long.toString
                (internalResponse.hits().totalHits())));
    }
    listener.onResponse(new SearchResponse(internalResponse, scrollId, expectedSuccessfulOps, successfulOps.get(), buildTookInMillis(), buildShardFailures()));
}
 
Example #17
Source File: QueryProcessTest.java    From elasticsearch-reindex-tool with Apache License 2.0 4 votes vote down vote up
private SearchResponse createSearchResponseWithScrollId(String scrollId) {
  return new SearchResponse(InternalSearchResponse.empty(), scrollId, 1, 1, 1, new ShardSearchFailure[0]);
}