All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polyglotted.eswrapper.query.ResultBuilder Maven / Gradle / Ivy

There is a newer version: 2.4.3
Show newest version
package io.polyglotted.eswrapper.query;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import io.polyglotted.pgmodel.search.IndexKey;
import io.polyglotted.pgmodel.search.SimpleDoc;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;

import java.util.List;
import java.util.Map;

import static com.google.common.collect.ImmutableMap.copyOf;
import static com.google.common.collect.ImmutableMap.of;
import static com.google.common.collect.Iterables.transform;
import static io.polyglotted.eswrapper.query.ModelQueryUtil.keyFrom;

public interface ResultBuilder {

    default List buildFrom(SearchResponse response) {
        return ImmutableList.copyOf(transform(response.getHits(), hit -> {
            ImmutableMap source = hit.isSourceEmpty() ? of() : copyOf(hit.getSource());
            return buildResult(keyFrom(hit), source);
        }));
    }

    default T buildFrom(GetResponse response) {
        ImmutableMap source = response.isSourceEmpty() ? of() : copyOf(response.getSourceAsMap());
        return buildResult(keyFrom(response), source);
    }

    T buildResult(IndexKey key, ImmutableMap source);

    ResultBuilder NullBuilder = (key, source) -> ImmutableMap.of();

    ResultBuilder IndexKeyBuilder = (key, source) -> key;

    ResultBuilder> SourceBuilder = (key, source) -> source;

    ResultBuilder SimpleDocBuilder = SimpleDoc::new;

    static  ResultBuilder SimpleObjectBuilder(final Gson gson, Class clazz) {
        return (key, source) -> source.isEmpty() ? null : gson.fromJson(gson.toJson(source), clazz);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy