org.apache.lucene.facet.FacetsCollector Java Examples

The following examples show how to use org.apache.lucene.facet.FacetsCollector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntTaxonomyFacets.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Return true if a sparse hash table should be used for counting, instead of a dense int[]. */
protected boolean useHashTable(FacetsCollector fc, TaxonomyReader taxoReader) {
  if (taxoReader.getSize() < 1024) {
    // small number of unique values: use an array
    return false;
  }

  if (fc == null) {
    // counting all docs: use an array
    return false;
  }
  
  int maxDoc = 0;
  int sumTotalHits = 0;
  for (MatchingDocs docs : fc.getMatchingDocs()) {
    sumTotalHits += docs.totalHits;
    maxDoc += docs.context.reader().maxDoc();
  }

  // if our result set is < 10% of the index, we collect sparsely (use hash map):
  return sumTotalHits < maxDoc/10;
}
 
Example #2
Source File: FacetStorageTest.java    From lumongo with Apache License 2.0 6 votes vote down vote up
/** User drills down on 'Publish Year/2010'. */
private FacetResult drillDown() throws IOException {
	DirectoryReader indexReader = DirectoryReader.open(directory);
	IndexSearcher searcher = new IndexSearcher(indexReader);
	SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);
	
	// Now user drills down on Publish Year/2010:
	DrillDownQuery q = new DrillDownQuery(config);
	q.add("Publish Year", "2010");
	FacetsCollector fc = new FacetsCollector();
	FacetsCollector.search(searcher, q, 10, fc);
	
	// Retrieve results
	Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
	FacetResult result = facets.getTopChildren(10, "Author");
	indexReader.close();
	
	return result;
}
 
Example #3
Source File: TestTaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testNoScore() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();

  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  FacetsConfig config = new FacetsConfig();
  for (int i = 0; i < 4; i++) {
    Document doc = new Document();
    doc.add(new NumericDocValuesField("price", (i+1)));
    doc.add(new FacetField("a", Integer.toString(i % 2)));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);

  FacetsCollector sfc = new FacetsCollector();
  newSearcher(r).search(new MatchAllDocsQuery(), sfc);
  Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, sfc, DoubleValuesSource.fromLongField("price"));
  assertEquals("dim=a path=[] value=10.0 childCount=2\n  1 (6.0)\n  0 (4.0)\n", facets.getTopChildren(10, "a").toString());

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example #4
Source File: TestSortedSetDocValuesFacets.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static Facets getAllFacets(IndexSearcher searcher, SortedSetDocValuesReaderState state,
                                   ExecutorService exec) throws IOException, InterruptedException {
  if (random().nextBoolean()) {
    FacetsCollector c = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), c);
    if (exec != null) {
      return new ConcurrentSortedSetDocValuesFacetCounts(state, c, exec);
    } else {
      return new SortedSetDocValuesFacetCounts(state, c);
    }
  } else if (exec != null) {
    return new ConcurrentSortedSetDocValuesFacetCounts(state, exec);
  } else {
    return new SortedSetDocValuesFacetCounts(state);
  }
}
 
Example #5
Source File: AssociationsFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User drills down on 'tags/solr'. */
private FacetResult drillDown() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

  // Passing no baseQuery means we drill down on all
  // documents ("browse only"):
  DrillDownQuery q = new DrillDownQuery(config);

  // Now user drills down on Publish Date/2010:
  q.add("tags", "solr");
  FacetsCollector fc = new FacetsCollector();
  FacetsCollector.search(searcher, q, 10, fc);

  // Retrieve results
  Facets facets = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc);
  FacetResult result = facets.getTopChildren(10, "genre");

  indexReader.close();
  taxoReader.close();
  
  return result;
}
 
