Search in sources :

Example 1 with IntervalFacets

use of org.apache.solr.request.IntervalFacets in project lucene-solr by apache.

the class RangeFacetProcessor method getFacetRangeCountsDocValues.

private <T extends Comparable<T>> NamedList<Object> getFacetRangeCountsDocValues(RangeFacetRequest rfr) throws IOException, SyntaxError {
    SchemaField sf = rfr.getSchemaField();
    final NamedList<Object> res = new SimpleOrderedMap<>();
    final NamedList<Integer> counts = new NamedList<>();
    res.add("counts", counts);
    ArrayList<IntervalFacets.FacetInterval> intervals = new ArrayList<>();
    // explicitly return the gap.  compute this early so we are more
    // likely to catch parse errors before attempting math
    res.add("gap", rfr.getGapObj());
    final int minCount = rfr.getMinCount();
    boolean includeBefore = false;
    boolean includeBetween = false;
    boolean includeAfter = false;
    Set<FacetRangeOther> others = rfr.getOthers();
    // anything if "none" is specified.
    if (!others.contains(FacetRangeOther.NONE)) {
        if (others.contains(FacetRangeOther.ALL) || others.contains(FacetRangeOther.BEFORE)) {
            // We'll add an interval later in this position
            intervals.add(null);
            includeBefore = true;
        }
        if (others.contains(FacetRangeOther.ALL) || others.contains(FacetRangeOther.BETWEEN)) {
            // We'll add an interval later in this position
            intervals.add(null);
            includeBetween = true;
        }
        if (others.contains(FacetRangeOther.ALL) || others.contains(FacetRangeOther.AFTER)) {
            includeAfter = true;
        }
    }
    IntervalFacets.FacetInterval after = null;
    for (RangeFacetRequest.FacetRange range : rfr.getFacetRanges()) {
        try {
            FacetRangeOther other = FacetRangeOther.get(range.name);
            if (other != null) {
                switch(other) {
                    case BEFORE:
                        assert range.lower == null;
                        intervals.set(0, new IntervalFacets.FacetInterval(sf, "*", range.upper, range.includeLower, range.includeUpper, FacetRangeOther.BEFORE.toString()));
                        break;
                    case AFTER:
                        assert range.upper == null;
                        after = new IntervalFacets.FacetInterval(sf, range.lower, "*", range.includeLower, range.includeUpper, FacetRangeOther.AFTER.toString());
                        break;
                    case BETWEEN:
                        intervals.set(includeBefore ? 1 : 0, new IntervalFacets.FacetInterval(sf, range.lower, range.upper, range.includeLower, range.includeUpper, FacetRangeOther.BETWEEN.toString()));
                        break;
                }
            }
            continue;
        } catch (SolrException e) {
        // safe to ignore
        }
        intervals.add(new IntervalFacets.FacetInterval(sf, range.lower, range.upper, range.includeLower, range.includeUpper, range.lower));
    }
    if (includeAfter) {
        assert after != null;
        intervals.add(after);
    }
    IntervalFacets.FacetInterval[] intervalsArray = intervals.toArray(new IntervalFacets.FacetInterval[intervals.size()]);
    // don't use the ArrayList anymore
    intervals = null;
    new IntervalFacets(sf, searcher, computeDocSet(docsOrig, rfr.getExcludeTags()), intervalsArray);
    int intervalIndex = 0;
    int lastIntervalIndex = intervalsArray.length - 1;
    // response named list instead of with the counts
    if (includeBefore) {
        res.add(intervalsArray[intervalIndex].getKey(), intervalsArray[intervalIndex].getCount());
        intervalIndex++;
    }
    // "BEFORE" was also requested). Needs to be added to the response named list instead of with the counts
    if (includeBetween) {
        res.add(intervalsArray[intervalIndex].getKey(), intervalsArray[intervalIndex].getCount());
        intervalIndex++;
    }
    // Needs to be added to the response named list instead of with the counts
    if (includeAfter) {
        res.add(intervalsArray[lastIntervalIndex].getKey(), intervalsArray[lastIntervalIndex].getCount());
        lastIntervalIndex--;
    }
    // now add all other intervals to the counts NL
    while (intervalIndex <= lastIntervalIndex) {
        IntervalFacets.FacetInterval interval = intervalsArray[intervalIndex];
        if (interval.getCount() >= minCount) {
            counts.add(interval.getKey(), interval.getCount());
        }
        intervalIndex++;
    }
    res.add("start", rfr.getStartObj());
    res.add("end", rfr.getEndObj());
    return res;
}
Also used : FacetRangeOther(org.apache.solr.common.params.FacetParams.FacetRangeOther) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) IntervalFacets(org.apache.solr.request.IntervalFacets) SchemaField(org.apache.solr.schema.SchemaField) SolrException(org.apache.solr.common.SolrException)

Aggregations

ArrayList (java.util.ArrayList)1 SolrException (org.apache.solr.common.SolrException)1 FacetRangeOther (org.apache.solr.common.params.FacetParams.FacetRangeOther)1 NamedList (org.apache.solr.common.util.NamedList)1 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)1 IntervalFacets (org.apache.solr.request.IntervalFacets)1 SchemaField (org.apache.solr.schema.SchemaField)1