Search in sources :

Example 11 with DoubleValuesSource

use of org.apache.lucene.search.DoubleValuesSource in project lucene-solr by apache.

the class TestRangeFacetCounts method testRandomDoubles.

public void testRandomDoubles() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    int numDocs = atLeast(1000);
    double[] values = new double[numDocs];
    double minValue = Double.POSITIVE_INFINITY;
    double maxValue = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < numDocs; i++) {
        Document doc = new Document();
        double v = random().nextDouble();
        values[i] = v;
        doc.add(new DoubleDocValuesField("field", v));
        doc.add(new DoublePoint("field", v));
        w.addDocument(doc);
        minValue = Math.min(minValue, v);
        maxValue = Math.max(maxValue, v);
    }
    IndexReader r = w.getReader();
    IndexSearcher s = newSearcher(r, false);
    FacetsConfig config = new FacetsConfig();
    int numIters = atLeast(10);
    for (int iter = 0; iter < numIters; iter++) {
        if (VERBOSE) {
            System.out.println("TEST: iter=" + iter);
        }
        int numRange = TestUtil.nextInt(random(), 1, 5);
        DoubleRange[] ranges = new DoubleRange[numRange];
        int[] expectedCounts = new int[numRange];
        double minAcceptedValue = Double.POSITIVE_INFINITY;
        double maxAcceptedValue = Double.NEGATIVE_INFINITY;
        for (int rangeID = 0; rangeID < numRange; rangeID++) {
            double min;
            if (rangeID > 0 && random().nextInt(10) == 7) {
                // Use an existing boundary:
                DoubleRange prevRange = ranges[random().nextInt(rangeID)];
                if (random().nextBoolean()) {
                    min = prevRange.min;
                } else {
                    min = prevRange.max;
                }
            } else {
                min = random().nextDouble();
            }
            double max;
            if (rangeID > 0 && random().nextInt(10) == 7) {
                // Use an existing boundary:
                DoubleRange prevRange = ranges[random().nextInt(rangeID)];
                if (random().nextBoolean()) {
                    max = prevRange.min;
                } else {
                    max = prevRange.max;
                }
            } else {
                max = random().nextDouble();
            }
            if (min > max) {
                double x = min;
                min = max;
                max = x;
            }
            boolean minIncl;
            boolean maxIncl;
            long minAsLong = NumericUtils.doubleToSortableLong(min);
            long maxAsLong = NumericUtils.doubleToSortableLong(max);
            // NOTE: maxAsLong - minAsLong >= 0 is here to handle the common overflow case!
            if (maxAsLong - minAsLong >= 0 && maxAsLong - minAsLong < 2) {
                minIncl = true;
                maxIncl = true;
            } else {
                minIncl = random().nextBoolean();
                maxIncl = random().nextBoolean();
            }
            ranges[rangeID] = new DoubleRange("r" + rangeID, min, minIncl, max, maxIncl);
            // expected count:
            for (int i = 0; i < numDocs; i++) {
                boolean accept = true;
                if (minIncl) {
                    accept &= values[i] >= min;
                } else {
                    accept &= values[i] > min;
                }
                if (maxIncl) {
                    accept &= values[i] <= max;
                } else {
                    accept &= values[i] < max;
                }
                if (accept) {
                    expectedCounts[rangeID]++;
                    minAcceptedValue = Math.min(minAcceptedValue, values[i]);
                    maxAcceptedValue = Math.max(maxAcceptedValue, values[i]);
                }
            }
        }
        FacetsCollector sfc = new FacetsCollector();
        s.search(new MatchAllDocsQuery(), sfc);
        Query fastMatchFilter;
        if (random().nextBoolean()) {
            if (random().nextBoolean()) {
                fastMatchFilter = DoublePoint.newRangeQuery("field", minValue, maxValue);
            } else {
                fastMatchFilter = DoublePoint.newRangeQuery("field", minAcceptedValue, maxAcceptedValue);
            }
        } else {
            fastMatchFilter = null;
        }
        DoubleValuesSource vs = DoubleValuesSource.fromDoubleField("field");
        Facets facets = new DoubleRangeFacetCounts("field", vs, sfc, fastMatchFilter, ranges);
        FacetResult result = facets.getTopChildren(10, "field");
        assertEquals(numRange, result.labelValues.length);
        for (int rangeID = 0; rangeID < numRange; rangeID++) {
            if (VERBOSE) {
                System.out.println("  range " + rangeID + " expectedCount=" + expectedCounts[rangeID]);
            }
            LabelAndValue subNode = result.labelValues[rangeID];
            assertEquals("r" + rangeID, subNode.label);
            assertEquals(expectedCounts[rangeID], subNode.value.intValue());
            DoubleRange range = ranges[rangeID];
            // Test drill-down:
            DrillDownQuery ddq = new DrillDownQuery(config);
            if (random().nextBoolean()) {
                ddq.add("field", DoublePoint.newRangeQuery("field", range.min, range.max));
            } else {
                ddq.add("field", range.getQuery(fastMatchFilter, vs));
            }
            assertEquals(expectedCounts[rangeID], s.search(ddq, 10).totalHits);
        }
    }
    w.close();
    IOUtils.close(r, dir);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MultiFacets(org.apache.lucene.facet.MultiFacets) Facets(org.apache.lucene.facet.Facets) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) Document(org.apache.lucene.document.Document) LabelAndValue(org.apache.lucene.facet.LabelAndValue) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) Directory(org.apache.lucene.store.Directory) FacetsConfig(org.apache.lucene.facet.FacetsConfig) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DoublePoint(org.apache.lucene.document.DoublePoint) IndexReader(org.apache.lucene.index.IndexReader) FacetResult(org.apache.lucene.facet.FacetResult) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 12 with DoubleValuesSource

use of org.apache.lucene.search.DoubleValuesSource in project lucene-solr by apache.

the class DistanceFacetsExample method drillDown.

/** User drills down on the specified range. */
public TopDocs drillDown(DoubleRange range) throws IOException {
    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(null);
    final DoubleValuesSource vs = getDistanceValueSource();
    q.add("field", range.getQuery(getBoundingBoxQuery(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, range.max), vs));
    DrillSideways ds = new DrillSideways(searcher, config, (TaxonomyReader) null) {

        @Override
        protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways, String[] drillSidewaysDims) throws IOException {
            assert drillSideways.length == 1;
            return new DoubleRangeFacetCounts("field", vs, drillSideways[0], ONE_KM, TWO_KM, FIVE_KM, TEN_KM);
        }
    };
    return ds.search(q, 10).hits;
}
Also used : DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) DrillSideways(org.apache.lucene.facet.DrillSideways) DoubleRangeFacetCounts(org.apache.lucene.facet.range.DoubleRangeFacetCounts) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Aggregations

DoubleValuesSource (org.apache.lucene.search.DoubleValuesSource)12 DoubleValues (org.apache.lucene.search.DoubleValues)4 Explanation (org.apache.lucene.search.Explanation)4 Query (org.apache.lucene.search.Query)4 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)3 FacetsCollector (org.apache.lucene.facet.FacetsCollector)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 Term (org.apache.lucene.index.Term)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 SortField (org.apache.lucene.search.SortField)3 TermQuery (org.apache.lucene.search.TermQuery)3 TopDocs (org.apache.lucene.search.TopDocs)3 Document (org.apache.lucene.document.Document)2 DrillSideways (org.apache.lucene.facet.DrillSideways)2 Facets (org.apache.lucene.facet.Facets)2 FacetsConfig (org.apache.lucene.facet.FacetsConfig)2 MultiFacets (org.apache.lucene.facet.MultiFacets)2 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2