Example #6
Source File: AssociationsFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User runs a query and aggregates facets by summing their association values. */
private List<FacetResult> sumAssociations() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  
  FacetsCollector fc = new FacetsCollector();
  
  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
  
  Facets tags = new TaxonomyFacetSumIntAssociations("$tags", taxoReader, config, fc);
  Facets genre = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc);

  // Retrieve results
  List<FacetResult> results = new ArrayList<>();
  results.add(tags.getTopChildren(10, "tags"));
  results.add(genre.getTopChildren(10, "genre"));

  indexReader.close();
  taxoReader.close();
  
  return results;
}
 
Example #7
Source File: DistanceFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User runs a query and counts facets. */
public FacetResult search() throws IOException {

  FacetsCollector fc = new FacetsCollector();

  searcher.search(new MatchAllDocsQuery(), fc);

  Facets facets = new DoubleRangeFacetCounts("field", getDistanceValueSource(), fc,
                                             getBoundingBoxQuery(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, 10.0),
                                             ONE_KM,
                                             TWO_KM,
                                             FIVE_KM,
                                             TEN_KM);

  return facets.getTopChildren(10, "field");
}
 
Example #8
Source File: SimpleSortedSetFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User drills down on 'Publish Year/2010'. */
private FacetResult drillDown() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);

  // Now user drills down on Publish Year/2010:
  DrillDownQuery q = new DrillDownQuery(config);
  q.add("Publish Year", "2010");
  FacetsCollector fc = new FacetsCollector();
  FacetsCollector.search(searcher, q, 10, fc);

  // Retrieve results
  Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
  FacetResult result = facets.getTopChildren(10, "Author");
  indexReader.close();
  
  return result;
}
 
Example #9
Source File: SimpleSortedSetFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User runs a query and counts facets. */
private List<FacetResult> search() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);

  // Aggregatses the facet counts
  FacetsCollector fc = new FacetsCollector();

  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

  // Retrieve results
  Facets facets = new SortedSetDocValuesFacetCounts(state, fc);

  List<FacetResult> results = new ArrayList<>();
  results.add(facets.getTopChildren(10, "Author"));
  results.add(facets.getTopChildren(10, "Publish Year"));
  indexReader.close();
  
  return results;
}
 
Example #10
Source File: SimpleFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User drills down on 'Publish Date/2010', and we
 *  return facets for 'Author' */
private FacetResult drillDown() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

  // Passing no baseQuery means we drill down on all
  // documents ("browse only"):
  DrillDownQuery q = new DrillDownQuery(config);

  // Now user drills down on Publish Date/2010:
  q.add("Publish Date", "2010");
  FacetsCollector fc = new FacetsCollector();
  FacetsCollector.search(searcher, q, 10, fc);

  // Retrieve results
  Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
  FacetResult result = facets.getTopChildren(10, "Author");

  indexReader.close();
  taxoReader.close();
  
  return result;
}
 
Example #11
Source File: SimpleFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User runs a query and counts facets. */
private List<FacetResult> facetsWithSearch() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

  FacetsCollector fc = new FacetsCollector();

  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

  // Retrieve results
  List<FacetResult> results = new ArrayList<>();

  // Count both "Publish Date" and "Author" dimensions
  Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
  results.add(facets.getTopChildren(10, "Author"));
  results.add(facets.getTopChildren(10, "Publish Date"));
  
  indexReader.close();
  taxoReader.close();
  
  return results;
}
 
Example #12
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static Facets getAllFacets(String indexFieldName, IndexSearcher searcher, TaxonomyReader taxoReader, FacetsConfig config) throws IOException {
  if (random().nextBoolean()) {
    // Aggregate the facet counts:
    FacetsCollector c = new FacetsCollector();

    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query, and use MultiCollector to
    // wrap collecting the "normal" hits and also facets:
    searcher.search(new MatchAllDocsQuery(), c);

    return new FastTaxonomyFacetCounts(taxoReader, config, c);
  } else {
    return new FastTaxonomyFacetCounts(indexFieldName, searcher.getIndexReader(), taxoReader, config);
  }
}
 
Example #13
Source File: RangeFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User runs a query and counts facets. */
public FacetResult search() throws IOException {

  // Aggregates the facet counts
  FacetsCollector fc = new FacetsCollector();

  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

  Facets facets = new LongRangeFacetCounts("timestamp", fc,
                                           PAST_HOUR,
                                           PAST_SIX_HOURS,
                                           PAST_DAY);
  return facets.getTopChildren(10, "timestamp");
}
 
Example #14
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSegmentsWithoutCategoriesOrResults() throws Exception {
  // tests the accumulator when there are segments with no results
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
  iwc.setMergePolicy(NoMergePolicy.INSTANCE); // prevent merges
  IndexWriter indexWriter = new IndexWriter(indexDir, iwc);

  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  FacetsConfig config = new FacetsConfig();
  indexTwoDocs(taxoWriter, indexWriter, config, false); // 1st segment, no content, with categories
  indexTwoDocs(taxoWriter, indexWriter, null, true);         // 2nd segment, with content, no categories
  indexTwoDocs(taxoWriter, indexWriter, config, true);  // 3rd segment ok
  indexTwoDocs(taxoWriter, indexWriter, null, false);        // 4th segment, no content, or categories
  indexTwoDocs(taxoWriter, indexWriter, null, true);         // 5th segment, with content, no categories
  indexTwoDocs(taxoWriter, indexWriter, config, true);  // 6th segment, with content, with categories
  indexTwoDocs(taxoWriter, indexWriter, null, true);         // 7th segment, with content, no categories
  indexWriter.close();
  IOUtils.close(taxoWriter);

  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  IndexSearcher indexSearcher = newSearcher(indexReader);
  
  // search for "f:a", only segments 1 and 3 should match results
  Query q = new TermQuery(new Term("f", "a"));
  FacetsCollector sfc = new FacetsCollector();
  indexSearcher.search(q, sfc);
  Facets facets = getTaxonomyFacetCounts(taxoReader, config, sfc);
  FacetResult result = facets.getTopChildren(10, "A");
  assertEquals("wrong number of children", 2, result.labelValues.length);
  for (LabelAndValue labelValue : result.labelValues) {
    assertEquals("wrong weight for child " + labelValue.label, 2, labelValue.value.intValue());
  }

  IOUtils.close(indexReader, taxoReader, indexDir, taxoDir);
}
 
Example #15
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #16
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testLongGetAllDims() 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));

  List<FacetResult> result = facets.getAllDims(10);
  assertEquals(1, result.size());
  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.get(0).toString());
  
  r.close();
  d.close();
}
 
Example #17
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFloatSumAssociation() throws Exception {
  FacetsCollector fc = new FacetsCollector();
  
  IndexSearcher searcher = newSearcher(reader);
  searcher.search(new MatchAllDocsQuery(), fc);
  
  Facets facets = new TaxonomyFacetSumFloatAssociations("$facets.float", taxoReader, config, fc);
  assertEquals("dim=float path=[] value=-1.0 childCount=2\n  a (50.0)\n  b (9.999995)\n", facets.getTopChildren(10, "float").toString());
  assertEquals("Wrong count for category 'a'!", 50f, facets.getSpecificValue("float", "a").floatValue(), 0.00001);
  assertEquals("Wrong count for category 'b'!", 10f, facets.getSpecificValue("float", "b").floatValue(), 0.00001);
}
 
Example #18
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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();
  }
 
Example #19
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #20
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testBasicDouble() throws Exception {
  Directory d = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), d);
  Document doc = new Document();
  DoubleDocValuesField field = new DoubleDocValuesField("field", 0.0);
  doc.add(field);
  for(long l=0;l<100;l++) {
    field.setDoubleValue(l);
    w.addDocument(doc);
  }

  IndexReader r = w.getReader();

  FacetsCollector fc = new FacetsCollector();

  IndexSearcher s = newSearcher(r);
  s.search(new MatchAllDocsQuery(), fc);
  Facets facets = new DoubleRangeFacetCounts("field", fc,
      new DoubleRange("less than 10", 0.0, true, 10.0, false),
      new DoubleRange("less than or equal to 10", 0.0, true, 10.0, true),
      new DoubleRange("over 90", 90.0, false, 100.0, false),
      new DoubleRange("90 or above", 90.0, true, 100.0, false),
      new DoubleRange("over 1000", 1000.0, false, Double.POSITIVE_INFINITY, false));
                                       
  assertEquals("dim=field path=[] value=21 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 (0)\n",
               facets.getTopChildren(10, "field").toString());
  w.close();
  IOUtils.close(r, d);
}
 
