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 *     bdelbosc
016 */
017package org.nuxeo.elasticsearch.fetcher;
018
019import java.util.Map;
020
021import org.elasticsearch.action.search.SearchResponse;
022import org.elasticsearch.search.SearchHit;
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
026import org.nuxeo.elasticsearch.io.DocumentModelReaders;
027
028/**
029 * @since 6.0
030 */
031public class EsFetcher extends Fetcher {
032
033    public EsFetcher(CoreSession session, SearchResponse response, Map<String, String> repoNames) {
034        super(session, response, repoNames);
035    }
036
037    @Override
038    public DocumentModelListImpl fetchDocuments() {
039        DocumentModelListImpl ret = new DocumentModelListImpl(getResponse().getHits().getHits().length);
040        DocumentModel doc;
041        String sid = getSession().getSessionId();
042        for (SearchHit hit : getResponse().getHits()) {
043            // TODO: this does not work on multi repo
044            doc = DocumentModelReaders.fromSource(hit.getSource()).sid(sid).getDocumentModel();
045            ret.add(doc);
046        }
047        return ret;
048    }
049}