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

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

Introduction

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

Prototype

String FACET_RANGE_INCLUDE

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

Click Source Link

Document

Multivalued string indicating what rules should be applied to determine when the the ranges generated for numeric faceting should be inclusive or exclusive of their end points.

Usage

From source file:com.frank.search.solr.core.DefaultQueryParser.java

License:Apache License

private void appendRangeFacetingOnFields(SolrQuery solrQuery, FacetQuery query) {
    FacetOptions facetRangeOptions = query.getFacetOptions();

    if (facetRangeOptions == null) {
        return;//from   w ww. j  a v a2 s . co m
    }

    for (FieldWithRangeParameters<?, ?, ?> rangeField : facetRangeOptions.getFieldsWithRangeParameters()) {

        if (rangeField instanceof FieldWithDateRangeParameters) {
            appendFieldFacetingByDateRange(solrQuery, (FieldWithDateRangeParameters) rangeField);
        } else if (rangeField instanceof FieldWithNumericRangeParameters) {
            appendFieldFacetingByNumberRange(solrQuery, (FieldWithNumericRangeParameters) rangeField);
        }

        if (rangeField.getHardEnd() != null && rangeField.getHardEnd()) {
            FacetParameter param = new FacetParameter(FacetParams.FACET_RANGE_HARD_END, true);
            addFieldSpecificParameterToSolrQuery(solrQuery, rangeField, param);
        }
        if (rangeField.getOther() != null) {
            FacetParameter param = new FacetParameter(FacetParams.FACET_RANGE_OTHER, rangeField.getOther());
            addFieldSpecificParameterToSolrQuery(solrQuery, rangeField, param);
        }

        if (rangeField.getInclude() != null) {
            FacetParameter param = new FacetParameter(FacetParams.FACET_RANGE_INCLUDE, rangeField.getInclude());
            addFieldSpecificParameterToSolrQuery(solrQuery, rangeField, param);
        }

    }
}

From source file:fi.nationallibrary.ndl.solr.request.RangeFieldFacets.java

License:Apache License

