001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.elasticsearch.api;
020
021import org.elasticsearch.action.search.SearchResponse;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.core.api.IterableQueryResult;
024import org.nuxeo.elasticsearch.query.NxQueryBuilder;
025
026/**
027 * Wrapper for the results of a scrollable search request.
028 *
029 * @see ElasticSearchService#scroll(NxQueryBuilder, long, int)
030 * @see ElasticSearchService#scanAndScroll(NxQueryBuilder, long, int)
031 * @see ElasticSearchService#scroll(EsScrollResult)
032 * @since 8.3
033 */
034public class EsScrollResult extends EsResult {
035
036    /**
037     * {@link NxQueryBuilder} used for the initial search request.
038     */
039    protected final NxQueryBuilder queryBuilder;
040
041    /**
042     * Scroll id returned by the search request.
043     */
044    protected final String scrollId;
045
046    /**
047     * Timeout for keeping the search context alive.
048     */
049    protected final long keepAlive;
050
051    public EsScrollResult(DocumentModelList documents, SearchResponse response, NxQueryBuilder queryBuilder,
052            String scrollId, long keepAlive) {
053        super(documents, null, response);
054        this.queryBuilder = queryBuilder;
055        this.scrollId = scrollId;
056        this.keepAlive = keepAlive;
057    }
058
059    public EsScrollResult(IterableQueryResult rows, SearchResponse response, NxQueryBuilder queryBuilder,
060            String scrollId, long keepAlive) {
061        super(rows, null, response);
062        this.queryBuilder = queryBuilder;
063        this.scrollId = scrollId;
064        this.keepAlive = keepAlive;
065    }
066
067    public EsScrollResult(SearchResponse response, NxQueryBuilder queryBuilder, String scrollId, long keepAlive) {
068        super(response);
069        this.queryBuilder = queryBuilder;
070        this.scrollId = scrollId;
071        this.keepAlive = keepAlive;
072    }
073
074    public EsScrollResult(NxQueryBuilder queryBuilder, String scrollId, long keepAlive) {
075        this(null, queryBuilder, scrollId, keepAlive);
076    }
077
078    public NxQueryBuilder getQueryBuilder() {
079        return queryBuilder;
080    }
081
082    public String getScrollId() {
083        return scrollId;
084    }
085
086    public long getKeepAlive() {
087        return keepAlive;
088    }
089
090}