Java Code Examples for org.apache.lucene.facet.Facets#getAllDims()

The following examples show how to use org.apache.lucene.facet.Facets#getAllDims() . 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: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testGetFacetResultsTwice() throws Exception {
  // LUCENE-4893: counts were multiplied as many times as getFacetResults was called.
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  FacetsConfig config = new FacetsConfig();

  Document doc = new Document();
  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);

  Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config);
  
  List<FacetResult> res1 = facets.getAllDims(10);
  List<FacetResult> res2 = facets.getAllDims(10);
  assertEquals("calling getFacetResults twice should return the .equals()=true result", res1, res2);

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example 2
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 3
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCountRoot() throws Exception {
  // LUCENE-4882: FacetsAccumulator threw NPE if a FacetRequest was defined on CP.EMPTY
  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();
    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);
  
  Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config);
  
  for (FacetResult result : facets.getAllDims(10)) {
    assertEquals(r.numDocs(), result.value.intValue());
  }

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example 4
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testWrongIndexFieldName() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();

  // Writes facet ords to a separate directory from the
  // main index:
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);

  FacetsConfig config = new FacetsConfig();
  config.setIndexFieldName("a", "$facets2");
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  Document doc = new Document();
  doc.add(new FacetField("a", "foo1"));
  writer.addDocument(config.build(taxoWriter, doc));

  // NRT open
  IndexSearcher searcher = newSearcher(writer.getReader());

  // NRT open
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);

  FacetsCollector c = new FacetsCollector();
  searcher.search(new MatchAllDocsQuery(), c);

  // Uses default $facets field:
  Facets facets;
  if (random().nextBoolean()) {
    facets = new FastTaxonomyFacetCounts(taxoReader, config, c);
  } else {
    OrdinalsReader ordsReader = new DocValuesOrdinalsReader();
    if (random().nextBoolean()) {
      ordsReader = new CachedOrdinalsReader(ordsReader);
    }
    facets = new TaxonomyFacetCounts(ordsReader, taxoReader, config, c);
  }

  // Ask for top 10 labels for any dims that have counts:
  List<FacetResult> results = facets.getAllDims(10);
  assertTrue(results.isEmpty());

  expectThrows(IllegalArgumentException.class, () -> {
    facets.getSpecificValue("a");
  });

  expectThrows(IllegalArgumentException.class, () -> {
    facets.getTopChildren(10, "a");
  });

  writer.close();
  IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, taxoDir, dir);
}