private <T extends Comparable<T>> NamedList getFacetRangeCounts(final SchemaField sf,
        final RangeEndpointCalculator<T> calc) throws IOException {

    final String f = sf.getName();
    final NamedList res = new SimpleOrderedMap();
    final NamedList counts = new NamedList();
    res.add("counts", counts);

    final T start = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_START));

    // not final, hardend may change this
    T end = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_END));
    if (end.compareTo(start) < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "range facet 'end' comes before 'start': " + end + " < " + start);
    }//from w ww  . ja v a  2s .c  o  m

    final String gap = required.getFieldParam(f, FacetParams.FACET_RANGE_GAP);
    String[] gaps = parseGaps(gap);
    // explicitly return the gap.  compute this early so we are more 
    // likely to catch parse errors before attempting math
    for (int i = 0; i < gaps.length; i++) {
        calc.getGap(gaps[i]);
    }
    res.add("gap", gap);

    final int minCount = params.getFieldInt(f, FacetParams.FACET_MINCOUNT, 0);

    final EnumSet<FacetRangeInclude> include = FacetRangeInclude
            .parseParam(params.getFieldParams(f, FacetParams.FACET_RANGE_INCLUDE));

    T low = start;
    int gapIdx = 0;
    int previousCount = 0;

    while (low.compareTo(end) < 0) {
        T high = calc.addGap(low, gaps[gapIdx]);
        if (end.compareTo(high) < 0) {
            if (params.getFieldBool(f, FacetParams.FACET_RANGE_HARD_END, false)) {
                high = end;
            } else {
                end = high;
            }
        }
        if (high.compareTo(low) < 0) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "range facet infinite loop (is gap negative? did the math overflow?)");
        }

        final boolean includeLower = (include.contains(FacetRangeInclude.LOWER)
                || (include.contains(FacetRangeInclude.EDGE) && 0 == low.compareTo(start)));
        final boolean includeUpper = (include.contains(FacetRangeInclude.UPPER)
                || (include.contains(FacetRangeInclude.EDGE) && 0 == high.compareTo(end)));

        final String lowS = calc.formatValue(low);
        final String highS = calc.formatValue(high);

        final int count = rangeCount(sf, lowS, highS, includeLower, includeUpper);
        if (count >= minCount && count != previousCount) {
            counts.add(lowS, count);
            previousCount = count;
        }

        low = high;

        gapIdx = Math.min(gaps.length - 1, gapIdx + 1);
    }

    // explicitly return the start and end so all the counts 
    // (including before/after/between) are meaningful - even if mincount
    // has removed the neighboring ranges
    res.add("start", start);
    res.add("end", end);

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

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

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

            boolean all = others.contains(FacetRangeOther.ALL);
            final String startS = calc.formatValue(start);
            final String endS = calc.formatValue(end);

            if (all || others.contains(FacetRangeOther.BEFORE)) {
                // include upper bound if "outer" or if first gap doesn't already include it
                res.add(FacetRangeOther.BEFORE.toString(),
                        rangeCount(sf, null, startS, false,
                                (include.contains(FacetRangeInclude.OUTER)
                                        || (!(include.contains(FacetRangeInclude.LOWER)
                                                || include.contains(FacetRangeInclude.EDGE))))));

            }
            if (all || others.contains(FacetRangeOther.AFTER)) {
                // include lower bound if "outer" or if last gap doesn't already include it
                res.add(FacetRangeOther.AFTER.toString(),
                        rangeCount(sf, endS, null,
                                (include.contains(FacetRangeInclude.OUTER)
                                        || (!(include.contains(FacetRangeInclude.UPPER)
                                                || include.contains(FacetRangeInclude.EDGE)))),
                                false));
            }
            if (all || others.contains(FacetRangeOther.BETWEEN)) {
                res.add(FacetRangeOther.BETWEEN.toString(), rangeCount(sf, startS, endS,
                        (include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE)),
                        (include.contains(FacetRangeInclude.UPPER)
                                || include.contains(FacetRangeInclude.EDGE))));

            }
        }
    }
    return res;
}

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

License:Open Source License

