Search in sources :

Example 1 with RangeFacetRequest

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

the class FacetingAccumulator method export.

@Override
@SuppressWarnings("unchecked")
public NamedList<?> export() {
    final NamedList<Object> base = (NamedList<Object>) super.export();
    NamedList<NamedList<?>> facetList = new NamedList<>();
    // Add the field facet buckets to the output
    base.add("fieldFacets", facetList);
    for (FieldFacetRequest freq : request.getFieldFacets()) {
        final String name = freq.getName();
        if (hiddenFieldFacets.contains(name)) {
            continue;
        }
        final Map<String, Expression[]> buckets = fieldFacetExpressions.get(name);
        final NamedList<Object> bucketBase = new NamedList<>();
        Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
        final FieldFacetRequest fr = (FieldFacetRequest) freq;
        final FacetSortSpecification sort = fr.getSort();
        final int limit = fr.getLimit();
        final int offset = fr.getOffset();
        final boolean showMissing = fr.showsMissing();
        if (!showMissing) {
            buckets.remove(MISSING_VALUE);
        }
        // Sorting the buckets if a sort specification is provided
        if (sort != null && buckets.values().iterator().hasNext()) {
            int sortPlace = Arrays.binarySearch(expressionNames, sort.getStatistic());
            final Expression first = buckets.values().iterator().next()[sortPlace];
            final Comparator<Expression> comp = (Comparator<Expression>) first.comparator(sort.getDirection());
            final List<Entry<String, Expression[]>> sorted = new ArrayList<>(buckets.size());
            Iterables.addAll(sorted, iter);
            Collections.sort(sorted, new EntryComparator(comp, sortPlace));
            iter = sorted;
        }
        // apply the limit
        if (limit > AnalyticsContentHandler.DEFAULT_FACET_LIMIT) {
            if (offset > 0) {
                iter = Iterables.skip(iter, offset);
            }
            iter = Iterables.limit(iter, limit);
        }
        // Export each expression in the bucket.
        for (Entry<String, Expression[]> bucket : iter) {
            bucketBase.add(bucket.getKey(), export(bucket.getValue()));
        }
        facetList.add(name, bucketBase);
    }
    // Add the range facet buckets to the output
    facetList = new NamedList<>();
    base.add("rangeFacets", facetList);
    for (RangeFacetRequest freq : request.getRangeFacets()) {
        final String name = freq.getName();
        final Map<String, Expression[]> buckets = rangeFacetExpressions.get(name);
        final NamedList<Object> bucketBase = new NamedList<>();
        Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
        for (Entry<String, Expression[]> bucket : iter) {
            bucketBase.add(bucket.getKey(), export(bucket.getValue()));
        }
        facetList.add(name, bucketBase);
    }
    // Add the query facet buckets to the output
    facetList = new NamedList<>();
    base.add("queryFacets", facetList);
    for (QueryFacetRequest freq : request.getQueryFacets()) {
        final String name = freq.getName();
        final Map<String, Expression[]> buckets = queryFacetExpressions.get(name);
        final NamedList<Object> bucketBase = new NamedList<>();
        Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
        for (Entry<String, Expression[]> bucket : iter) {
            bucketBase.add(bucket.getKey(), export(bucket.getValue()));
        }
        facetList.add(name, bucketBase);
    }
    return base;
}
Also used : RangeFacetRequest(org.apache.solr.analytics.request.RangeFacetRequest) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator) FacetSortSpecification(org.apache.solr.analytics.request.FieldFacetRequest.FacetSortSpecification) Entry(java.util.Map.Entry) Expression(org.apache.solr.analytics.expression.Expression) FieldFacetRequest(org.apache.solr.analytics.request.FieldFacetRequest) QueryFacetRequest(org.apache.solr.analytics.request.QueryFacetRequest)

Example 2 with RangeFacetRequest

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

the class FacetingAccumulator method processRangeFacets.

/**
   * Initiates the collecting of range facets
   * @param filter the base filter to use
   * @throws IOException if searching fails
   */
public void processRangeFacets(final Filter filter) throws IOException {
    for (RangeFacetRequest rfr : rangeFacets) {
        String[] pivotStr;
        String start = rfr.getStart();
        if (start.contains(AnalyticsParams.QUERY_RESULT)) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Query result requests can not be used in Range Facets");
        } else if (start.contains(AnalyticsParams.RESULT)) {
            try {
                pivotStr = ExpressionFactory.getArguments(start.substring(start.indexOf('(') + 1, start.indexOf(')')).trim());
                if (pivotStr.length == 1) {
                    rfr.setStart(getResult(pivotStr[0]));
                } else if (pivotStr.length == 3) {
                    rfr.setStart(getResult(pivotStr[0], pivotStr[1], pivotStr[2]));
                } else {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + start + " has an invalid amount of arguments.");
                }
            } catch (IndexOutOfBoundsException e) {
                throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + start + " is invalid. Lacks parentheses.", e);
            }
        }
        String end = rfr.getEnd();
        if (end.contains(AnalyticsParams.QUERY_RESULT)) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Query result requests can not be used in Range Facets");
        } else if (end.contains(AnalyticsParams.RESULT)) {
            try {
                pivotStr = ExpressionFactory.getArguments(end.substring(end.indexOf('(') + 1, end.indexOf(')')).trim());
                if (pivotStr.length == 1) {
                    rfr.setEnd(getResult(pivotStr[0]));
                } else if (pivotStr.length == 3) {
                    rfr.setEnd(getResult(pivotStr[0], pivotStr[1], pivotStr[2]));
                } else {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + end + " has an invalid amount of arguments.");
                }
            } catch (IndexOutOfBoundsException e) {
                throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + end + " is invalid. Lacks parentheses.", e);
            }
        }
        String[] gaps = rfr.getGaps();
        for (int count = 0; count < gaps.length; count++) {
            String gap = gaps[count];
            if (gap.contains(AnalyticsParams.QUERY_RESULT)) {
                throw new SolrException(ErrorCode.BAD_REQUEST, "Query result requests can not be used in Range Facets");
            } else if (gap.contains(AnalyticsParams.RESULT)) {
                try {
                    pivotStr = ExpressionFactory.getArguments(gap.substring(gap.indexOf('(') + 1, gap.indexOf(')')).trim());
                    if (pivotStr.length == 1) {
                        gaps[count] = getResult(pivotStr[0]);
                    } else if (pivotStr.length == 3) {
                        gaps[count] = getResult(pivotStr[0], pivotStr[1], pivotStr[2]);
                    } else {
                        throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + gap + " has an invalid amount of arguments.");
                    }
                } catch (IndexOutOfBoundsException e) {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + gap + " is invalid. Lacks parentheses.", e);
                }
            }
        }
        // Computes the end points of the ranges in the rangeFacet
        final RangeEndpointCalculator<? extends Comparable<?>> rec = RangeEndpointCalculator.create(rfr);
        final SchemaField sf = rfr.getField();
        // collect the documents for that range.
        for (FacetRange range : rec.getRanges()) {
            final String upper;
            final String lower;
            String facetValue = "";
            if (range.lower == null) {
                facetValue = "(*";
                lower = null;
            } else {
                lower = range.lower;
                facetValue = ((range.includeLower) ? "[" : "(") + range.lower;
            }
            facetValue += " TO ";
            if (range.upper == null) {
                upper = null;
                facetValue += "*)";
            } else {
                upper = range.upper;
                facetValue += range.upper + ((range.includeUpper) ? "]" : ")");
            }
            Query q = sf.getType().getRangeQuery(null, sf, lower, upper, range.includeLower, range.includeUpper);
            RangeFacetAccumulator rAcc = new RangeFacetAccumulator(this, rfr.getName(), facetValue);
            // The searcher sends docIds to the RangeFacetAccumulator which forwards
            // them to <code>collectRange()</code> in this class for collection.
            Query filtered = new BooleanQuery.Builder().add(q, Occur.MUST).add(filter, Occur.FILTER).build();
            searcher.search(filtered, rAcc);
            computeRangeFacet(sf.getName());
        }
    }
}
Also used : RangeFacetRequest(org.apache.solr.analytics.request.RangeFacetRequest) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) SchemaField(org.apache.solr.schema.SchemaField) RangeFacetAccumulator(org.apache.solr.analytics.accumulator.facet.RangeFacetAccumulator) FacetRange(org.apache.solr.analytics.util.RangeEndpointCalculator.FacetRange) SolrException(org.apache.solr.common.SolrException)

Aggregations

RangeFacetRequest (org.apache.solr.analytics.request.RangeFacetRequest)2 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Entry (java.util.Map.Entry)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 Query (org.apache.lucene.search.Query)1 RangeFacetAccumulator (org.apache.solr.analytics.accumulator.facet.RangeFacetAccumulator)1 Expression (org.apache.solr.analytics.expression.Expression)1 FieldFacetRequest (org.apache.solr.analytics.request.FieldFacetRequest)1 FacetSortSpecification (org.apache.solr.analytics.request.FieldFacetRequest.FacetSortSpecification)1 QueryFacetRequest (org.apache.solr.analytics.request.QueryFacetRequest)1 FacetRange (org.apache.solr.analytics.util.RangeEndpointCalculator.FacetRange)1 SolrException (org.apache.solr.common.SolrException)1 NamedList (org.apache.solr.common.util.NamedList)1 SchemaField (org.apache.solr.schema.SchemaField)1