Example usage for org.apache.solr.common.params FacetParams FACET_DATE_OTHER

List of usage examples for org.apache.solr.common.params FacetParams FACET_DATE_OTHER

Introduction

In this page you can find the example usage for org.apache.solr.common.params FacetParams FACET_DATE_OTHER.

Prototype

String FACET_DATE_OTHER

To view the source code for org.apache.solr.common.params FacetParams FACET_DATE_OTHER.

Click Source Link

Document

String indicating what "other" ranges should be computed for a date facet range (multi-value).

Usage

From source file:org.dspace.app.xmlui.aspect.discovery.AbstractFiltersTransformer.java

License:Open Source License

protected SolrQuery prepareDefaultFilters(String scope) {

    queryArgs = new SolrQuery();

    /* Get Dynamic list of Facets off Request *
    for(String facet : getFacetsList())//  ww w.j a v  a  2 s .  c  om
    {
    queryArgs.addFacetField(facet);
    int max = ConfigurationManager.getIntProperty("search.facet.max", 10) + 1;
    //Set the default limit to 11
    queryArgs.setFacetLimit(max);
    queryArgs.setFacetMinCount(1);
    queryArgs.setFacet(true);
    }                                     */

    String[] facets = SearchUtils.getFacetsForType(scope);
    String[] dateFacets = SearchUtils.getDateFacetsForType(scope);

    log.debug("facets for scope '" + scope + "': " + (facets != null ? facets.length : null));
    log.debug("date facets for scope, " + scope + ": " + (dateFacets != null ? dateFacets.length : null));

    if (facets != null || dateFacets != null) {
        int max = ConfigurationManager.getIntProperty("search.facet.max", 10) + 1;
        //Set the default limit to 11
        queryArgs.setFacetLimit(max);
        queryArgs.setFacetMinCount(1);
        queryArgs.setFacet(true);
    }

    /** enable faceting of search results */
    if (facets != null)
        queryArgs.addFacetField(facets);

    /** add any dateFacets configured */
    if (dateFacets != null && 0 < dateFacets.length) {
        queryArgs.setParam(FacetParams.FACET_DATE, dateFacets);
        queryArgs.setParam(FacetParams.FACET_DATE_GAP, "+1YEAR");
        queryArgs.setParam(FacetParams.FACET_DATE_START,
                "NOW/YEAR-" + SearchUtils.getConfig().getString("solr.date.gap", "10") + "YEARS");
        queryArgs.setParam(FacetParams.FACET_DATE_END, "NOW");
        queryArgs.setParam(FacetParams.FACET_DATE_OTHER, FacetParams.FacetDateOther.ALL.toString());
    }

    //Add the default filters
    queryArgs.addFilterQuery(SearchUtils.getDefaultFilters(scope));

    return queryArgs;
}

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

License:Open Source License

/**
 * @deprecated Use getFacetRangeCounts which is more generalized
 *//*from   w w  w . j a va 2  s  . com*/