private <T extends Comparable<T>> NamedList<Object> getFacetRangeCounts(final SchemaField sf, final String f,
        final RangeEndpointCalculator<T> calc) throws IOException {
    String prefix = params.getFieldParam(f, FacetParams.FACET_PREFIX);

    final NamedList<Object> res = new SimpleOrderedMap<Object>();
    final NamedList<Object> counts = new NamedList<Object>();
    res.add("counts", counts);

    final T start = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_START));
    // not final, hardend may change this
    T end = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_END));
    if (end.compareTo(start) < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "range facet 'end' comes before 'start': " + end + " < " + start);
    }//from w  w w  .ja v a2  s  . c  om

    final String gap = required.getFieldParam(f, FacetParams.FACET_RANGE_GAP);
    // explicitly return the gap.  compute this early so we are more 
    // likely to catch parse errors before attempting math
    res.add("gap", calc.getGap(gap));

    final int minCount = params.getFieldInt(f, FacetParams.FACET_MINCOUNT, 0);

    final EnumSet<FacetRangeInclude> include = FacetRangeInclude
            .parseParam(params.getFieldParams(f, FacetParams.FACET_RANGE_INCLUDE));

    T low = start;

    while (low.compareTo(end) < 0) {
        T high = calc.addGap(low, gap);
        if (end.compareTo(high) < 0) {
            if (params.getFieldBool(f, FacetParams.FACET_RANGE_HARD_END, false)) {
                high = end;
            } else {
                end = high;
            }
        }
        if (high.compareTo(low) < 0) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "range facet infinite loop (is gap negative? did the math overflow?)");
        }

        final boolean includeLower = (include.contains(FacetRangeInclude.LOWER)
                || (include.contains(FacetRangeInclude.EDGE) && 0 == low.compareTo(start)));
        final boolean includeUpper = (include.contains(FacetRangeInclude.UPPER)
                || (include.contains(FacetRangeInclude.EDGE) && 0 == high.compareTo(end)));

        final String lowS = calc.formatValue(low);
        final String highS = calc.formatValue(high);

        Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, lowS, highS, includeLower, includeUpper);
        final int count = rangeCount(rangeQ);
        if (count >= minCount) {
            counts.add(lowS + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(), count);
        }

        low = high;
    }

    // explicitly return the start and end so all the counts 
    // (including before/after/between) are meaningful - even if mincount
    // has removed the neighboring ranges
    res.add("start", start);
    res.add("end", end);

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

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

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

            boolean all = others.contains(FacetRangeOther.ALL);
            final String startS = calc.formatValue(start);
            final String endS = calc.formatValue(end);

            if (all || others.contains(FacetRangeOther.BEFORE)) {
                // include upper bound if "outer" or if first gap doesn't already include it
                Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, null, startS, false,
                        (include.contains(FacetRangeInclude.OUTER)
                                || (!(include.contains(FacetRangeInclude.LOWER)
                                        || include.contains(FacetRangeInclude.EDGE)))));
                int count = rangeCount(rangeQ);
                if (count >= minCount) {
                    res.add(FacetRangeOther.BEFORE.toString(), count);
                    counts.add(FacetRangeOther.BEFORE.toString() + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(),
                            count);
                }
            }
            if (all || others.contains(FacetRangeOther.AFTER)) {
                // include lower bound if "outer" or if last gap doesn't already include it
                Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, endS, null,
                        (include.contains(FacetRangeInclude.OUTER)
                                || (!(include.contains(FacetRangeInclude.UPPER)
                                        || include.contains(FacetRangeInclude.EDGE)))),
                        false);
                int count = rangeCount(rangeQ);
                if (count >= minCount) {
                    res.add(FacetRangeOther.AFTER.toString(), count);
                    counts.add(FacetRangeOther.AFTER.toString() + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(),
                            count);
                }
            }
            if (all || others.contains(FacetRangeOther.BETWEEN)) {
                Query rangeQ = getRangeQuery(sf.getType(), null, sf, prefix, startS, endS,
                        (include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE)),
                        (include.contains(FacetRangeInclude.UPPER)
                                || include.contains(FacetRangeInclude.EDGE)));
                int count = rangeCount(rangeQ);
                if (count >= minCount) {
                    res.add(FacetRangeOther.BETWEEN.toString(), count);
                    counts.add(
                            FacetRangeOther.BETWEEN.toString() + PROPNAME_INDEX_SEPARATOR + rangeQ.toString(),
                            count);
                }
            }
        }
    }
    return res;
}

From source file:org.springframework.data.solr.core.query.FacetOptionsTests.java

License:Apache License

@Test // DATSOLR-86
public void testDateRangeFacetAccessors() {
    Date start = new Date(100);
    Date end = new Date(10000000);
    String gap = "+1DAY";
    boolean hardEnd = true;
    FacetRangeInclude include = FacetRangeInclude.LOWER;
    FacetRangeOther other = FacetRangeOther.BEFORE;

    FieldWithDateRangeParameters dateRangeField = new FieldWithDateRangeParameters( //
            "name", //
            start, //
            end, //
            gap//
    )////  w w w  .ja  va  2  s. c om
            .setHardEnd(hardEnd) //
            .setInclude(include) //
            .setOther(other);

    Assert.assertEquals("name", dateRangeField.getName());
    Assert.assertEquals(start, dateRangeField.getStart());
    Assert.assertEquals(end, dateRangeField.getEnd());
    Assert.assertEquals(gap, dateRangeField.getGap());
    Assert.assertEquals(hardEnd, dateRangeField.getHardEnd());
    Assert.assertEquals(include, dateRangeField.getInclude());
    Assert.assertEquals(other, dateRangeField.getOther());

    Assert.assertEquals(start, dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_START).getValue());
    Assert.assertEquals(end, dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_END).getValue());
    Assert.assertEquals(gap, dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_GAP).getValue());
    Assert.assertEquals(hardEnd, dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_HARD_END).getValue());
    Assert.assertEquals(include, dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_INCLUDE).getValue());
    Assert.assertEquals(other, dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_OTHER).getValue());
}

