Search in sources :

Example 31 with FacetResult

use of org.apache.lucene.facet.FacetResult in project orientdb by orientechnologies.

the class LuceneNativeFacet method main.

/**
   * Runs the search and drill-down examples and prints the results.
   */
public static void main(String[] args) throws Exception {
    System.out.println("Facet counting example:");
    System.out.println("-----------------------");
    LuceneNativeFacet example = new LuceneNativeFacet();
    List<FacetResult> results1 = example.runFacetOnly();
    System.out.println("Author: " + results1.get(0));
    System.out.println("Publish Date: " + results1.get(1));
    System.out.println("Facet counting example (combined facets and search):");
    System.out.println("-----------------------");
    List<FacetResult> results = example.runSearch();
    System.out.println("Author: " + results.get(0));
    System.out.println("Publish Date: " + results.get(1));
    System.out.println("Facet drill-down example (Publish Date/2010):");
    System.out.println("---------------------------------------------");
    System.out.println("Author: " + example.runDrillDown());
    System.out.println("Facet drill-sideways example (Publish Date/2010):");
    System.out.println("---------------------------------------------");
    for (FacetResult result : example.runDrillSideways()) {
        System.out.println(result);
    }
}
Also used : FacetResult(org.apache.lucene.facet.FacetResult)

Example 32 with FacetResult

use of org.apache.lucene.facet.FacetResult in project lucene-solr by apache.

the class TestRangeFacetCounts method testBasicLong.

public void testBasicLong() throws Exception {
    Directory d = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), d);
    Document doc = new Document();
    NumericDocValuesField field = new NumericDocValuesField("field", 0L);
    doc.add(field);
    for (long l = 0; l < 100; l++) {
        field.setLongValue(l);
        w.addDocument(doc);
    }
    // Also add Long.MAX_VALUE
    field.setLongValue(Long.MAX_VALUE);
    w.addDocument(doc);
    IndexReader r = w.getReader();
    w.close();
    FacetsCollector fc = new FacetsCollector();
    IndexSearcher s = newSearcher(r);
    s.search(new MatchAllDocsQuery(), fc);
    Facets facets = new LongRangeFacetCounts("field", fc, new LongRange("less than 10", 0L, true, 10L, false), new LongRange("less than or equal to 10", 0L, true, 10L, true), new LongRange("over 90", 90L, false, 100L, false), new LongRange("90 or above", 90L, true, 100L, false), new LongRange("over 1000", 1000L, false, Long.MAX_VALUE, true));
    FacetResult result = facets.getTopChildren(10, "field");
    assertEquals("dim=field path=[] value=22 childCount=5\n  less than 10 (10)\n  less than or equal to 10 (11)\n  over 90 (9)\n  90 or above (10)\n  over 1000 (1)\n", result.toString());
    r.close();
    d.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiFacets(org.apache.lucene.facet.MultiFacets) Facets(org.apache.lucene.facet.Facets) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) FacetResult(org.apache.lucene.facet.FacetResult) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 33 with FacetResult

use of org.apache.lucene.facet.FacetResult in project lucene-solr by apache.

the class TestRangeFacetCounts method testOverlappedEndStart.

public void testOverlappedEndStart() throws Exception {
    Directory d = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), d);
    Document doc = new Document();
    NumericDocValuesField field = new NumericDocValuesField("field", 0L);
    doc.add(field);
    for (long l = 0; l < 100; l++) {
        field.setLongValue(l);
        w.addDocument(doc);
    }
    field.setLongValue(Long.MAX_VALUE);
    w.addDocument(doc);
    IndexReader r = w.getReader();
    w.close();
    FacetsCollector fc = new FacetsCollector();
    IndexSearcher s = newSearcher(r);
    s.search(new MatchAllDocsQuery(), fc);
    Facets facets = new LongRangeFacetCounts("field", fc, new LongRange("0-10", 0L, true, 10L, true), new LongRange("10-20", 10L, true, 20L, true), new LongRange("20-30", 20L, true, 30L, true), new LongRange("30-40", 30L, true, 40L, true));
    FacetResult result = facets.getTopChildren(10, "field");
    assertEquals("dim=field path=[] value=41 childCount=4\n  0-10 (11)\n  10-20 (11)\n  20-30 (11)\n  30-40 (11)\n", result.toString());
    r.close();
    d.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiFacets(org.apache.lucene.facet.MultiFacets) Facets(org.apache.lucene.facet.Facets) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) FacetResult(org.apache.lucene.facet.FacetResult) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 34 with FacetResult

use of org.apache.lucene.facet.FacetResult in project lucene-solr by apache.

the class TestRangeFacetCounts method testLongMinMax.

public void testLongMinMax() throws Exception {
    Directory d = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), d);
    Document doc = new Document();
    NumericDocValuesField field = new NumericDocValuesField("field", 0L);
    doc.add(field);
    field.setLongValue(Long.MIN_VALUE);
    w.addDocument(doc);
    field.setLongValue(0);
    w.addDocument(doc);
    field.setLongValue(Long.MAX_VALUE);
    w.addDocument(doc);
    IndexReader r = w.getReader();
    w.close();
    FacetsCollector fc = new FacetsCollector();
    IndexSearcher s = newSearcher(r);
    s.search(new MatchAllDocsQuery(), fc);
    Facets facets = new LongRangeFacetCounts("field", fc, new LongRange("min", Long.MIN_VALUE, true, Long.MIN_VALUE, true), new LongRange("max", Long.MAX_VALUE, true, Long.MAX_VALUE, true), new LongRange("all0", Long.MIN_VALUE, true, Long.MAX_VALUE, true), new LongRange("all1", Long.MIN_VALUE, false, Long.MAX_VALUE, true), new LongRange("all2", Long.MIN_VALUE, true, Long.MAX_VALUE, false), new LongRange("all3", Long.MIN_VALUE, false, Long.MAX_VALUE, false));
    FacetResult result = facets.getTopChildren(10, "field");
    assertEquals("dim=field path=[] value=3 childCount=6\n  min (1)\n  max (1)\n  all0 (3)\n  all1 (2)\n  all2 (2)\n  all3 (1)\n", result.toString());
    r.close();
    d.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MultiFacets(org.apache.lucene.facet.MultiFacets) Facets(org.apache.lucene.facet.Facets) IndexReader(org.apache.lucene.index.IndexReader) FacetResult(org.apache.lucene.facet.FacetResult) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Example 35 with FacetResult

