Example usage for org.apache.lucene.spatial.prefix PrefixTreeFacetCounter compute

List of usage examples for org.apache.lucene.spatial.prefix PrefixTreeFacetCounter compute

Introduction

In this page you can find the example usage for org.apache.lucene.spatial.prefix PrefixTreeFacetCounter compute.

Prototype

public static void compute(final PrefixTreeStrategy strategy, final LeafReaderContext context,
        final Bits acceptDocs, final Shape queryShape, final int facetLevel, final FacetVisitor facetVisitor)
        throws IOException 

Source Link

Document

Lower-level per-leaf segment method.

Usage

From source file:index.NumberRangePrefixTreeStrategy.java

License:Apache License

/**
 * Calculates facets (aggregated counts) given a range shape (start-end span) and a level, which specifies the detail.
 * To get the level of an existing shape, say a Calendar, call
 * {@link org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree#toUnitShape(Object)} then call
 * {@link org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape#getLevel()}.
 * Facet computation is implemented by navigating the underlying indexed terms efficiently.
 *//*  w  w  w  . java 2 s.  com*/
public Facets calcFacets(IndexReaderContext context, Filter filter, Shape facetRange, final int level)
        throws IOException {
    final Facets facets = new Facets(level);
    PrefixTreeFacetCounter.compute(this, context, filter, facetRange, level,
            new PrefixTreeFacetCounter.FacetVisitor() {
                Facets.FacetParentVal parentFacet;
                UnitNRShape parentShape;

                @Override
                public void visit(Cell cell, int count) {
                    if (cell.getLevel() < level - 1) {//some ancestor of parent facet level, direct or distant
                        parentFacet = null;//reset
                        parentShape = null;//reset
                        facets.topLeaves += count;
                    } else if (cell.getLevel() == level - 1) {//parent
                        //set up FacetParentVal
                        setupParent((UnitNRShape) cell.getShape());
                        parentFacet.parentLeaves += count;
                    } else {//at facet level
                        UnitNRShape unitShape = (UnitNRShape) cell.getShape();
                        UnitNRShape unitShapeParent = unitShape.getShapeAtLevel(unitShape.getLevel() - 1);
                        if (parentFacet == null || !parentShape.equals(unitShapeParent)) {
                            setupParent(unitShapeParent);
                        }
                        //lazy init childCounts
                        if (parentFacet.childCounts == null) {
                            parentFacet.childCounts = new int[parentFacet.childCountsLen];
                        }
                        parentFacet.childCounts[unitShape.getValAtLevel(cell.getLevel())] += count;
                    }
                }

                private void setupParent(UnitNRShape unitShape) {
                    parentShape = unitShape.clone();
                    //Look for existing parentFacet (from previous segment), or create anew if needed
                    parentFacet = facets.parents.get(parentShape);
                    if (parentFacet == null) {//didn't find one; make a new one
                        parentFacet = new Facets.FacetParentVal();
                        parentFacet.childCountsLen = getGrid().getNumSubCells(parentShape);
                        facets.parents.put(parentShape, parentFacet);
                    }
                }
            });
    return facets;
}