From source file:org.springframework.data.solr.core.query.FacetOptionsTests.java

License:Apache License

@Test // DATSOLR-86
public void testNumericRangeFacetAccessors() {
    int start = 100;
    int end = 10000000;
    int gap = 200;
    boolean hardEnd = true;
    FacetRangeInclude include = FacetRangeInclude.LOWER;
    FacetRangeOther other = FacetRangeOther.BEFORE;

    FieldWithNumericRangeParameters numRangeField = new FieldWithNumericRangeParameters( //
            "name", //
            start, //
            end, //
            gap //
    )///*from  w w w  . ja va  2s .c o  m*/
            .setHardEnd(hardEnd) //
            .setInclude(include) //
            .setOther(other);

    Assert.assertEquals("name", numRangeField.getName());
    Assert.assertEquals(start, numRangeField.getStart());
    Assert.assertEquals(end, numRangeField.getEnd());
    Assert.assertEquals(gap, numRangeField.getGap());
    Assert.assertEquals(hardEnd, numRangeField.getHardEnd());
    Assert.assertEquals(include, numRangeField.getInclude());
    Assert.assertEquals(other, numRangeField.getOther());

    Assert.assertEquals(start, numRangeField.getQueryParameter(FacetParams.FACET_RANGE_START).getValue());
    Assert.assertEquals(end, numRangeField.getQueryParameter(FacetParams.FACET_RANGE_END).getValue());
    Assert.assertEquals(gap, numRangeField.getQueryParameter(FacetParams.FACET_RANGE_GAP).getValue());
    Assert.assertEquals(hardEnd, numRangeField.getQueryParameter(FacetParams.FACET_RANGE_HARD_END).getValue());
    Assert.assertEquals(include, numRangeField.getQueryParameter(FacetParams.FACET_RANGE_INCLUDE).getValue());
    Assert.assertEquals(other, numRangeField.getQueryParameter(FacetParams.FACET_RANGE_OTHER).getValue());
}

From source file:org.springframework.data.solr.core.query.FacetOptionsTests.java

License:Apache License

@Test // DATSOLR-86
public void testDateRangeFacetAccessorsAfterNullSet() {
    FieldWithDateRangeParameters dateRangeField = new FieldWithDateRangeParameters( //
            "name", //
            new Date(100), //
            new Date(10000000), //
            "+1DAY"//
    )///*from   www .  jav a2s  .  c om*/
            .setHardEnd(true) //
            .setInclude(FacetRangeInclude.LOWER) //
            .setOther(FacetRangeOther.BEFORE);

    dateRangeField.setHardEnd(null);
    dateRangeField.setInclude(null);
    dateRangeField.setOther(null);

    Assert.assertNull(dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_HARD_END));
    Assert.assertNull(dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_INCLUDE));
    Assert.assertNull(dateRangeField.getQueryParameter(FacetParams.FACET_RANGE_OTHER));
}

From source file:org.springframework.data.solr.core.query.FacetOptionsTests.java

License:Apache License

@Test // DATSOLR-86
public void testNumericRangeFacetAccessorsAfterNullSet() {
    FieldWithNumericRangeParameters numRangeField = new FieldWithNumericRangeParameters( //
            "name", //
            100, //
            10000000, //
            200 //
    )////www .  ja  va  2  s .c  o m
            .setHardEnd(true) //
            .setInclude(FacetRangeInclude.LOWER) //
            .setOther(FacetRangeOther.BEFORE);

    numRangeField.setHardEnd(null);
    numRangeField.setInclude(null);
    numRangeField.setOther(null);

    Assert.assertNull(numRangeField.getQueryParameter(FacetParams.FACET_RANGE_HARD_END));
    Assert.assertNull(numRangeField.getQueryParameter(FacetParams.FACET_RANGE_INCLUDE));
    Assert.assertNull(numRangeField.getQueryParameter(FacetParams.FACET_RANGE_OTHER));
}