org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader Java Examples

The following examples show how to use org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader. 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: TestDrillSideways.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEmptyIndex() throws Exception {
  // LUCENE-5045: make sure DrillSideways works with an empty index
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
  IndexSearcher searcher = newSearcher(writer.getReader());
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);

  // Count "Author"
  FacetsConfig config = new FacetsConfig();
  DrillSideways ds = getNewDrillSideways(searcher, config, taxoReader);
  DrillDownQuery ddq = new DrillDownQuery(config);
  ddq.add("Author", "Lisa");

  DrillSidewaysResult r = ds.search(ddq, 10); // this used to fail on IllegalArgEx
  assertEquals(0, r.hits.totalHits.value);

  r = ds.search(ddq, null, null, 10, new Sort(new SortField("foo", SortField.Type.INT)), false); // this used to fail on IllegalArgEx
  assertEquals(0, r.hits.totalHits.value);

  writer.close();
  IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, dir, taxoDir);
}
 
Example #2
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testChildCount() throws Exception {
  // LUCENE-4885: FacetResult.numValidDescendants was not set properly by FacetsAccumulator
  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 < 10; i++) {
    Document doc = new Document();
    doc.add(new FacetField("a", Integer.toString(i)));
    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);
  
  assertEquals(10, facets.getTopChildren(2, "a").childCount);

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example #3
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 #4
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**  Test writing an empty index, and seeing that a reader finds in it
  the root category, and only it. We check all the methods on that
  root category return the expected results.
 */
@Test
public void testRootOnly() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  // right after opening the index, it should already contain the
  // root, so have size 1:
  assertEquals(1, tw.getSize());
  tw.close();
  TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
  assertEquals(1, tr.getSize());
  assertEquals(0, tr.getPath(0).length);
  assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getParallelTaxonomyArrays().parents()[0]);
  assertEquals(0, tr.getOrdinal(new FacetLabel()));
  tr.close();
  indexDir.close();
}
 
Example #5
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testNRT() throws Exception {
  Directory dir = newDirectory();
  DirectoryTaxonomyWriter writer = new DirectoryTaxonomyWriter(dir);
  TaxonomyReader reader = new DirectoryTaxonomyReader(writer);
  
  FacetLabel cp = new FacetLabel("a");
  writer.addCategory(cp);
  TaxonomyReader newReader = TaxonomyReader.openIfChanged(reader);
  assertNotNull("expected a new instance", newReader);
  assertEquals(2, newReader.getSize());
  assertNotSame(TaxonomyReader.INVALID_ORDINAL, newReader.getOrdinal(cp));
  reader.close();
  reader = newReader;
  
  writer.close();
  reader.close();
  
  dir.close();
}
 
Example #6
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 #7
Source File: OLuceneFacetManager.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
public void addFacetContext(QueryContext queryContext, Object key) throws IOException {
  queryContext.setFacet(true);
  queryContext.setFacetField(facetField);
  queryContext.setFacetConfig(config);
  // queryContext.setfacetDim(facetDim);
  queryContext.setReader(new DirectoryTaxonomyReader(getTaxDirectory(owner.getDatabase())));

  if (key instanceof OCompositeKey) {
    List<Object> keys = ((OCompositeKey) key).getKeys();
    for (Object o : keys) {
      if (o instanceof Map) {
        String drillDown = (String) ((Map) o).get("drillDown");
        queryContext.setDrillDownQuery(drillDown);
      }
    }
  }
}
 
Example #8
Source File: LuceneNativeFacet.java    From orientdb-lucene 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<FacetResult>();

    // 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 #9
Source File: PrintTaxonomyStats.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Command-line tool. */
@SuppressForbidden(reason = "System.out required: command line tool")
public static void main(String[] args) throws IOException {
  boolean printTree = false;
  String path = null;
  for(int i=0;i<args.length;i++) {
    if (args[i].equals("-printTree")) {
      printTree = true;
    } else {
      path = args[i];
    }
  }
  if (args.length != (printTree ? 2 : 1)) {
    System.out.println("\nUsage: java -classpath ... org.apache.lucene.facet.util.PrintTaxonomyStats [-printTree] /path/to/taxononmy/index\n");
    System.exit(1);
  }
  Directory dir = FSDirectory.open(Paths.get(path));
  TaxonomyReader r = new DirectoryTaxonomyReader(dir);
  printStats(r, System.out, printTree);
  r.close();
  dir.close();
}
 
