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

The following examples show how to use org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter. 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: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**  testWriterTwice is exactly like testWriter, except that after adding
  all the categories, we add them again, and see that we get the same
  old ids again - not new categories.
 */
@Test
public void testWriterTwice() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  fillTaxonomy(tw);
  // run fillTaxonomy again - this will try to add the same categories
  // again, and check that we see the same ordinal paths again, not
  // different ones. 
  fillTaxonomy(tw);
  // Let's check the number of categories again, to see that no
  // extraneous categories were created:
  assertEquals(expectedCategories.length, tw.getSize());    
  tw.close();
  indexDir.close();
}
 
Example #2
Source File: TestOrdinalMappingLeafReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testTaxonomyMergeUtils() throws Exception {
  Directory srcIndexDir = newDirectory();
  Directory srcTaxoDir = newDirectory();
  buildIndexWithFacets(srcIndexDir, srcTaxoDir, true);
  
  Directory targetIndexDir = newDirectory();
  Directory targetTaxoDir = newDirectory();
  buildIndexWithFacets(targetIndexDir, targetTaxoDir, false);
  
  IndexWriter destIndexWriter = new IndexWriter(targetIndexDir, newIndexWriterConfig(null));
  DirectoryTaxonomyWriter destTaxoWriter = new DirectoryTaxonomyWriter(targetTaxoDir);
  try {
    TaxonomyMergeUtils.merge(srcIndexDir, srcTaxoDir, new MemoryOrdinalMap(), destIndexWriter, destTaxoWriter, facetConfig);
  } finally {
    IOUtils.close(destIndexWriter, destTaxoWriter);
  }
  verifyResults(targetIndexDir, targetTaxoDir);
  
  IOUtils.close(targetIndexDir, targetTaxoDir, srcIndexDir, srcTaxoDir);
}
 
Example #3
Source File: TaxonomyMergeUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the given taxonomy and index directories and commits the changes to
 * the given writers.
 */
public static void merge(Directory srcIndexDir, Directory srcTaxoDir, OrdinalMap map, IndexWriter destIndexWriter,
    DirectoryTaxonomyWriter destTaxoWriter, FacetsConfig srcConfig) throws IOException {
  
  // merge the taxonomies
  destTaxoWriter.addTaxonomy(srcTaxoDir, map);
  int ordinalMap[] = map.getMap();
  DirectoryReader reader = DirectoryReader.open(srcIndexDir);
  try {
    List<LeafReaderContext> leaves = reader.leaves();
    int numReaders = leaves.size();
    CodecReader wrappedLeaves[] = new CodecReader[numReaders];
    for (int i = 0; i < numReaders; i++) {
      wrappedLeaves[i] = SlowCodecReaderWrapper.wrap(new OrdinalMappingLeafReader(leaves.get(i).reader(), ordinalMap, srcConfig));
    }
    destIndexWriter.addIndexes(wrappedLeaves);
    
    // commit changes to taxonomy and index respectively.
    destTaxoWriter.commit();
    destIndexWriter.commit();
  } finally {
    reader.close();
  }
}
 
Example #4
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 #5
Source File: LindenCoreImpl.java    From linden with Apache License 2.0 6 votes vote down vote up
public LindenCoreImpl(LindenConfig lindenConfig, String subIndexDirectory) throws IOException {
  this.config = lindenConfig;
  idFieldName = config.getSchema().getId();
  facetsConfig = config.createFacetsConfig();

  String directory = config.getIndexDirectory();
  if (subIndexDirectory != null) {
    directory = FilenameUtils.concat(config.getIndexDirectory(), subIndexDirectory);
  }

  indexWriter =
      new IndexWriter(createIndexDirectory(directory, config.getIndexType()), config.createIndexWriterConfig());
  trackingIndexWriter = new TrackingIndexWriter(indexWriter);

  taxoWriter =
      facetsConfig != null ? new DirectoryTaxonomyWriter(createTaxoIndexDirectory(directory, config.getIndexType()))
                           : null;
  commitStrategy = new CommitStrategy(indexWriter, taxoWriter);
  commitStrategy.start();

  lindenNRTSearcherManager = new LindenNRTSearcherManager(config,
                                                          trackingIndexWriter, taxoWriter);
  snippetGenerator = new LindenSnippetGenerator();
}
 
Example #6
Source File: ShardWriter.java    From linden with Apache License 2.0 6 votes vote down vote up
/**
 * Process an intermediate form by carrying out, on the Lucene instance of
 * the shard, the deletes and the inserts (a ram index) in the form.
 * @param form  the intermediate form containing deletes and a ram index
 * @throws IOException
 */
