001/*
002 * (C) Copyright 2016 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 *     Funsho David
018 */
019
020package org.nuxeo.elasticsearch.io;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024
025import java.io.IOException;
026import java.util.List;
027import java.util.Map;
028
029import org.codehaus.jackson.JsonGenerator;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
032import org.nuxeo.ecm.core.io.registry.reflect.Setup;
033import org.nuxeo.ecm.platform.query.api.PageProvider;
034
035/**
036 * @since 9.1
037 */
038@Setup(mode = SINGLETON, priority = REFERENCE)
039public class HighlightJsonEnricher extends AbstractJsonEnricher<DocumentModel> {
040
041    public HighlightJsonEnricher() {
042        super(PageProvider.HIGHLIGHT_CTX_DATA);
043    }
044
045    @Override
046    public void write(JsonGenerator jg, DocumentModel document) throws IOException {
047        jg.writeArrayFieldStart(PageProvider.HIGHLIGHT_CTX_DATA);
048        Map<String, List<String>> h = (Map<String, List<String>>) document.getContextData(
049                PageProvider.HIGHLIGHT_CTX_DATA);
050        if (h != null) {
051            for (String field : h.keySet()) {
052                jg.writeStartObject();
053                jg.writeStringField("field", field);
054                jg.writeObjectField("segments", h.get(field));
055                jg.writeEndObject();
056            }
057        }
058        jg.writeEndArray();
059    }
060}