Example #21
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testMissingValues() 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++) {
    if (l % 5 == 0) {
      // Every 5th doc is missing the value:
      w.addDocument(new Document());
      continue;
    }
    field.setLongValue(l);
    w.addDocument(doc);
  }

  IndexReader r = w.getReader();

  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, false));
  
  assertEquals("dim=field path=[] value=16 childCount=5\n  less than 10 (8)\n  less than or equal to 10 (8)\n  over 90 (8)\n  90 or above (8)\n  over 1000 (0)\n",
               facets.getTopChildren(10, "field").toString());

  w.close();
  IOUtils.close(r, d);
}
 
Example #22
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testIntSumAssociation() throws Exception {
  
  FacetsCollector fc = new FacetsCollector();
  
  IndexSearcher searcher = newSearcher(reader);
  searcher.search(new MatchAllDocsQuery(), fc);

  Facets facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
  assertEquals("dim=int path=[] value=-1 childCount=2\n  a (200)\n  b (150)\n", facets.getTopChildren(10, "int").toString());
  assertEquals("Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
  assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
}
 
Example #23
Source File: TestTaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSumScoreAggregator() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();

  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));

  FacetsConfig config = new FacetsConfig();

  for(int i = atLeast(30); i > 0; --i) {
    Document doc = new Document();
    if (random().nextBoolean()) { // don't match all documents
      doc.add(new StringField("f", "v", Field.Store.NO));
    }
    doc.add(new FacetField("dim", "a"));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
  
  FacetsCollector fc = new FacetsCollector(true);
  BoostQuery csq = new BoostQuery(new ConstantScoreQuery(new MatchAllDocsQuery()), 2f);
  
  TopDocs td = FacetsCollector.search(newSearcher(r), csq, 10, fc);

  Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
  
  int expected = (int) (csq.getBoost() * td.totalHits.value);
  assertEquals(expected, facets.getSpecificValue("dim", "a").intValue());

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example #24
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Make sure we can test both int and float assocs in one
 *  index, as long as we send each to a different field. */
public void testIntAndFloatAssocation() throws Exception {
  FacetsCollector fc = new FacetsCollector();
  
  IndexSearcher searcher = newSearcher(reader);
  searcher.search(new MatchAllDocsQuery(), fc);
  
  Facets facets = new TaxonomyFacetSumFloatAssociations("$facets.float", taxoReader, config, fc);
  assertEquals("Wrong count for category 'a'!", 50f, facets.getSpecificValue("float", "a").floatValue(), 0.00001);
  assertEquals("Wrong count for category 'b'!", 10f, facets.getSpecificValue("float", "b").floatValue(), 0.00001);
  
  facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
  assertEquals("Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
  assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
}
 
Example #25
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWrongIndexFieldName() throws Exception {
  FacetsCollector fc = new FacetsCollector();
  
  IndexSearcher searcher = newSearcher(reader);
  searcher.search(new MatchAllDocsQuery(), fc);
  Facets facets = new TaxonomyFacetSumFloatAssociations(taxoReader, config, fc);
  expectThrows(IllegalArgumentException.class, () -> {
    facets.getSpecificValue("float");
  });

  expectThrows(IllegalArgumentException.class, () -> {
    facets.getTopChildren(10, "float");
  });
}
 
Example #26
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testIntSumAssociationDrillDown() throws Exception {
  FacetsCollector fc = new FacetsCollector();
  
  IndexSearcher searcher = newSearcher(reader);
  DrillDownQuery q = new DrillDownQuery(config);
  q.add("int", "b");
  searcher.search(q, fc);

  Facets facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
  assertEquals("dim=int path=[] value=-1 childCount=2\n  b (150)\n  a (100)\n", facets.getTopChildren(10, "int").toString());
  assertEquals("Wrong count for category 'a'!", 100, facets.getSpecificValue("int", "a").intValue());
  assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
}
 
Example #27
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSeparateIndexedFields() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  FacetsConfig config = new FacetsConfig();
  config.setIndexFieldName("b", "$b");
  
  for(int i = atLeast(30); i > 0; --i) {
    Document doc = new Document();
    doc.add(new StringField("f", "v", Field.Store.NO));
    doc.add(new FacetField("a", "1"));
    doc.add(new FacetField("b", "1"));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
  
  FacetsCollector sfc = new FacetsCollector();
  newSearcher(r).search(new MatchAllDocsQuery(), sfc);
  Facets facets1 = getTaxonomyFacetCounts(taxoReader, config, sfc);
  Facets facets2 = getTaxonomyFacetCounts(taxoReader, config, sfc, "$b");
  assertEquals(r.maxDoc(), facets1.getTopChildren(10, "a").value.intValue());
  assertEquals(r.maxDoc(), facets2.getTopChildren(10, "b").value.intValue());
  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example #28
Source File: TestTaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCountAndSumScore() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  FacetsConfig config = new FacetsConfig();
  config.setIndexFieldName("b", "$b");
  
  for(int i = atLeast(30); i > 0; --i) {
    Document doc = new Document();
    doc.add(new StringField("f", "v", Field.Store.NO));
    doc.add(new FacetField("a", "1"));
    doc.add(new FacetField("b", "1"));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
  
  FacetsCollector fc = new FacetsCollector(true);
  FacetsCollector.search(newSearcher(r), new MatchAllDocsQuery(), 10, fc);
  
  Facets facets1 = getTaxonomyFacetCounts(taxoReader, config, fc);
  Facets facets2 = new TaxonomyFacetSumValueSource(new DocValuesOrdinalsReader("$b"), taxoReader, config, fc, DoubleValuesSource.SCORES);

  assertEquals(r.maxDoc(), facets1.getTopChildren(10, "a").value.intValue());
  assertEquals(r.maxDoc(), facets2.getTopChildren(10, "b").value.doubleValue(), 1E-10);
  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example #29
Source File: ExpressionAggregationFacetsExample.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** User runs a query and aggregates facets. */
private FacetResult search() throws IOException, ParseException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

  // Aggregate categories by an expression that combines the document's score
  // and its popularity field
  Expression expr = JavascriptCompiler.compile("_score * sqrt(popularity)");
  SimpleBindings bindings = new SimpleBindings();
  bindings.add("_score", DoubleValuesSource.SCORES); // the score of the document
  bindings.add("popularity", DoubleValuesSource.fromLongField("popularity")); // the value of the 'popularity' field

  // Aggregates the facet values
  FacetsCollector fc = new FacetsCollector(true);

  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

  // Retrieve results
  Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, expr.getDoubleValuesSource(bindings));
  FacetResult result = facets.getTopChildren(10, "A");
  
  indexReader.close();
  taxoReader.close();
  
  return result;
}
 
Example #30
Source File: MultiCategoryListsFacetsExample.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** User runs a query and counts facets. */
private List<FacetResult> search() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

  FacetsCollector fc = new FacetsCollector();

  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

  // Retrieve results
  List<FacetResult> results = new ArrayList<>();

  // Count both "Publish Date" and "Author" dimensions
  Facets author = new FastTaxonomyFacetCounts("author", taxoReader, config, fc);
  results.add(author.getTopChildren(10, "Author"));

  Facets pubDate = new FastTaxonomyFacetCounts("pubdate", taxoReader, config, fc);
  results.add(pubDate.getTopChildren(10, "Publish Date"));

  indexReader.close();
  taxoReader.close();

  return results;
}