@Deprecated
public void getFacetDateCounts(String dateFacet, NamedList<Object> resOuter)
        throws IOException, ParseException, RepositoryException, JahiaException {

    parseParams(FacetParams.FACET_DATE, dateFacet);
    String f = facetValue;

    final NamedList<Object> resInner = new SimpleOrderedMap<Object>();
    String fieldName = StringUtils.substringBeforeLast(f, PROPNAME_INDEX_SEPARATOR);
    ExtendedPropertyDefinition epd = NodeTypeRegistry.getInstance()
            .getNodeType(params.get("f." + f + ".facet.nodetype")).getPropertyDefinition(fieldName);
    String fieldNameInIndex = getFieldNameInIndex(f, fieldName, epd, params.getFieldParam(f, "facet.locale"));
    String prefix = params.getFieldParam(f, FacetParams.FACET_PREFIX);
    DateField ft = StringUtils.isEmpty(prefix) ? JahiaQueryParser.DATE_TYPE : JahiaQueryParser.JR_DATE_TYPE;
    final SchemaField sf = new SchemaField(fieldNameInIndex, ft);

    // TODO: Should we use the key now ?
    //    resOuter.add(key, resInner);
    resOuter.add(fieldName + PROPNAME_INDEX_SEPARATOR + fieldNameInIndex, resInner);

    if (!(epd.getRequiredType() == PropertyType.DATE)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Can not date facet on a field which is not a DateField: " + f);
    }
    Integer minCount = params.getFieldInt(f, FacetParams.FACET_MINCOUNT);
    if (minCount == null) {
        Boolean zeros = params.getFieldBool(f, FacetParams.FACET_ZEROS);
        // mincount = (zeros!=null && zeros) ? 0 : 1;
        minCount = (zeros != null && !zeros) ? 1 : 0;
        // current default is to include zeros.
    }

    final String startS = required.getFieldParam(f, FacetParams.FACET_DATE_START);
    final Date start;
    try {
        start = JahiaQueryParser.DATE_TYPE.parseMath(NOW, startS);
    } catch (SolrException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'start' is not a valid Date string: " + startS, e);
    }
    final String endS = required.getFieldParam(f, FacetParams.FACET_DATE_END);
    Date end; // not final, hardend may change this
    try {
        end = JahiaQueryParser.DATE_TYPE.parseMath(NOW, endS);
    } catch (SolrException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'end' is not a valid Date string: " + endS, e);
    }

    if (end.before(start)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'end' comes before 'start': " + endS + " < " + startS);
    }

    final String gap = required.getFieldParam(f, FacetParams.FACET_DATE_GAP);
    final DateMathParser dmp = new DateMathParser(DateField.UTC, Locale.US);
    dmp.setNow(NOW);

    String[] iStrs = params.getFieldParams(f, FacetParams.FACET_DATE_INCLUDE);
    // Legacy support for default of [lower,upper,edge] for date faceting
    // this is not handled by FacetRangeInclude.parseParam because
    // range faceting has differnet defaults
    final EnumSet<FacetRangeInclude> include = (null == iStrs || 0 == iStrs.length)
            ? EnumSet.of(FacetRangeInclude.LOWER, FacetRangeInclude.UPPER, FacetRangeInclude.EDGE)
            : FacetRangeInclude.parseParam(iStrs);

    try {
        Date low = start;
        while (low.before(end)) {
            dmp.setNow(low);
            String label = JahiaQueryParser.DATE_TYPE.toExternal(low);

            Date high = dmp.parseMath(gap);
            if (end.before(high)) {
                if (params.getFieldBool(f, FacetParams.FACET_DATE_HARD_END, false)) {
                    high = end;
                } else {
                    end = high;
                }
            }
            if (high.before(low)) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "date facet infinite loop (is gap negative?)");
            }
            final boolean includeLower = (include.contains(FacetRangeInclude.LOWER)
                    || (include.contains(FacetRangeInclude.EDGE) && low.equals(start)));
            final boolean includeUpper = (include.contains(FacetRangeInclude.UPPER)
                    || (include.contains(FacetRangeInclude.EDGE) && high.equals(end)));

            Query rangeQuery = getRangeQuery(ft, null, sf, prefix, low, high, includeLower, includeUpper);

            int count = rangeCount(rangeQuery);
            if (count >= minCount) {
                // TODO: Can we use just label here ?                  
                resInner.add(label + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(), count);
            }
            low = high;
        }
    } catch (java.text.ParseException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "date facet 'gap' is not a valid Date Math string: " + gap, e);
    }

    // explicitly return the gap and end so all the counts are meaningful
    resInner.add("gap", gap);
    resInner.add("start", start);
    resInner.add("end", end);

    final String[] othersP = params.getFieldParams(f, FacetParams.FACET_DATE_OTHER);
    if (null != othersP && 0 < othersP.length) {
        final Set<FacetDateOther> others = EnumSet.noneOf(FacetDateOther.class);

        for (final String o : othersP) {
            others.add(FacetDateOther.get(o));
        }

        // no matter what other values are listed, we don't do
        // anything if "none" is specified.
        if (!others.contains(FacetDateOther.NONE)) {
            boolean all = others.contains(FacetDateOther.ALL);

            if (all || others.contains(FacetDateOther.BEFORE)) {
                Query rangeQuery = getRangeQuery(ft, null, sf, prefix, null, start, false,
                        (include.contains(FacetRangeInclude.OUTER)
                                || (!(include.contains(FacetRangeInclude.LOWER)
                                        || include.contains(FacetRangeInclude.EDGE)))));
                int count = rangeCount(rangeQuery);
                if (count >= minCount) {
                    resInner.add(
                            FacetDateOther.BEFORE.toString() + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(),
                            count);
                }
            }
            if (all || others.contains(FacetDateOther.AFTER)) {
                Query rangeQuery = getRangeQuery(ft, null, sf, prefix, end, null,
                        (include.contains(FacetRangeInclude.OUTER)
                                || (!(include.contains(FacetRangeInclude.UPPER)
                                        || include.contains(FacetRangeInclude.EDGE)))),
                        false);
                int count = rangeCount(rangeQuery);
                if (count >= minCount) {
                    resInner.add(
                            FacetDateOther.AFTER.toString() + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(),
                            count);
                }
            }
            if (all || others.contains(FacetDateOther.BETWEEN)) {
                Query rangeQuery = getRangeQuery(ft, null, sf, prefix, start, end,
                        (include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE)),
                        (include.contains(FacetRangeInclude.UPPER)
                                || include.contains(FacetRangeInclude.EDGE)));
                int count = rangeCount(rangeQuery);
                if (count >= minCount) {
                    resInner.add(FacetDateOther.BETWEEN.toString() + PROPNAME_INDEX_SEPARATOR
                            + rangeQuery.toString(), count);
                }
            }
        }
    }
}