Search in sources :

Example 1 with NO_LIMIT

use of org.graylog2.indexer.searches.ScrollCommand.NO_LIMIT in project graylog2-server by Graylog2.

the class ScrollResultES6 method nextChunk.

@Override
public ScrollChunk nextChunk() throws IOException {
    if (limit != NO_LIMIT && resultCount >= limit) {
        LOG.debug("[{}] Reached limit for query {}", queryHash, getOriginalQuery());
        return null;
    }
    final JestResult search;
    final List<ResultMessage> resultMessages;
    if (initialResult == null) {
        search = getNextScrollResult();
        resultMessages = StreamSupport.stream(search.getJsonObject().path("hits").path("hits").spliterator(), false).map(hit -> ResultMessage.parseFromSource(hit.path("_id").asText(), hit.path("_index").asText(), objectMapper.convertValue(hit.get("_source"), TypeReferences.MAP_STRING_OBJECT))).collect(Collectors.toList());
    } else {
        // make sure to return the initial resultMessages, see https://github.com/Graylog2/graylog2-server/issues/2126
        search = initialResult;
        resultMessages = initialResult.getHits(Map.class, false).stream().map(hit -> ResultMessage.parseFromSource(hit.id, hit.index, (Map<String, Object>) hit.source)).collect(Collectors.toList());
        this.initialResult = null;
    }
    if (resultMessages.size() == 0) {
        // scroll exhausted
        LOG.debug("[{}] Reached end of scroll results for query {}", queryHash, getOriginalQuery());
        return null;
    }
    final int remainingResultsForLimit = limit - resultCount;
    final List<ResultMessage> resultMessagesSlice = (limit != NO_LIMIT && remainingResultsForLimit < resultMessages.size()) ? resultMessages.subList(0, remainingResultsForLimit) : resultMessages;
    resultCount += resultMessagesSlice.size();
    LOG.debug("[{}][{}] New scroll id {}, number of resultMessages in chunk: {}", queryHash, chunkId, getScrollIdFromResult(search), resultMessagesSlice.size());
    // save the id for the next request.
    scrollId = getScrollIdFromResult(search);
    return new ScrollChunkES6(resultMessagesSlice, fields, chunkId++);
}
Also used : ResultMessage(org.graylog2.indexer.results.ResultMessage) Map(java.util.Map) JestResult(io.searchbox.client.JestResult)

Aggregations

JestResult (io.searchbox.client.JestResult)1 Map (java.util.Map)1 ResultMessage (org.graylog2.indexer.results.ResultMessage)1