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, HitDocConsumer consumer) {
054        super(session, response, repoNames);
055        this.consumer = consumer;
056    }
057
058    @Override
059    public DocumentModelListImpl fetchDocuments() {
060        DocumentModelListImpl ret = new DocumentModelListImpl(getResponse().getHits().getHits().length);
061        DocumentModel doc;
062        String sid = getSession().getSessionId();
063        for (SearchHit hit : getResponse().getHits()) {
064            // TODO: this does not work on multi repo
065            doc = DocumentModelReaders.fromSource(hit.getSourceAsMap()).sid(sid).getDocumentModel();
066
067            if (doc != null && consumer != null) {
068                consumer.accept(hit, doc);
069            }
070
071            // Add highlight if it exists
072            Map<String, HighlightField> esHighlights = hit.getHighlightFields();
073            if (!esHighlights.isEmpty()) {
074                Map<String, List<String>> fields = new HashMap<>();
075                for (Map.Entry<String, HighlightField> entry : esHighlights.entrySet()) {
076                    String field = entry.getKey();
077                    List<String> list = new ArrayList<>();
078                    for (Text fragment : entry.getValue().getFragments()) {
079                        list.add(fragment.toString());
080                    }
081                    fields.put(field, list);
082                }
083                doc.putContextData(PageProvider.HIGHLIGHT_CTX_DATA, (Serializable) fields);
084            }
085            ret.add(doc);
086        }
087        return ret;
088    }
089
090    /**
091     * Consumes both a SearchHit and DocumentModel.
092     * @since 10.2
093     */
094    @FunctionalInterface
095    public interface HitDocConsumer extends BiConsumer<SearchHit, DocumentModel> {
096
097    }
098}