Search in sources :

Example 1 with Facets

use of org.apache.lucene.spatial.prefix.NumberRangePrefixTreeStrategy.Facets in project lucene-solr by apache.

the class NumberRangeFacetsTest method test.

@Repeat(iterations = 20)
@Test
public void test() throws IOException {
    //generate test data
    List<Shape> indexedShapes = new ArrayList<>();
    final int numIndexedShapes = random().nextInt(15);
    for (int i = 0; i < numIndexedShapes; i++) {
        indexedShapes.add(randomShape());
    }
    //Main index loop:
    for (int i = 0; i < indexedShapes.size(); i++) {
        Shape shape = indexedShapes.get(i);
        adoc("" + i, shape);
        if (random().nextInt(10) == 0)
            //intermediate commit, produces extra segments
            commit();
    }
    //delete some documents randomly
    for (int id = 0; id < indexedShapes.size(); id++) {
        if (random().nextInt(10) == 0) {
            deleteDoc("" + id);
            indexedShapes.set(id, null);
        }
    }
    commit();
    //Main query loop:
    for (int queryIdx = 0; queryIdx < 10; queryIdx++) {
        preQueryHavoc();
        // We need to have a facet range window to do the facets between (a start time & end time). We randomly
        // pick a date, decide the level we want to facet on, and then pick a right end time that is up to 2 thousand
        // values later.
        int calFieldFacet = randomCalWindowField - 1;
        if (calFieldFacet > 1 && rarely()) {
            calFieldFacet--;
        }
        final Calendar leftCal = randomCalendar();
        leftCal.add(calFieldFacet, -1 * randomInt(1000));
        Calendar rightCal = (Calendar) leftCal.clone();
        rightCal.add(calFieldFacet, randomInt(2000));
        // Pick facet detail level based on cal field.
        int detailLevel = tree.getTreeLevelForCalendarField(calFieldFacet);
        if (detailLevel < 0) {
            //no exact match
            detailLevel = -1 * detailLevel;
        }
        //Randomly pick a filter/acceptDocs
        Bits topAcceptDocs = null;
        List<Integer> acceptFieldIds = new ArrayList<>();
        if (usually()) {
            // replace the list.
            for (int i = 0; i < indexedShapes.size(); i++) {
                if (indexedShapes.get(i) == null) {
                    // we deleted this one
                    continue;
                }
                acceptFieldIds.add(i);
            }
            Collections.shuffle(acceptFieldIds, random());
            acceptFieldIds = acceptFieldIds.subList(0, randomInt(acceptFieldIds.size()));
            if (!acceptFieldIds.isEmpty()) {
                List<BytesRef> terms = new ArrayList<>();
                for (Integer acceptDocId : acceptFieldIds) {
                    terms.add(new BytesRef(acceptDocId.toString()));
                }
                topAcceptDocs = searchForDocBits(new TermInSetQuery("id", terms));
            }
        }
        //Lets do it!
        NumberRangePrefixTree.NRShape facetRange = tree.toRangeShape(tree.toShape(leftCal), tree.toShape(rightCal));
        Facets facets = ((NumberRangePrefixTreeStrategy) strategy).calcFacets(indexSearcher.getTopReaderContext(), topAcceptDocs, facetRange, detailLevel);
        //System.out.println("Q: " + queryIdx + " " + facets);
        //Verify results. We do it by looping over indexed shapes and reducing the facet counts.
        Shape facetShapeRounded = facetRange.roundToLevel(detailLevel);
        for (int indexedShapeId = 0; indexedShapeId < indexedShapes.size(); indexedShapeId++) {
            if (topAcceptDocs != null && !acceptFieldIds.contains(indexedShapeId)) {
                // this doc was filtered out via acceptDocs
                continue;
            }
            Shape indexedShape = indexedShapes.get(indexedShapeId);
            if (indexedShape == null) {
                //was deleted
                continue;
            }
            Shape indexedShapeRounded = ((NumberRangePrefixTree.NRShape) indexedShape).roundToLevel(detailLevel);
            if (!indexedShapeRounded.relate(facetShapeRounded).intersects()) {
                // no intersection at all
                continue;
            }
            // walk the cells
            final CellIterator cellIterator = tree.getTreeCellIterator(indexedShape, detailLevel);
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (!cell.getShape().relate(facetShapeRounded).intersects()) {
                    //no intersection; prune
                    cellIterator.remove();
                    continue;
                }
                assert cell.getLevel() <= detailLevel;
                if (cell.getLevel() == detailLevel) {
                    //count it
                    UnitNRShape shape = (UnitNRShape) cell.getShape();
                    //get parent
                    final UnitNRShape parentShape = shape.getShapeAtLevel(detailLevel - 1);
                    final Facets.FacetParentVal facetParentVal = facets.parents.get(parentShape);
                    assertNotNull(facetParentVal);
                    int index = shape.getValAtLevel(shape.getLevel());
                    assertNotNull(facetParentVal.childCounts);
                    assert facetParentVal.childCounts[index] > 0;
                    facetParentVal.childCounts[index]--;
                } else if (cell.isLeaf()) {
                    //count it, and remove/prune.
                    if (cell.getLevel() < detailLevel - 1) {
                        assert facets.topLeaves > 0;
                        facets.topLeaves--;
                    } else {
                        UnitNRShape shape = (UnitNRShape) cell.getShape();
                        //get parent
                        final UnitNRShape parentShape = shape.getShapeAtLevel(detailLevel - 1);
                        final Facets.FacetParentVal facetParentVal = facets.parents.get(parentShape);
                        assertNotNull(facetParentVal);
                        assert facetParentVal.parentLeaves > 0;
                        facetParentVal.parentLeaves--;
                    }
                    cellIterator.remove();
                }
            }
        }
        // At this point; all counts should be down to zero.
        assertTrue(facets.topLeaves == 0);
        for (Facets.FacetParentVal facetParentVal : facets.parents.values()) {
            assertTrue(facetParentVal.parentLeaves == 0);
            if (facetParentVal.childCounts != null) {
                for (int childCount : facetParentVal.childCounts) {
                    assertTrue(childCount == 0);
                }
            }
        }
    }
}
Also used : UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) Shape(org.locationtech.spatial4j.shape.Shape) Facets(org.apache.lucene.spatial.prefix.NumberRangePrefixTreeStrategy.Facets) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) NumberRangePrefixTree(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree) UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) Bits(org.apache.lucene.util.Bits) CellIterator(org.apache.lucene.spatial.prefix.tree.CellIterator) Cell(org.apache.lucene.spatial.prefix.tree.Cell) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) Repeat(com.carrotsearch.randomizedtesting.annotations.Repeat)

Aggregations

Repeat (com.carrotsearch.randomizedtesting.annotations.Repeat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)1 Facets (org.apache.lucene.spatial.prefix.NumberRangePrefixTreeStrategy.Facets)1 Cell (org.apache.lucene.spatial.prefix.tree.Cell)1 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)1 NumberRangePrefixTree (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree)1 UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)1 Bits (org.apache.lucene.util.Bits)1 BytesRef (org.apache.lucene.util.BytesRef)1 Test (org.junit.Test)1 Shape (org.locationtech.spatial4j.shape.Shape)1