public void process(IntermediateForm form, FacetsConfig facetsConfig) throws IOException {
  if (facetsConfig != null) {
    DirectoryTaxonomyWriter.OrdinalMap map = new DirectoryTaxonomyWriter.MemoryOrdinalMap();
    // merge the taxonomies
    taxoWriter.addTaxonomy(form.getTaxoDirectory(), map);
    int ordinalMap[] = map.getMap();
    DirectoryReader reader = DirectoryReader.open(form.getDirectory());
    try {
      List<AtomicReaderContext> leaves = reader.leaves();
      int numReaders = leaves.size();
      AtomicReader wrappedLeaves[] = new AtomicReader[numReaders];
      for (int i = 0; i < numReaders; i++) {
        wrappedLeaves[i] = new OrdinalMappingAtomicReader(leaves.get(i).reader(), ordinalMap, facetsConfig);
      }
      writer.addIndexes(new MultiReader(wrappedLeaves));
    } finally {
      reader.close();
    }
  } else {
    writer.addIndexes(new Directory[] { form.getDirectory() });
  }
  numForms++;
}
 
Example #7
Source File: OLuceneFacetManager.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
protected void buildFacetIndexIfNeeded() throws IOException {

    if (metadata != null && metadata.containsField(FACET_FIELDS)) {
      ODatabaseDocumentInternal database = owner.getDatabase();
      Iterable<String> iterable = metadata.field(FACET_FIELDS);
      if (iterable != null) {
        Directory dir = getTaxDirectory(database);
        taxonomyWriter = new DirectoryTaxonomyWriter(dir, IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        for (String s : iterable) {
          facetField = s;
          // facetField = "facet_" + s;
          // facetDim = s;
          // config.setIndexFieldName(s, "facet_" + s);
          config.setHierarchical(s, true);
        }
      }

    }
  }
 
Example #8
Source File: TestOrdinalMappingLeafReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void buildIndexWithFacets(Directory indexDir, Directory taxoDir, boolean asc) throws IOException {
  IndexWriterConfig config = newIndexWriterConfig(null);
  RandomIndexWriter writer = new RandomIndexWriter(random(), indexDir, config);
  
  DirectoryTaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(taxoDir);
  for (int i = 1; i <= NUM_DOCS; i++) {
    Document doc = new Document();
    for (int j = i; j <= NUM_DOCS; j++) {
      int facetValue = asc ? j: NUM_DOCS - j;
      doc.add(new FacetField("tag", Integer.toString(facetValue)));
    }
    // add a facet under default dim config
    doc.add(new FacetField("id", Integer.toString(i)));
    
    // make sure OrdinalMappingLeafReader ignores non-facet BinaryDocValues fields
    doc.add(new BinaryDocValuesField("bdv", new BytesRef(Integer.toString(i))));
    doc.add(new BinaryDocValuesField("cbdv", new BytesRef(Integer.toString(i*2))));
    writer.addDocument(facetConfig.build(taxonomyWriter, doc));
  }
  taxonomyWriter.commit();
  taxonomyWriter.close();
  writer.commit();
  writer.close();
}
 
Example #9
Source File: LumongoIndex.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public DirectoryTaxonomyWriter getTaxoWriter(int segmentNumber) throws IOException {

		Directory d;

		if (indexConfig.getIndexSettings().getStoreIndexOnDisk()) {
			d = MMapDirectory.open(getPathForFacetsIndex(segmentNumber));
		}
		else {
			String indexSegmentDbName = getIndexSegmentDbName(segmentNumber);
			String indexSegmentCollectionName = getIndexSegmentCollectionName(segmentNumber) + "_facets";
			MongoDirectory mongoDirectory = new MongoDirectory(mongo, indexSegmentDbName, indexSegmentCollectionName, clusterConfig.isSharded(),
					clusterConfig.getIndexBlockSize());
			d = new DistributedDirectory(mongoDirectory);
		}

		NRTCachingDirectory nrtCachingDirectory = new NRTCachingDirectory(d, 2, 10);

		return new DirectoryTaxonomyWriter(nrtCachingDirectory);
	}
 
Example #10
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 #11
Source File: TestSearcherTaxonomyManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testExceptionDuringRefresh() throws Exception {

    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();

    IndexWriter w = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
    w.commit();
    tw.commit();

    SearcherTaxonomyManager mgr = new SearcherTaxonomyManager(indexDir, taxoDir, null);

    tw.addCategory(new FacetLabel("a", "b"));
    w.addDocument(new Document());

    tw.commit();
    w.commit();

    // intentionally corrupt the taxo index:
    SegmentInfos infos = SegmentInfos.readLatestCommit(taxoDir);
    taxoDir.deleteFile(infos.getSegmentsFileName());
    expectThrows(IndexNotFoundException.class, mgr::maybeRefreshBlocking);
    IOUtils.close(w, tw, mgr, indexDir, taxoDir);
  }
 
Example #12
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testMixedTypesInSameIndexField() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  
  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  FacetsConfig config = new FacetsConfig();
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  Document doc = new Document();
  doc.add(new IntAssociationFacetField(14, "a", "x"));
  doc.add(new FloatAssociationFacetField(55.0f, "b", "y"));
  expectThrows(IllegalArgumentException.class, () -> {
    writer.addDocument(config.build(taxoWriter, doc));
  });
  writer.close();
  IOUtils.close(taxoWriter, dir, taxoDir);
}
 
Example #13
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testNoHierarchy() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  
  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  FacetsConfig config = new FacetsConfig();
  config.setHierarchical("a", true);
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  Document doc = new Document();
  doc.add(new IntAssociationFacetField(14, "a", "x"));
  expectThrows(IllegalArgumentException.class, () -> {
    writer.addDocument(config.build(taxoWriter, doc));
  });

  writer.close();
  IOUtils.close(taxoWriter, dir, taxoDir);
}
 
Example #14
Source File: TestSearcherTaxonomyManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testReplaceTaxonomyNRT() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);

  Directory taxoDir2 = newDirectory();
  DirectoryTaxonomyWriter tw2 = new DirectoryTaxonomyWriter(taxoDir2);
  tw2.close();

  SearcherTaxonomyManager mgr = new SearcherTaxonomyManager(w, true, null, tw);
  w.addDocument(new Document());
  tw.replaceTaxonomy(taxoDir2);
  taxoDir2.close();

  expectThrows(IllegalStateException.class, () -> {
    mgr.maybeRefresh();
  });

  w.close();
  IOUtils.close(mgr, tw, taxoDir, dir);
}
 
Example #15
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testRequireDimCount() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  
  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  FacetsConfig config = new FacetsConfig();
  config.setRequireDimCount("a", true);
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  Document doc = new Document();
  doc.add(new IntAssociationFacetField(14, "a", "x"));
  expectThrows(IllegalArgumentException.class, () -> {
    writer.addDocument(config.build(taxoWriter, doc));
  });

  writer.close();
  IOUtils.close(taxoWriter, dir, taxoDir);
}
 
Example #16
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testReallyNoNormsForDrillDown() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
  iwc.setSimilarity(new PerFieldSimilarityWrapper() {
      final Similarity sim = new ClassicSimilarity();

      @Override
      public Similarity get(String name) {
        assertEquals("field", name);
        return sim;
      }
    });
  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
  FacetsConfig config = new FacetsConfig();

  Document doc = new Document();
  doc.add(newTextField("field", "text", Field.Store.NO));
  doc.add(new FacetField("a", "path"));
  writer.addDocument(config.build(taxoWriter, doc));
  writer.close();
  IOUtils.close(taxoWriter, dir, taxoDir);
}
 
Example #17
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testDetectHierarchicalField() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
  FacetsConfig config = new FacetsConfig();

  Document doc = new Document();
  doc.add(newTextField("field", "text", Field.Store.NO));
  doc.add(new FacetField("a", "path", "other"));
  expectThrows(IllegalArgumentException.class, () -> {
    config.build(taxoWriter, doc);
  });

  writer.close();
  IOUtils.close(taxoWriter, dir, taxoDir);
}
 
Example #18
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testDetectMultiValuedField() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();
  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
  FacetsConfig config = new FacetsConfig();

  Document doc = new Document();
  doc.add(newTextField("field", "text", Field.Store.NO));
  doc.add(new FacetField("a", "path"));
  doc.add(new FacetField("a", "path2"));
  expectThrows(IllegalArgumentException.class, () -> {
    config.build(taxoWriter, doc);
  });

  writer.close();
  IOUtils.close(taxoWriter, dir, taxoDir);
}
 
Example #19
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 #20
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 #21
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 #22
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 #23
Source File: IntermediateForm.java    From linden with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used by the index update combiner and process an
 * intermediate form into the current intermediate form. More specifically,
 * the input intermediate forms are a single-document ram index and/or a
 * single delete term.
 * @param form  the input intermediate form
 * @throws IOException
 */
public void process(IntermediateForm form, FacetsConfig facetsConfig) throws IOException {
  if (form.dir.ramBytesUsed() > 0 || form.taxoDir.ramBytesUsed() > 0) {
    if (writer == null) {
      createWriter();
    }

    if (facetsConfig != null) {
      DirectoryTaxonomyWriter.OrdinalMap map = new DirectoryTaxonomyWriter.MemoryOrdinalMap();
      // merge the taxonomies
      taxoWriter.addTaxonomy(form.taxoDir, map);
      int ordinalMap[] = map.getMap();
      DirectoryReader reader = DirectoryReader.open(form.dir);
      try {
        List<AtomicReaderContext> leaves = reader.leaves();
        int numReaders = leaves.size();
        AtomicReader wrappedLeaves[] = new AtomicReader[numReaders];
        for (int i = 0; i < numReaders; i++) {
          wrappedLeaves[i] = new OrdinalMappingAtomicReader(leaves.get(i).reader(), ordinalMap, facetsConfig);
        }
        writer.addIndexes(new MultiReader(wrappedLeaves));
      } finally {
        reader.close();
      }
    } else {
      writer.addIndexes(new Directory[] { form.dir });
    }
    numDocs++;
  }
}
 
Example #24
Source File: TestSearcherTaxonomyManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testReplaceTaxonomyDirectory() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  IndexWriter w = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
  w.commit();
  tw.commit();

  Directory taxoDir2 = newDirectory();
  DirectoryTaxonomyWriter tw2 = new DirectoryTaxonomyWriter(taxoDir2);
  tw2.addCategory(new FacetLabel("a", "b"));
  tw2.close();

  SearcherTaxonomyManager mgr = new SearcherTaxonomyManager(indexDir, taxoDir, null);
  SearcherAndTaxonomy pair = mgr.acquire();
  try {
    assertEquals(1, pair.taxonomyReader.getSize());
  } finally {
    mgr.release(pair);
  }
  
  w.addDocument(new Document());
  tw.replaceTaxonomy(taxoDir2);
  taxoDir2.close();
  w.commit();
  tw.commit();

  mgr.maybeRefresh();
  pair = mgr.acquire();
  try {
    assertEquals(3, pair.taxonomyReader.getSize());
  } finally {
    mgr.release(pair);
  }

  w.close();
  IOUtils.close(mgr, tw, taxoDir, indexDir);
}
 
Example #25
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 #26
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 #27
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 #28
Source File: LuceneIndexProviderImpl.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DirectoryTaxonomyWriter provideFacetsWriter() {
    if (facetsWriter == null) {
        try {
            return facetsWriter = new DirectoryTaxonomyWriter(facets, IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        } catch (IOException e) {
            LOGFTQ.error("Unable to acquire facets writer " + name + ", cause: " + e.getMessage());
            throw new RuntimeException(e);
        }
    }
    return facetsWriter;
}
 
Example #29
Source File: TestTaxonomyCombined.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**  Another set of tests for the writer, which don't use an array and
 *  try to distill the different cases, and therefore may be more helpful
 *  for debugging a problem than testWriter() which is hard to know why
 *  or where it failed. 
 */
@Test
public void testWriterSimpler() throws Exception {
  Directory indexDir = newDirectory();
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
  assertEquals(1, tw.getSize()); // the root only
  // Test that adding a new top-level category works
  assertEquals(1, tw.addCategory(new FacetLabel("a")));
  assertEquals(2, tw.getSize());
  // Test that adding the same category again is noticed, and the
  // same ordinal (and not a new one) is returned.
  assertEquals(1, tw.addCategory(new FacetLabel("a")));
  assertEquals(2, tw.getSize());
  // Test that adding another top-level category returns a new ordinal,
  // not the same one
  assertEquals(2, tw.addCategory(new FacetLabel("b")));
  assertEquals(3, tw.getSize());
  // Test that adding a category inside one of the above adds just one
  // new ordinal:
  assertEquals(3, tw.addCategory(new FacetLabel("a","c")));
  assertEquals(4, tw.getSize());
  // Test that adding the same second-level category doesn't do anything:
  assertEquals(3, tw.addCategory(new FacetLabel("a","c")));
  assertEquals(4, tw.getSize());
  // Test that adding a second-level category with two new components
  // indeed adds two categories
  assertEquals(5, tw.addCategory(new FacetLabel("d","e")));
  assertEquals(6, tw.getSize());
  // Verify that the parents were added above in the order we expected
  assertEquals(4, tw.addCategory(new FacetLabel("d")));
  // Similar, but inside a category that already exists:
  assertEquals(7, tw.addCategory(new FacetLabel("b", "d","e")));
  assertEquals(8, tw.getSize());
  // And now inside two levels of categories that already exist:
  assertEquals(8, tw.addCategory(new FacetLabel("b", "d","f")));
  assertEquals(9, tw.getSize());
  
  tw.close();
  indexDir.close();
}
 
Example #30
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);
}