Example #10
Source File: LuceneNativeFacet.java    From orientdb-lucene 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: 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 #12
Source File: LumongoSegment.java    From lumongo with Apache License 2.0 6 votes vote down vote up
private void openReaderIfChanges() throws IOException {
	DirectoryReader newDirectoryReader = DirectoryReader
			.openIfChanged(directoryReader, indexWriter, indexConfig.getIndexSettings().getApplyUncommittedDeletes());
	if (newDirectoryReader != null) {
		directoryReader = newDirectoryReader;
		QueryResultCache qrc = queryResultCache;
		if (qrc != null) {
			qrc.clear();
		}

	}

	DirectoryTaxonomyReader newone = TaxonomyReader.openIfChanged(taxoReader);
	if (newone != null) {
		taxoReader = newone;
	}
}
 
Example #13
Source File: LumongoSegment.java    From lumongo with Apache License 2.0 6 votes vote down vote up
private void openIndexWriters() throws Exception {
	if (this.indexWriter != null) {
		indexWriter.close();
	}
	if (this.taxoWriter != null) {
		taxoWriter.close();
	}

	this.perFieldAnalyzer = this.indexSegmentInterface.getPerFieldAnalyzer();

	this.indexWriter = this.indexSegmentInterface.getIndexWriter(segmentNumber);
	if (this.directoryReader != null) {
		this.directoryReader.close();
	}
	this.directoryReader = DirectoryReader.open(indexWriter, indexConfig.getIndexSettings().getApplyUncommittedDeletes(), false);

	this.taxoWriter = this.indexSegmentInterface.getTaxoWriter(segmentNumber);
	if (this.taxoReader != null) {
		this.taxoReader.close();
	}
	this.taxoReader = new DirectoryTaxonomyReader(taxoWriter);
}
 
Example #14
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 #15
Source File: LumongoSegment.java    From lumongo with Apache License 2.0 6 votes vote down vote up
private void reopenIndexWritersIfNecessary() throws Exception {
	if (!indexWriter.isOpen()) {
		synchronized (this) {
			if (!indexWriter.isOpen()) {
				this.indexWriter = this.indexSegmentInterface.getIndexWriter(segmentNumber);
				this.directoryReader = DirectoryReader.open(indexWriter, indexConfig.getIndexSettings().getApplyUncommittedDeletes(), false);
			}
		}
	}

	//TODO: is this a real use case?
	try {
		taxoWriter.getSize();
	}
	catch (AlreadyClosedException e) {
		synchronized (this) {
			this.taxoWriter = this.indexSegmentInterface.getTaxoWriter(segmentNumber);
			this.taxoReader = new DirectoryTaxonomyReader(taxoWriter);
		}
	}

}
 
Example #16
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 #17
Source File: LuceneNativeFacet.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
/** User drills down on 'Publish Date/2010', and we
 *  return facets for both 'Publish Date' and 'Author',
 *  using DrillSideways. */
private List<FacetResult> drillSideways() 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");

    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
    DrillSidewaysResult result = ds.search(q, 10);

    // Retrieve results

    List<FacetResult> facets = result.facets.getAllDims(10);

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

    return facets;
}
 
Example #18
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 #19
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 both 'Publish Date' and 'Author',
 *  using DrillSideways. */
private List<FacetResult> drillSideways() 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");

  DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
  DrillSidewaysResult result = ds.search(q, 10);

  // Retrieve results
  List<FacetResult> facets = result.facets.getAllDims(10);

  indexReader.close();
  taxoReader.close();
  
  return facets;
}
 
Example #20
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaxonomyReaderRefreshRaces() throws Exception {
  // compute base child arrays - after first chunk, and after the other
  Directory indexDirBase = newDirectory();
  TaxonomyWriter twBase = new DirectoryTaxonomyWriter(indexDirBase);
  twBase.addCategory(new FacetLabel("a", "0"));
  final FacetLabel abPath = new FacetLabel("a", "b");
  twBase.addCategory(abPath);
  twBase.commit();
  TaxonomyReader trBase = new DirectoryTaxonomyReader(indexDirBase);

  final ParallelTaxonomyArrays ca1 = trBase.getParallelTaxonomyArrays();
  
  final int abOrd = trBase.getOrdinal(abPath);
  final int abYoungChildBase1 = ca1.children()[abOrd]; 
  
  final int numCategories = atLeast(200);
  for (int i = 0; i < numCategories; i++) {
    twBase.addCategory(new FacetLabel("a", "b", Integer.toString(i)));
  }
  twBase.close();
  
  TaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(trBase);
  assertNotNull(newTaxoReader);
  trBase.close();
  trBase = newTaxoReader;
  
  final ParallelTaxonomyArrays ca2 = trBase.getParallelTaxonomyArrays();
  final int abYoungChildBase2 = ca2.children()[abOrd];
  
  int numRetries = atLeast(10);
  for (int retry = 0; retry < numRetries; retry++) {
    assertConsistentYoungestChild(abPath, abOrd, abYoungChildBase1, abYoungChildBase2, retry, numCategories);
  }
  
  trBase.close();
  indexDirBase.close();
}
 
Example #21
Source File: LuceneNativeFacet.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
/** User runs a query and counts facets only without collecting the matching documents.*/
private List<FacetResult> facetsOnly() 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:
    searcher.search(new MatchAllDocsQuery(), null /*Filter */, fc);

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

    // 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 #22
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 #23
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**  The following test is exactly the same as testRootOnly, except we
 *  do not close the writer before opening the reader. We want to see
 *  that the root is visible to the reader not only after the writer is
 *  closed, but immediately after it is created.
 */
@Test
public void testRootOnly2() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  tw.commit();
  TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
  assertEquals(1, tr.getSize());
  assertEquals(0, tr.getPath(0).length);
  assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getParallelTaxonomyArrays().parents()[0]);
  assertEquals(0, tr.getOrdinal(new FacetLabel()));
  tw.close();
  tr.close();
  indexDir.close();
}
 
Example #24
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**  Tests for TaxonomyReader's getParent() method.
  We check it by comparing its results to those we could have gotten by
  looking at the category string paths (where the parentage is obvious).
  Note that after testReaderBasic(), we already know we can trust the
  ordinal &lt;=&gt; category conversions.
  
  Note: At the moment, the parent methods in the reader are deprecated,
  but this does not mean they should not be tested! Until they are
  removed (*if* they are removed), these tests should remain to see
  that they still work correctly.
 */

@Test
public void testReaderParent() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  fillTaxonomy(tw);
  tw.close();
  TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);

  // check that the parent of the root ordinal is the invalid ordinal:
  int[] parents = tr.getParallelTaxonomyArrays().parents();
  assertEquals(TaxonomyReader.INVALID_ORDINAL, parents[0]);

  // check parent of non-root ordinals:
  for (int ordinal=1; ordinal<tr.getSize(); ordinal++) {
    FacetLabel me = tr.getPath(ordinal);
    int parentOrdinal = parents[ordinal];
    FacetLabel parent = tr.getPath(parentOrdinal);
    if (parent==null) {
      fail("Parent of "+ordinal+" is "+parentOrdinal+
      ", but this is not a valid category.");
    }
    // verify that the parent is indeed my parent, according to the strings
    if (!me.subpath(me.length-1).equals(parent)) {
      fail("Got parent "+parentOrdinal+" for ordinal "+ordinal+
          " but categories are "+showcat(parent)+" and "+showcat(me)+
          " respectively.");
    }
  }

  tr.close();
  indexDir.close();
}
 
Example #25
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriterParent2() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  fillTaxonomy(tw);
  tw.commit();
  TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
  
  checkWriterParent(tr, tw);
  
  tw.close();
  tr.close();
  indexDir.close();
}
 
Example #26
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Test how getChildrenArrays() deals with the taxonomy's growth:
 */
@Test
public void testChildrenArraysGrowth() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  tw.addCategory(new FacetLabel("hi", "there"));
  tw.commit();
  TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
  ParallelTaxonomyArrays ca = tr.getParallelTaxonomyArrays();
  assertEquals(3, tr.getSize());
  assertEquals(3, ca.siblings().length);
  assertEquals(3, ca.children().length);
  assertTrue(Arrays.equals(new int[] { 1, 2, -1 }, ca.children()));
  assertTrue(Arrays.equals(new int[] { -1, -1, -1 }, ca.siblings()));
  tw.addCategory(new FacetLabel("hi", "ho"));
  tw.addCategory(new FacetLabel("hello"));
  tw.commit();
  // Before refresh, nothing changed..
  ParallelTaxonomyArrays newca = tr.getParallelTaxonomyArrays();
  assertSame(newca, ca); // we got exactly the same object
  assertEquals(3, tr.getSize());
  assertEquals(3, ca.siblings().length);
  assertEquals(3, ca.children().length);
  // After the refresh, things change:
  TaxonomyReader newtr = TaxonomyReader.openIfChanged(tr);
  assertNotNull(newtr);
  tr.close();
  tr = newtr;
  ca = tr.getParallelTaxonomyArrays();
  assertEquals(5, tr.getSize());
  assertEquals(5, ca.siblings().length);
  assertEquals(5, ca.children().length);
  assertTrue(Arrays.equals(new int[] { 4, 3, -1, -1, -1 }, ca.children()));
  assertTrue(Arrays.equals(new int[] { -1, -1, -1, 2, 1 }, ca.siblings()));
  tw.close();
  tr.close();
  indexDir.close();
}
 
Example #27
Source File: TestTaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testRollupValues() 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.setHierarchical("a", true);
  //config.setRequireDimCount("a", true);
  
  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), "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 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 #28
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSeparateReaderAndWriter2() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  tw.commit();
  TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);

  // Test getOrdinal():
  FacetLabel author = new FacetLabel("Author");

  assertEquals(1, tr.getSize()); // the empty taxonomy has size 1 (the root)
  assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
  tw.addCategory(author);
  // before commit and refresh, no change:
  assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
  assertEquals(1, tr.getSize()); // still root only...
  assertNull(TaxonomyReader.openIfChanged(tr)); // this is not enough, because tw.commit() hasn't been done yet
  assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
  assertEquals(1, tr.getSize()); // still root only...
  tw.commit();
  // still not enough before refresh:
  assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
  assertEquals(1, tr.getSize()); // still root only...
  TaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(tr);
  assertNotNull(newTaxoReader);
  tr.close();
  tr = newTaxoReader;
  assertEquals(1, tr.getOrdinal(author));
  assertEquals(2, tr.getSize());
  tw.close();
  tr.close();
  indexDir.close();
}
 
Example #29
Source File: TestSearcherTaxonomyManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private SearcherTaxonomyManager getSearcherTaxonomyManager(Directory indexDir, Directory taxoDir, SearcherFactory searcherFactory) throws IOException {
  if (random().nextBoolean()) {
    return new SearcherTaxonomyManager(indexDir, taxoDir, searcherFactory);
  } else {
    IndexReader reader = DirectoryReader.open(indexDir);
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    return new SearcherTaxonomyManager(reader, taxoReader, searcherFactory);
  }
}
 
Example #30
Source File: TestOrdinalMappingLeafReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void verifyResults(Directory indexDir, Directory taxoDir) throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  IndexSearcher searcher = newSearcher(indexReader);
  
  FacetsCollector collector = new FacetsCollector();
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, collector);

  // tag facets
  Facets tagFacets = new FastTaxonomyFacetCounts("$tags", taxoReader, facetConfig, collector);
  FacetResult result = tagFacets.getTopChildren(10, "tag");
  for (LabelAndValue lv: result.labelValues) {
    if (VERBOSE) {
      System.out.println(lv);
    }
    assertEquals(NUM_DOCS, lv.value.intValue());
  }
  
  // id facets
  Facets idFacets = new FastTaxonomyFacetCounts(taxoReader, facetConfig, collector);
  FacetResult idResult = idFacets.getTopChildren(10, "id");
  assertEquals(NUM_DOCS, idResult.childCount);
  assertEquals(NUM_DOCS * 2, idResult.value); // each "id" appears twice
  
  BinaryDocValues bdv = MultiDocValues.getBinaryValues(indexReader, "bdv");
  BinaryDocValues cbdv = MultiDocValues.getBinaryValues(indexReader, "cbdv");
  for (int i = 0; i < indexReader.maxDoc(); i++) {
    assertEquals(i, bdv.nextDoc());
    assertEquals(i, cbdv.nextDoc());
    assertEquals(Integer.parseInt(cbdv.binaryValue().utf8ToString()), Integer.parseInt(bdv.binaryValue().utf8ToString())*2);
  }
  IOUtils.close(indexReader, taxoReader);
}