use of org.apache.lucene.facet.FacetResult in project lucene-solr by apache.

the class TestRangeFacetCounts method testRandomLongs.

public void testRandomLongs() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    int numDocs = atLeast(1000);
    if (VERBOSE) {
        System.out.println("TEST: numDocs=" + numDocs);
    }
    long[] values = new long[numDocs];
    long minValue = Long.MAX_VALUE;
    long maxValue = Long.MIN_VALUE;
    for (int i = 0; i < numDocs; i++) {
        Document doc = new Document();
        long v = random().nextLong();
        values[i] = v;
        doc.add(new NumericDocValuesField("field", v));
        doc.add(new LongPoint("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, 100);
        LongRange[] ranges = new LongRange[numRange];
        int[] expectedCounts = new int[numRange];
        long minAcceptedValue = Long.MAX_VALUE;
        long maxAcceptedValue = Long.MIN_VALUE;
        for (int rangeID = 0; rangeID < numRange; rangeID++) {
            long min;
            if (rangeID > 0 && random().nextInt(10) == 7) {
                // Use an existing boundary:
                LongRange prevRange = ranges[random().nextInt(rangeID)];
                if (random().nextBoolean()) {
                    min = prevRange.min;
                } else {
                    min = prevRange.max;
                }
            } else {
                min = random().nextLong();
            }
            long max;
            if (rangeID > 0 && random().nextInt(10) == 7) {
                // Use an existing boundary:
                LongRange prevRange = ranges[random().nextInt(rangeID)];
                if (random().nextBoolean()) {
                    max = prevRange.min;
                } else {
                    max = prevRange.max;
                }
            } else {
                max = random().nextLong();
            }
            if (min > max) {
                long x = min;
                min = max;
                max = x;
            }
            boolean minIncl;
            boolean maxIncl;
            // NOTE: max - min >= 0 is here to handle the common overflow case!
            if (max - min >= 0 && max - min < 2) {
                // If max == min or max == min+1, we always do inclusive, else we might pass an empty range and hit exc from LongRange's ctor:
                minIncl = true;
                maxIncl = true;
            } else {
                minIncl = random().nextBoolean();
                maxIncl = random().nextBoolean();
            }
            ranges[rangeID] = new LongRange("r" + rangeID, min, minIncl, max, maxIncl);
            if (VERBOSE) {
                System.out.println("  range " + rangeID + ": " + ranges[rangeID]);
            }
            // 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 fastMatchQuery;
        if (random().nextBoolean()) {
            if (random().nextBoolean()) {
                fastMatchQuery = LongPoint.newRangeQuery("field", minValue, maxValue);
            } else {
                fastMatchQuery = LongPoint.newRangeQuery("field", minAcceptedValue, maxAcceptedValue);
            }
        } else {
            fastMatchQuery = null;
        }
        LongValuesSource vs = LongValuesSource.fromLongField("field");
        Facets facets = new LongRangeFacetCounts("field", vs, sfc, fastMatchQuery, 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());
            LongRange range = ranges[rangeID];
            // Test drill-down:
            DrillDownQuery ddq = new DrillDownQuery(config);
            if (random().nextBoolean()) {
                ddq.add("field", LongPoint.newRangeQuery("field", range.min, range.max));
            } else {
                ddq.add("field", range.getQuery(fastMatchQuery, vs));
            }
            assertEquals(expectedCounts[rangeID], s.search(ddq, 10).totalHits);
        }
    }
    w.close();
    IOUtils.close(r, dir);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FacetsConfig(org.apache.lucene.facet.FacetsConfig) 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) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LabelAndValue(org.apache.lucene.facet.LabelAndValue) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) FacetsCollector(org.apache.lucene.facet.FacetsCollector) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) FacetResult(org.apache.lucene.facet.FacetResult) LongValuesSource(org.apache.lucene.search.LongValuesSource) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

FacetResult (org.apache.lucene.facet.FacetResult)68 Facets (org.apache.lucene.facet.Facets)47 FacetsCollector (org.apache.lucene.facet.FacetsCollector)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)36 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)29 LabelAndValue (org.apache.lucene.facet.LabelAndValue)28 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)23 DirectoryReader (org.apache.lucene.index.DirectoryReader)22 ArrayList (java.util.ArrayList)21 Directory (org.apache.lucene.store.Directory)21 FacetsConfig (org.apache.lucene.facet.FacetsConfig)19 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)19 Document (org.apache.lucene.document.Document)18 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)14 DefaultSortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState)13 SortedSetDocValuesFacetCounts (org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts)13 SortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState)13 IOException (java.io.IOException)12 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)12 FacetField (org.apache.lucene.facet.FacetField)11