Search in sources :

Example 1 with FacetInterval

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

the class IntervalFacets method getSortedIntervals.

private FacetInterval[] getSortedIntervals(String[] intervals, SolrParams params) throws SyntaxError {
    FacetInterval[] sortedIntervals = new FacetInterval[intervals.length];
    int idx = 0;
    for (String intervalStr : intervals) {
        sortedIntervals[idx++] = new FacetInterval(schemaField, intervalStr, params);
    }
    /*
     * This comparator sorts the intervals by start value from lower to greater
     */
    Arrays.sort(sortedIntervals, new Comparator<FacetInterval>() {

        @Override
        public int compare(FacetInterval o1, FacetInterval o2) {
            assert o1 != null;
            assert o2 != null;
            return compareStart(o1, o2);
        }

        private int compareStart(FacetInterval o1, FacetInterval o2) {
            if (o1.start == null) {
                if (o2.start == null) {
                    return 0;
                }
                return -1;
            }
            if (o2.start == null) {
                return 1;
            }
            int startComparison = o1.start.compareTo(o2.start);
            if (startComparison == 0) {
                if (o1.startOpen != o2.startOpen) {
                    if (!o1.startOpen) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            return startComparison;
        }
    });
    return sortedIntervals;
}
Also used : FacetInterval(org.apache.solr.request.IntervalFacets.FacetInterval)

Example 2 with FacetInterval

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

the class IntervalFacets method accumIntervalWithValue.

private void accumIntervalWithValue(long value) {
    for (int i = 0; i < intervals.length; i++) {
        FacetInterval interval = intervals[i];
        IntervalCompareResult result = interval.includes(value);
        if (result == IntervalCompareResult.INCLUDED) {
            interval.incCount();
        } else if (result == IntervalCompareResult.LOWER_THAN_START) {
            // we can skip them
            break;
        }
    }
}
Also used : FacetInterval(org.apache.solr.request.IntervalFacets.FacetInterval)

Example 3 with FacetInterval

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

the class SimpleFacets method getFacetIntervalCounts.

/**
   * Returns a <code>NamedList</code> with each entry having the "key" of the interval as name and the count of docs 
   * in that interval as value. All intervals added in the request are included in the returned 
   * <code>NamedList</code> (included those with 0 count), and it's required that the order of the intervals
   * is deterministic and equals in all shards of a distributed request, otherwise the collation of results
   * will fail. 
   * 
   */
public NamedList<Object> getFacetIntervalCounts() throws IOException, SyntaxError {
    NamedList<Object> res = new SimpleOrderedMap<Object>();
    String[] fields = global.getParams(FacetParams.FACET_INTERVAL);
    if (fields == null || fields.length == 0)
        return res;
    for (String field : fields) {
        final ParsedParams parsed = parseParams(FacetParams.FACET_INTERVAL, field);
        String[] intervalStrs = parsed.required.getFieldParams(parsed.facetValue, FacetParams.FACET_INTERVAL_SET);
        SchemaField schemaField = searcher.getCore().getLatestSchema().getField(parsed.facetValue);
        if (parsed.params.getBool(GroupParams.GROUP_FACET, false)) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Interval Faceting can't be used with " + GroupParams.GROUP_FACET);
        }
        if (schemaField.getType().isPointField() && !schemaField.hasDocValues()) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't use interval faceting on a PointField without docValues");
        }
        SimpleOrderedMap<Integer> fieldResults = new SimpleOrderedMap<Integer>();
        res.add(parsed.key, fieldResults);
        IntervalFacets intervalFacets = new IntervalFacets(schemaField, searcher, parsed.docs, intervalStrs, parsed.params);
        for (FacetInterval interval : intervalFacets) {
            fieldResults.add(interval.getKey(), interval.getCount());
        }
    }
    return res;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) FacetInterval(org.apache.solr.request.IntervalFacets.FacetInterval) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrException(org.apache.solr.common.SolrException)

Example 4 with FacetInterval

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

the class TestIntervalFaceting method assertIntervalKey.

private void assertIntervalKey(String fieldName, String intervalStr, String expectedKey, String... params) throws SyntaxError {
    assert (params.length & 1) == 0 : "Params must have an even number of elements";
    SchemaField f = h.getCore().getLatestSchema().getField(fieldName);
    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    for (int i = 0; i < params.length - 1; ) {
        solrParams.set(params[i], params[i + 1]);
        i += 2;
    }
    FacetInterval interval = new FacetInterval(f, intervalStr, solrParams);
    assertEquals("Expected key " + expectedKey + " but found " + interval.getKey(), expectedKey, interval.getKey());
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) FacetInterval(org.apache.solr.request.IntervalFacets.FacetInterval) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 5 with FacetInterval

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

the class TestIntervalFaceting method assertBadInterval.

private void assertBadInterval(String fieldName, String intervalStr, String errorMsg) {
    SchemaField f = h.getCore().getLatestSchema().getField(fieldName);
    try {
        new FacetInterval(f, intervalStr, new ModifiableSolrParams());
        fail("Expecting SyntaxError for interval String: " + intervalStr);
    } catch (SyntaxError e) {
        assertTrue("Unexpected error message for interval String: " + intervalStr + ": " + e.getMessage(), e.getMessage().contains(errorMsg));
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SyntaxError(org.apache.solr.search.SyntaxError) FacetInterval(org.apache.solr.request.IntervalFacets.FacetInterval) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

FacetInterval (org.apache.solr.request.IntervalFacets.FacetInterval)8 SchemaField (org.apache.solr.schema.SchemaField)5 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 BytesRef (org.apache.lucene.util.BytesRef)1 SolrException (org.apache.solr.common.SolrException)1 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)1 SyntaxError (org.apache.solr.search.SyntaxError)1