001/*
002 * (C) Copyright 2014 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 *     bdelbosc
018 */
019package org.nuxeo.elasticsearch.fetcher;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.elasticsearch.action.search.SearchResponse;
028import org.elasticsearch.common.text.Text;
029import org.elasticsearch.search.SearchHit;
030import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
034import org.nuxeo.ecm.platform.query.api.PageProvider;
035import org.nuxeo.elasticsearch.io.DocumentModelReaders;
036
037/**
038 * @since 6.0
039 */
040public class EsFetcher extends Fetcher {
041
042    public EsFetcher(CoreSession session, SearchResponse response, Map<String, String> repoNames) {
043        super(session, response, repoNames);
044    }
045
046    @Override
047    public DocumentModelListImpl fetchDocuments() {
048        DocumentModelListImpl ret = new DocumentModelListImpl(getResponse().getHits().getHits().length);
049        DocumentModel doc;
050        String sid = getSession().getSessionId();
051        for (SearchHit hit : getResponse().getHits()) {
052            // TODO: this does not work on multi repo
053            doc = DocumentModelReaders.fromSource(hit.getSource()).sid(sid).getDocumentModel();
054            // Add highlight if it exists
055            Map<String, HighlightField> esHighlights = hit.getHighlightFields();
056            if (!esHighlights.isEmpty()) {
057                Map<String, List<String>> fields = new HashMap<>();
058                for (Map.Entry<String, HighlightField> entry : esHighlights.entrySet()) {
059                    String field = entry.getKey();
060                    List<String> list = new ArrayList<>();
061                    for (Text fragment : entry.getValue().getFragments()) {
062                        list.add(fragment.toString());
063                    }
064                    fields.put(field, list);
065                }
066                doc.putContextData(PageProvider.HIGHLIGHT_CTX_DATA, (Serializable) fields);
067            }
068            ret.add(doc);
069        }
070        return ret;
071    }
072}