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;
026import java.util.function.BiConsumer;
027
028import org.elasticsearch.action.search.SearchResponse;
029import org.elasticsearch.common.text.Text;
030import org.elasticsearch.search.SearchHit;
031import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
035import org.nuxeo.ecm.platform.query.api.PageProvider;
036import org.nuxeo.elasticsearch.io.DocumentModelReaders;
037
038/**
039 * @since 6.0
040 */
041public class EsFetcher extends Fetcher {
042
043    protected final HitDocConsumer consumer;
044
045    public EsFetcher(CoreSession session, SearchResponse response, Map<String, String> repoNames) {
046        super(session, response, repoNames);
047        this.consumer = null;
048    }
049
050    /**
051     * @since 10.2
052     */
053    public EsFetcher(CoreSession session, SearchResponse response, Map<String, String> repoNames,
054            HitDocConsumer consumer) {
055        super(session, response, repoNames);
056        this.consumer = consumer;
057    }
058
059    @Override
060    public DocumentModelListImpl fetchDocuments() {
061        DocumentModelListImpl ret = new DocumentModelListImpl(getResponse().getHits().getHits().length);
062        DocumentModel doc;
063        String sid = getSession().getSessionId();
064        for (SearchHit hit : getResponse().getHits()) {
065            // TODO: this does not work on multi repo
066            doc = DocumentModelReaders.fromSource(hit.getSourceAsMap()).sid(sid).getDocumentModel();
067
068            if (doc != null && consumer != null) {
069                consumer.accept(hit, doc);
070            }
071
072            // Add highlight if it exists
073            Map<String, HighlightField> esHighlights = hit.getHighlightFields();
074            if (!esHighlights.isEmpty()) {
075                Map<String, List<String>> fields = new HashMap<>();
076                for (Map.Entry<String, HighlightField> entry : esHighlights.entrySet()) {
077                    String field = entry.getKey();
078                    List<String> list = new ArrayList<>();
079                    for (Text fragment : entry.getValue().getFragments()) {
080                        list.add(fragment.toString());
081                    }
082                    fields.put(field, list);
083                }
084                doc.putContextData(PageProvider.HIGHLIGHT_CTX_DATA, (Serializable) fields);
085            }
086            ret.add(doc);
087        }
088        return ret;
089    }
090
091    /**
092     * Consumes both a SearchHit and DocumentModel.
093     *
094     * @since 10.2
095     */
096    @FunctionalInterface
097    public interface HitDocConsumer extends BiConsumer<SearchHit, DocumentModel> {
098
099    }
100}