001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Benoit Delbosc
016 */
017package org.nuxeo.elasticsearch.api;
018
019import java.util.List;
020
021import org.elasticsearch.action.search.SearchResponse;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.core.api.IterableQueryResult;
024import org.nuxeo.ecm.platform.query.api.Aggregate;
025
026
027/**
028 * @since 6.0
029 */
030public class EsResult {
031    private final DocumentModelList documents;
032
033    private final IterableQueryResult rows;
034
035    private final List<Aggregate> aggregates;
036
037    private final SearchResponse response;
038
039    public EsResult(DocumentModelList documents, List<Aggregate> aggregates, SearchResponse response) {
040        this.documents = documents;
041        this.rows = null;
042        this.aggregates = aggregates;
043        this.response = response;
044    }
045
046    public EsResult(IterableQueryResult rows, List<Aggregate> aggregates, SearchResponse response) {
047        this.documents = null;
048        this.rows = rows;
049        this.aggregates = aggregates;
050        this.response = response;
051    }
052
053    public EsResult(SearchResponse response) {
054        this.documents = null;
055        this.rows = null;
056        this.aggregates = null;
057        this.response = response;
058    }
059
060    /**
061     * Get the list of Nuxeo documents, this is populated when using a SELECT * clause, or when submitting esQuery.
062     *
063     * @return null if the query returns fields or if the onlyElasticsearchResponse option is set.
064     */
065    public DocumentModelList getDocuments() {
066        return documents;
067    }
068
069    /**
070     * Iterator to use when selecting fields: SELECT ecm:uuid ...
071     *
072     * @since 7.2
073     * @return null if the query returns documents or if the onlyElasticsearchResponse option is set.
074     */
075    public IterableQueryResult getRows() {
076        return rows;
077    }
078
079    /**
080     * Get the aggretages list or null if onlyElasticsearchResponse option is set.
081     */
082    public List<Aggregate> getAggregates() {
083        return aggregates;
084    }
085
086    /**
087     * Returns the original Elasticsearch response.
088     *
089     * Use it at your own risk
090     * @since 7.3
091     */
092    public SearchResponse getElasticsearchResponse() {
093        return response;
094    }
095}