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 *     Kevin Leturc
018 */
019package org.nuxeo.elasticsearch.core;
020
021import java.io.Serializable;
022import java.util.Arrays;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.stream.Collectors;
027
028import org.elasticsearch.search.SearchHit;
029import org.elasticsearch.search.SearchHitField;
030import org.nuxeo.ecm.core.query.sql.NXQL;
031import org.nuxeo.ecm.core.schema.types.Type;
032import org.nuxeo.ecm.core.schema.types.primitives.DateType;
033
034/**
035 * Converter used to convert a {@link SearchHit} to a {@link Map}<{@link String}, {@link Serializable}>.
036 *
037 * @since 8.4
038 */
039public class EsSearchHitConverter {
040
041    private final Map<String, Type> selectFieldsAndTypes;
042
043    private final Map<String, Serializable> emptyRow;
044
045    public EsSearchHitConverter(Map<String, Type> selectFieldsAndTypes) {
046        this.selectFieldsAndTypes = selectFieldsAndTypes;
047        this.emptyRow = buildEmptyRow(selectFieldsAndTypes);
048    }
049
050    public List<Map<String, Serializable>> convert(SearchHit... hits) {
051        return Arrays.stream(hits).map(this::convert).collect(Collectors.toList());
052    }
053
054    public Map<String, Serializable> convert(SearchHit hit) {
055        Map<String, Serializable> row = new HashMap<>(emptyRow);
056        for (SearchHitField field : hit.getFields().values()) {
057            String name = field.getName();
058            Serializable value = field.<Serializable> getValue();
059            // type conversion
060            Type type;
061            if (value instanceof String && (type = selectFieldsAndTypes.get(name)) instanceof DateType) {
062                // convert back to calendar
063                value = (Serializable) type.decode(((String) value));
064            }
065            row.put(name, value);
066        }
067        if (selectFieldsAndTypes.containsKey(NXQL.ECM_FULLTEXT_SCORE)) {
068            row.put(NXQL.ECM_FULLTEXT_SCORE, Double.valueOf(hit.getScore()));
069        }
070        return row;
071    }
072
073    private static Map<String, Serializable> buildEmptyRow(Map<String, Type> selectFieldsAndTypes) {
074        Map<String, Serializable> emptyRow = new HashMap<>(selectFieldsAndTypes.size());
075        for (String fieldName : selectFieldsAndTypes.keySet()) {
076            emptyRow.put(fieldName, null);
077        }
078        return emptyRow;
079    }
080
081}