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

com.rbmhtechnology.vind.api.result.FacetResults Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.rbmhtechnology.vind.api.result;

import com.rbmhtechnology.vind.api.result.facet.*;
import com.rbmhtechnology.vind.model.DocumentFactory;
import com.rbmhtechnology.vind.model.FieldDescriptor;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Class to store the facet query results.
 * @author Thomas Kurz ([email protected])
 * @since 27.06.16.
 */
public class FacetResults {

    private final TermFacetResult typeFacet;
    private HashMap> termFacets;
    private HashMap> queryFacets;
    private HashMap> rangeFacets;
    private HashMap intervalFacets;
    private HashMap> statsFacets;
    private HashMap>> pivotFacets;
    private Collection subdocumentFacets;

    private DocumentFactory factory;

    /**
     * Creates a new instance of {@link FacetResults}.
     * @param factory document factory configured with a document schema.
     * @param termFacets term facet query results.
     * @param typeFacet type facet query results.
     * @param queryFacets query facet query results.
     * @param rangeFacets range facet query results.
     * @param statsFacets statistics facet query results.
     * @param pivotFacets picot facet query results.
     */
    public FacetResults(DocumentFactory factory,
                        HashMap> termFacets,
                        TermFacetResult typeFacet,
                        HashMap> queryFacets,
                        HashMap> rangeFacets,
                        HashMap intervalFacets,
                        HashMap> statsFacets,
                        HashMap>> pivotFacets,
                        Collection subDocumentFacets) {
        this.factory = factory;
        this.termFacets = termFacets;
        this.typeFacet = typeFacet;
        this.queryFacets = queryFacets;
        this.rangeFacets = rangeFacets;
        this.intervalFacets = intervalFacets;
        this.statsFacets = statsFacets;
        this.pivotFacets = pivotFacets;
        this.subdocumentFacets = subDocumentFacets;
    }

    /**
     * Gets the term facet results for a given field.
     * @param descriptor Field descriptor.
     * @param  Field descriptor type.
     * @return a {@link TermFacetResult} with the facets of the given field.
     */
    public  TermFacetResult getTermFacet(FieldDescriptor descriptor) {
        if(termFacets.containsKey(descriptor)) {
            return (TermFacetResult) termFacets.get(descriptor); //TODO
        } else return null;
    }

    /**
     * Gets the term facet results for a given field.
     * @param name String name of the field.
     * @param t Class type stored by the field.
     * @param  Field descriptor type.
     * @return A {@link TermFacetResult} with a set of facets of type T and the count of documents matching each one.
     */
    public  TermFacetResult getTermFacet(String name, Class t) {
        final FieldDescriptor descriptor = factory.getField(name);
        if(descriptor != null) {
            final Object ret = getTermFacet(descriptor);
            return ret != null ? (TermFacetResult) ret : null;
        }
        else return null;
    }

    /**
     * Gets the type facet results for a given field.
     * @return A {@link TermFacetResult} with a set of facets of type T and the count of documents matching each one.
     */
    public TermFacetResult getTypeFacet() {
        final Object ret = this.typeFacet;
        return Objects.nonNull(ret)? (TermFacetResult) ret : null;
    }

    /**
     * Gets a query facet query result by facet name.
     * @param name String name of the query facet.
     * @param t Class type stored by the field.
     * @param  Field descriptor type.
     * @return A {@link QueryFacetResult}.
     */
    public  QueryFacetResult getQueryFacet(String name, Class t) {
        return queryFacets.containsKey(name) ? (QueryFacetResult) queryFacets.get(name) : null; //TODO
    }

    /**
     * Gets a range facet query result by facet name.
     * @param name String name of the range facet.
     * @param t Class type stored by the field.
     * @param  Field descriptor type.
     * @return A {@link RangeFacetResult}.
     */
    public  RangeFacetResult getRangeFacet(String name, Class t) {
        return rangeFacets.containsKey(name) ? (RangeFacetResult) rangeFacets.get(name) : null; //TODO
    }

    /**
     * Gets a interval facet query result by facet name.
     * @param name String name of the range facet.
     * @return A {@link IntervalFacetResult}.
     */
    public IntervalFacetResult getIntervalFacet(String name) { //TODO add type
        return intervalFacets.containsKey(name) ? intervalFacets.get(name) : null; //TODO
    }

    /**
     * Gets a stats facet query result by facet name.
     * @param name String name of the stats facet.
     * @param t Class type stored by the field.
     * @param  Field descriptor type.
     * @return A {@link StatsFacetResult}.
     */
    public  StatsFacetResult getStatsFacet(String name, Class t) {
        return statsFacets.containsKey(name) ? (StatsFacetResult) statsFacets.get(name) : null; //TODO
    }
    /**
     * Gets a pivot facet query results by facet name.
     * @param name String name of the pivot facet.
     * @param t Class type stored by the field.
     * @param  Field descriptor type.
     * @return A list of {@link PivotFacetResult}.
     */
    public  List> getPivotsFacet(String name, Class t) {
        return pivotFacets.containsKey(name) ? pivotFacets.get(name).stream()
                                                .map(pivot->(PivotFacetResult)pivot)
                                                .collect(Collectors.toList()) : null; //TODO
    }

    /**
     * Get all the term facet results. This method is not typesave so please use it with care OR use @this#
     * @return a map of field descriptor with its term facets results.
     */
    public HashMap> getTermFacets() {
        return termFacets;
    }

    /**
     * Gets all the query facet results.
     * @return A map of name of facet and query facet result.
     */
    public HashMap> getQueryFacets() {
        return queryFacets;
    }
    /**
     * Gets all the range facet results.
     * @return A map of name of facet and range facet result.
     */
    public HashMap> getRangeFacets() {
        return rangeFacets;
    }
    /**
     * Gets all the stats facet results.
     * @return A map of name of facet an stats facet result.
     */
    public HashMap> getStatsFacets() {
        return statsFacets;
    }
    /**
     * Gets all the pivot facet results.
     * @return A map of name of facet an pivot facet result.
     */
    public HashMap>> getPivotFacets() {
        return pivotFacets;
    }

    public Collection getSubdocumentFacets() {
        return subdocumentFacets;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy