Example usage for org.apache.lucene.facet.range LongRangeFacetCounts LongRangeFacetCounts

List of usage examples for org.apache.lucene.facet.range LongRangeFacetCounts LongRangeFacetCounts

Introduction

In this page you can find the example usage for org.apache.lucene.facet.range LongRangeFacetCounts LongRangeFacetCounts.

Prototype

public LongRangeFacetCounts(String field, FacetsCollector hits, LongRange... ranges) throws IOException 

Source Link

Document

Create LongRangeFacetCounts , using LongValuesSource from the specified field.

Usage

From source file:com.xiaomi.linden.core.search.LindenResultParser.java

License:Apache License

private void parseFacets(LindenResult result, Facets facets, FacetsCollector facetsCollector)
        throws IOException {
    // Set facets
    if (request.isSetFacet()) {
        if (request.getFacet().isSetFacetParams() && facets != null) {
            List<LindenFacetParam> facetParams = request.getFacet().getFacetParams();
            for (LindenFacetParam facetParam : facetParams) {
                LindenFacetResult lindenFacetResult = new LindenFacetResult();
                lindenFacetResult.setDim(facetParam.facetDimAndPath.dim);
                lindenFacetResult.setPath(facetParam.facetDimAndPath.path);
                FacetResult facetResult;
                if (facetParam.facetDimAndPath.path != null) {
                    facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim,
                            facetParam.facetDimAndPath.path.split("/"));
                } else {
                    facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim);
                }/*from  w w w .j  a va2s . c o m*/
                if (facetResult != null) {
                    lindenFacetResult.setValue(facetResult.value.intValue());
                    lindenFacetResult.setChildCount(facetResult.childCount);
                    int sumValue = 0;
                    for (int j = 0; j < facetResult.labelValues.length; ++j) {
                        LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
                        labelAndValue.setLabel(facetResult.labelValues[j].label);
                        int value = facetResult.labelValues[j].value.intValue();
                        labelAndValue.setValue(value);
                        sumValue += value;
                        lindenFacetResult.addToLabelValues(labelAndValue);
                    }
                    if (sumValue > lindenFacetResult.getValue()
                            || facetResult.labelValues.length < facetParam.topN) {
                        lindenFacetResult.setValue(sumValue);
                    }
                }
                result.addToFacetResults(lindenFacetResult);
            }
        } else if (request.getFacet().isSetAggregations() && facetsCollector != null) {
            List<Aggregation> aggregations = request.getFacet().getAggregations();
            for (int i = 0; i < aggregations.size(); ++i) {
                Aggregation aggregation = aggregations.get(i);
                String fieldName = aggregation.getField();
                LindenType type = aggregation.getType();
                AggregationResult aggregationResult = new AggregationResult();
                aggregationResult.setField(fieldName);
                Facets aggFacets;
                if (type == LindenType.INTEGER || type == LindenType.LONG) {
                    LongRange[] ranges = new LongRange[aggregation.getBucketsSize()];
                    for (int j = 0; j < ranges.length; ++j) {
                        Bucket bucket = aggregation.getBuckets().get(j);
                        String label = generateBucketLabel(bucket);
                        long minValue = bucket.getStartValue().equals("*") ? Long.MIN_VALUE
                                : Long.parseLong(bucket.getStartValue());
                        long maxValue = bucket.getEndValue().equals("*") ? Long.MAX_VALUE
                                : Long.parseLong(bucket.getEndValue());
                        ranges[j] = new LongRange(label, minValue, bucket.isStartClosed(), maxValue,
                                bucket.isEndClosed());
                    }
                    aggFacets = new LongRangeFacetCounts(fieldName, facetsCollector, ranges);
                } else if (type == LindenType.DOUBLE) {
                    DoubleRange[] ranges = new DoubleRange[aggregation.getBucketsSize()];
                    for (int j = 0; j < ranges.length; ++j) {
                        Bucket bucket = aggregation.getBuckets().get(j);
                        String label = generateBucketLabel(bucket);
                        double minValue = bucket.getStartValue().equals("*") ? -Double.MAX_VALUE
                                : Double.parseDouble(bucket.getStartValue());
                        double maxValue = bucket.getEndValue().equals("*") ? Double.MAX_VALUE
                                : Double.parseDouble(bucket.getEndValue());
                        ranges[j] = new DoubleRange(label, minValue, bucket.isStartClosed(), maxValue,
                                bucket.isEndClosed());
                    }
                    aggFacets = new DoubleRangeFacetCounts(fieldName, facetsCollector, ranges);
                } else {
                    throw new IOException(type + " type is not supported in aggregation");
                }
                FacetResult facetResult = aggFacets.getTopChildren(aggregation.getBucketsSize(), fieldName);
                for (int j = 0; j < facetResult.labelValues.length; ++j) {
                    LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
                    labelAndValue.setLabel(facetResult.labelValues[j].label);
                    labelAndValue.setValue(facetResult.labelValues[j].value.intValue());
                    aggregationResult.addToLabelValues(labelAndValue);
                }
                result.addToAggregationResults(aggregationResult);
            }
        }
    }
}

From source file:perf.SearchTask.java

License:Apache License

@Override
public void go(IndexState state) throws IOException {
    //System.out.println("go group=" + this.group + " single=" + singlePassGroup + " xxx=" + xxx + " this=" + this);
    final IndexSearcher searcher = state.mgr.acquire();

    //System.out.println("GO query=" + q);

    try {/*from  w  ww  . ja  v a2 s. c  om*/
        if (doHilite) {
            if (state.fastHighlighter != null) {
                fieldQuery = state.fastHighlighter.getFieldQuery(q, searcher.getIndexReader());
            } else if (state.useHighlighter) {
                highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(q));
            } else {
                // no setup for postingshighlighter
            }
        }

        if (group != null) {
            if (singlePassGroup) {
                final BlockGroupingCollector c = new BlockGroupingCollector(Sort.RELEVANCE, 10, true,
                        searcher.createNormalizedWeight(state.groupEndQuery, false));
                searcher.search(q, c);
                groupsResultBlock = c.getTopGroups(Sort.RELEVANCE, 0, 0, 10, true);

                if (doHilite) {
                    hilite(groupsResultBlock, state, searcher);
                }

            } else {
                //System.out.println("GB: " + group);
                final TermFirstPassGroupingCollector c1 = new TermFirstPassGroupingCollector(group,
                        Sort.RELEVANCE, 10);

                final Collector c;
                final TermAllGroupsCollector allGroupsCollector;
                // Turn off AllGroupsCollector for now -- it's very slow:
                if (false && doCountGroups) {
                    allGroupsCollector = new TermAllGroupsCollector(group);
                    //c = MultiCollector.wrap(allGroupsCollector, c1);
                    c = c1;
                } else {
                    allGroupsCollector = null;
                    c = c1;
                }

                searcher.search(q, c);

                final Collection<SearchGroup<BytesRef>> topGroups = c1.getTopGroups(0, true);
                if (topGroups != null) {
                    final TermSecondPassGroupingCollector c2 = new TermSecondPassGroupingCollector(group,
                            topGroups, Sort.RELEVANCE, Sort.RELEVANCE, 10, true, true, true);
                    searcher.search(q, c2);
                    groupsResultTerms = c2.getTopGroups(0);
                    if (allGroupsCollector != null) {
                        groupsResultTerms = new TopGroups<BytesRef>(groupsResultTerms,
                                allGroupsCollector.getGroupCount());
                    }
                    if (doHilite) {
                        hilite(groupsResultTerms, state, searcher);
                    }
                }
            }
        } else if (!facetRequests.isEmpty()) {
            // TODO: support sort, filter too!!
            // TODO: support other facet methods
            if (doDrillSideways) {
                // nocommit todo
                hits = null;
                facetResults = null;
            } else {
                facetResults = new ArrayList<FacetResult>();
                FacetsCollector fc = new FacetsCollector();
                hits = FacetsCollector.search(searcher, q, 10, fc);
                long t0 = System.nanoTime();

                Facets mainFacets = null;
                for (String request : facetRequests) {
                    if (request.startsWith("range:")) {
                        int i = request.indexOf(':', 6);
                        if (i == -1) {
                            throw new IllegalArgumentException("range facets request \"" + request
                                    + "\" is missing field; should be range:field:0-10,10-20");
                        }
                        String field = request.substring(6, i);
                        String[] rangeStrings = request.substring(i + 1, request.length()).split(",");
                        LongRange[] ranges = new LongRange[rangeStrings.length];
                        for (int rangeIDX = 0; rangeIDX < ranges.length; rangeIDX++) {
                            String rangeString = rangeStrings[rangeIDX];
                            int j = rangeString.indexOf('-');
                            if (j == -1) {
                                throw new IllegalArgumentException(
                                        "range facets request should be X-Y; got: " + rangeString);
                            }
                            long start = Long.parseLong(rangeString.substring(0, j));
                            long end = Long.parseLong(rangeString.substring(j + 1));
                            ranges[rangeIDX] = new LongRange(rangeString, start, true, end, true);
                        }
                        LongRangeFacetCounts facets = new LongRangeFacetCounts(field, fc, ranges);
                        facetResults.add(facets.getTopChildren(ranges.length, field));
                    } else {
                        Facets facets = new FastTaxonomyFacetCounts(state.taxoReader, state.facetsConfig, fc);
                        facetResults.add(facets.getTopChildren(10, request));
                    }
                }
                getFacetResultsMsec = (System.nanoTime() - t0) / 1000000.0;
            }
        } else if (s == null) {
            hits = searcher.search(q, topN);
            if (doHilite) {
                hilite(hits, state, searcher, q);
            }
        } else {
            hits = searcher.search(q, topN, s);
            if (doHilite) {
                hilite(hits, state, searcher, q);
            }
            /*
              final boolean fillFields = true;
              final boolean fieldSortDoTrackScores = true;
              final boolean fieldSortDoMaxScore = true;
              final TopFieldCollector c = TopFieldCollector.create(s, topN,
              fillFields,
              fieldSortDoTrackScores,
              fieldSortDoMaxScore,
              false);
              searcher.search(q, c);
              hits = c.topDocs();
            */
        }
        if (hits != null) {
            totalHitCount = hits.totalHits;

            if (doStoredLoads) {
                for (int i = 0; i < hits.scoreDocs.length; i++) {
                    ScoreDoc scoreDoc = hits.scoreDocs[i];
                    searcher.doc(scoreDoc.doc);
                }
            }

        } else if (groupsResultBlock != null) {
            totalHitCount = groupsResultBlock.totalHitCount;
        }
    } catch (Throwable t) {
        System.out.println("EXC: " + q);
        throw new RuntimeException(t);
        //System.out.println("TE: " + TermsEnum.getStats());
    } finally {
        state.mgr.release(searcher);
        fieldQuery = null;
        highlighter = null;
    }
}