Java Code Examples for org.apache.lucene.facet.taxonomy.TaxonomyReader

The following examples show how to use org.apache.lucene.facet.taxonomy.TaxonomyReader. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: SimpleFacetsExample.java    License: 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 2
Source Project: lucene-solr   Source File: SimpleFacetsExample.java    License: 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 3
Source Project: lucene-solr   Source File: SimpleFacetsExample.java    License: 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 4
Source Project: lucene-solr   Source File: AssociationsFacetsExample.java    License: 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 5
Source Project: lucene-solr   Source File: AssociationsFacetsExample.java    License: 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 Project: lucene-solr   Source File: TaxonomyIndexArrays.java    License: Apache License 2.0 6 votes vote down vote up
private void computeChildrenSiblings(int first) {
  // reset the youngest child of all ordinals. while this should be done only
  // for the leaves, we don't know up front which are the leaves, so we reset
  // all of them.
  for (int i = first; i < parents.length; i++) {
    children[i] = TaxonomyReader.INVALID_ORDINAL;
  }
  
  // the root category has no parent, and therefore no siblings
  if (first == 0) {
    first = 1;
    siblings[0] = TaxonomyReader.INVALID_ORDINAL;
  }
  
  for (int i = first; i < parents.length; i++) {
    // note that parents[i] is always < i, so the right-hand-side of
    // the following line is already set when we get here
    siblings[i] = children[parents[i]];
    children[parents[i]] = i;
  }
}
 
Example 7
Source Project: lucene-solr   Source File: DirectoryTaxonomyWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Add a new category into the index (and the cache), and return its new
 * ordinal.
 * <p>
 * Actually, we might also need to add some of the category's ancestors
 * before we can add the category itself (while keeping the invariant that a
 * parent is always added to the taxonomy before its child). We do this by
 * recursion.
 */
private int internalAddCategory(FacetLabel cp) throws IOException {
  // Find our parent's ordinal (recursively adding the parent category
  // to the taxonomy if it's not already there). Then add the parent
  // ordinal as payloads (rather than a stored field; payloads can be
  // more efficiently read into memory in bulk by LuceneTaxonomyReader)
  int parent;
  if (cp.length > 1) {
    FacetLabel parentPath = cp.subpath(cp.length - 1);
    parent = findCategory(parentPath);
    if (parent < 0) {
      parent = internalAddCategory(parentPath);
    }
  } else if (cp.length == 1) {
    parent = TaxonomyReader.ROOT_ORDINAL;
  } else {
    parent = TaxonomyReader.INVALID_ORDINAL;
  }
  int id = addCategoryDocument(cp, parent);

  return id;
}
 
Example 8
Source Project: lucene-solr   Source File: TestDrillSideways.java    License: Apache License 2.0 6 votes vote down vote up
protected DrillSideways getNewDrillSidewaysBuildFacetsResult(IndexSearcher searcher, FacetsConfig config,
        TaxonomyReader taxoReader) {
  return new DrillSideways(searcher, config, taxoReader) {
    @Override
    protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways,
            String[] drillSidewaysDims) throws IOException {
      Map<String, Facets> drillSidewaysFacets = new HashMap<>();
      Facets drillDownFacets = getTaxonomyFacetCounts(taxoReader, config, drillDowns);
      if (drillSideways != null) {
        for (int i = 0; i < drillSideways.length; i++) {
          drillSidewaysFacets.put(drillSidewaysDims[i], getTaxonomyFacetCounts(taxoReader, config, drillSideways[i]));
        }
      }

      if (drillSidewaysFacets.isEmpty()) {
        return drillDownFacets;
      } else {
        return new MultiFacets(drillSidewaysFacets, drillDownFacets);
      }

    }
  };
}
 
Example 9
Source Project: lucene-solr   Source File: TestDrillSideways.java    License: 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 10
Source Project: lucene-solr   Source File: TestParallelDrillSideways.java    License: Apache License 2.0 6 votes vote down vote up
protected DrillSideways getNewDrillSidewaysBuildFacetsResult(IndexSearcher searcher, FacetsConfig config,
        TaxonomyReader taxoReader) {
  return new DrillSideways(searcher, config, taxoReader, null, executor) {
    @Override
    protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways,
            String[] drillSidewaysDims) throws IOException {
      Map<String, Facets> drillSidewaysFacets = new HashMap<>();
      Facets drillDownFacets = getTaxonomyFacetCounts(taxoReader, config, drillDowns);
      if (drillSideways != null) {
        for (int i = 0; i < drillSideways.length; i++) {
          drillSidewaysFacets.put(drillSidewaysDims[i], getTaxonomyFacetCounts(taxoReader, config, drillSideways[i]));
        }
      }

      if (drillSidewaysFacets.isEmpty()) {
        return drillDownFacets;
      } else {
        return new MultiFacets(drillSidewaysFacets, drillDownFacets);
      }

    }
  };
}
 
Example 11
Source Project: lucene-solr   Source File: TestDirectoryTaxonomyWriter.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBackwardsCompatibility() throws Exception {
  // tests that if the taxonomy index doesn't have the INDEX_EPOCH
  // property (supports pre-3.6 indexes), all still works.
  Directory dir = newDirectory();
  
  // create an empty index first, so that DirTaxoWriter initializes indexEpoch to 1.
  new IndexWriter(dir, new IndexWriterConfig(null)).close();
  
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
  taxoWriter.close();
  
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir);
  assertEquals(1, Integer.parseInt(taxoReader.getCommitUserData().get(DirectoryTaxonomyWriter.INDEX_EPOCH)));
  assertNull(TaxonomyReader.openIfChanged(taxoReader));
  taxoReader.close();
  
  dir.close();
}
 
Example 12
Source Project: lucene-solr   Source File: TestDirectoryTaxonomyReader.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOpenIfChangedAndRefCount() throws Exception {
  Directory dir = new ByteBuffersDirectory(); // no need for random directories here

  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
  taxoWriter.addCategory(new FacetLabel("a"));
  taxoWriter.commit();

  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir);
  assertEquals("wrong refCount", 1, taxoReader.getRefCount());

  taxoReader.incRef();
  assertEquals("wrong refCount", 2, taxoReader.getRefCount());

  taxoWriter.addCategory(new FacetLabel("a", "b"));
  taxoWriter.commit();
  TaxonomyReader newtr = TaxonomyReader.openIfChanged(taxoReader);
  assertNotNull(newtr);
  taxoReader.close();
  taxoReader = newtr;
  assertEquals("wrong refCount", 1, taxoReader.getRefCount());

  taxoWriter.close();
  taxoReader.close();
  dir.close();
}
 
Example 13
Source Project: orientdb-lucene   Source File: LuceneNativeFacet.java    License: 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 14
Source Project: orientdb-lucene   Source File: LuceneNativeFacet.java    License: 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 15
Source Project: orientdb-lucene   Source File: LuceneNativeFacet.java    License: 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 16
Source Project: lumongo   Source File: LumongoSegment.java    License: 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 17
Source Project: lucene-solr   Source File: CloseTaxonomyReaderTask.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int doLogic() throws IOException {
  try (TaxonomyReader taxoReader = getRunData().getTaxonomyReader()) {
    getRunData().setTaxonomyReader(null);
    if (taxoReader.getRefCount() != 1) {
      System.out.println("WARNING: CloseTaxonomyReader: reference count is currently " + taxoReader.getRefCount());
    }
  }
  return 1;
}
 
Example 18
Source Project: lucene-solr   Source File: PerfRunData.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set the taxonomy reader. Takes ownership of that taxonomy reader, that is,
 * internally performs taxoReader.incRef() (If caller no longer needs that 
 * reader it should decRef()/close() it after calling this method, otherwise, 
 * the reader will remain open). 
 * @param taxoReader The taxonomy reader to set.
 */
public synchronized void setTaxonomyReader(TaxonomyReader taxoReader) throws IOException {
  if (taxoReader == this.taxonomyReader) {
    return;
  }
  if (taxonomyReader != null) {
    taxonomyReader.decRef();
  }
  
  if (taxoReader != null) {
    taxoReader.incRef();
  }
  this.taxonomyReader = taxoReader;
}
 
Example 19
Source Project: lucene-solr   Source File: PerfRunData.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return Returns the taxonomyReader.  NOTE: this returns a
 * reference.  You must call TaxonomyReader.decRef() when
 * you're done.
 */
public synchronized TaxonomyReader getTaxonomyReader() {
  if (taxonomyReader != null) {
    taxonomyReader.incRef();
  }
  return taxonomyReader;
}
 
Example 20
Source Project: lucene-solr   Source File: TestPerfTasksLogic.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test indexing with facets tasks.
 */
public void testIndexingWithFacets() throws Exception {
  // 1. alg definition (required in every "logic" test)
  String algLines[] = {
      "# ----- properties ",
      "content.source=org.apache.lucene.benchmark.byTask.feeds.LineDocSource",
      "docs.file=" + getReuters20LinesFile(),
      "content.source.log.step=100",
      "content.source.forever=false",
      "directory=ByteBuffersDirectory",
      "doc.stored=false",
      "merge.factor=3",
      "doc.tokenized=false",
      "debug.level=1",
      "# ----- alg ",
      "ResetSystemErase",
      "CreateIndex",
      "CreateTaxonomyIndex",
      "{ \"AddDocs\"  AddFacetedDoc > : * ",
      "CloseIndex",
      "CloseTaxonomyIndex",
      "OpenTaxonomyReader",
  };

  // 2. execute the algorithm  (required in every "logic" test)
  Benchmark benchmark = execBenchmark(algLines);
  PerfRunData runData = benchmark.getRunData();
  assertNull("taxo writer was not properly closed",runData.getTaxonomyWriter());
  TaxonomyReader taxoReader = runData.getTaxonomyReader();
  assertNotNull("taxo reader was not opened", taxoReader);
  assertTrue("nothing was added to the taxnomy (expecting root and at least one addtional category)",taxoReader.getSize()>1);
  taxoReader.close();
}
 
Example 21
/** 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 22
/** 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;
}
 
Example 23
Source Project: lucene-solr   Source File: SimpleFacetsExample.java    License: 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(), 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 24
@Override
public Boolean call() throws Exception {
  if (indexReader == null) {
    indexReader = DirectoryReader.open(indexDir);
    lastIndexGeneration = indexReader.getIndexCommit().getGeneration();
    taxoReader = new DirectoryTaxonomyReader(taxoDir);
  } else {
    // verify search index
    DirectoryReader newReader = DirectoryReader.openIfChanged(indexReader);
    assertNotNull("should not have reached here if no changes were made to the index", newReader);
    long newGeneration = newReader.getIndexCommit().getGeneration();
    assertTrue("expected newer generation; current=" + lastIndexGeneration + " new=" + newGeneration, newGeneration > lastIndexGeneration);
    indexReader.close();
    indexReader = newReader;
    lastIndexGeneration = newGeneration;
    TestUtil.checkIndex(indexDir);
    
    // verify taxonomy index
    DirectoryTaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(taxoReader);
    if (newTaxoReader != null) {
      taxoReader.close();
      taxoReader = newTaxoReader;
    }
    TestUtil.checkIndex(taxoDir);
    
    // verify faceted search
    int id = Integer.parseInt(indexReader.getIndexCommit().getUserData().get(VERSION_ID), 16);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    FacetsCollector fc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), fc);
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
    assertEquals(1, facets.getSpecificValue("A", Integer.toString(id, 16)).intValue());
    
    DrillDownQuery drillDown = new DrillDownQuery(config);
    drillDown.add("A", Integer.toString(id, 16));
    TopDocs docs = searcher.search(drillDown, 10);
    assertEquals(1, docs.totalHits.value);
  }
  return null;
}
 
Example 25
Source Project: lucene-solr   Source File: DrillSideways.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@code DrillSideways} instance, where some
 * dimensions were indexed with {@link
 * SortedSetDocValuesFacetField} and others were indexed
 * with {@link FacetField}.
 * <p>
 * Use this constructor to use the concurrent implementation and/or the CollectorManager
 */
public DrillSideways(IndexSearcher searcher, FacetsConfig config, TaxonomyReader taxoReader,
        SortedSetDocValuesReaderState state, ExecutorService executor) {
  this.searcher = searcher;
  this.config = config;
  this.taxoReader = taxoReader;
  this.state = state;
  this.executor = executor;
}
 
Example 26
Source Project: lucene-solr   Source File: TaxonomyIndexArrays.java    License: Apache License 2.0 5 votes vote down vote up
public TaxonomyIndexArrays(IndexReader reader) throws IOException {
  parents = new int[reader.maxDoc()];
  if (parents.length > 0) {
    initParents(reader, 0);
    // Starting Lucene 2.9, following the change LUCENE-1542, we can
    // no longer reliably read the parent "-1" (see comment in
    // LuceneTaxonomyWriter.SinglePositionTokenStream). We have no way
    // to fix this in indexing without breaking backward-compatibility
    // with existing indexes, so what we'll do instead is just
    // hard-code the parent of ordinal 0 to be -1, and assume (as is
    // indeed the case) that no other parent can be -1.
    parents[0] = TaxonomyReader.INVALID_ORDINAL;
  }
}
 
Example 27
Source Project: lucene-solr   Source File: TestDrillSideways.java    License: Apache License 2.0 5 votes vote down vote up
protected DrillSideways getNewDrillSidewaysScoreSubdocsAtOnce(IndexSearcher searcher, FacetsConfig config,
        TaxonomyReader taxoReader) {
  return new DrillSideways(searcher, config, taxoReader) {
    @Override
    protected boolean scoreSubDocsAtOnce() {
      return true;
    }
  };
}
 
Example 28
Source Project: lucene-solr   Source File: TestDrillSideways.java    License: Apache License 2.0 5 votes vote down vote up
public void testScorer() throws Exception {
  // LUCENE-6001 some scorers, eg ReqExlScorer, can hit NPE if cost is called after nextDoc
  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();

  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  Document doc = new Document();
  doc.add(newTextField("field", "foo bar", Field.Store.NO));
  doc.add(new FacetField("Author", "Bob"));
  doc.add(new FacetField("dim", "a"));
  writer.addDocument(config.build(taxoWriter, doc));

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

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

  DrillSideways ds = getNewDrillSideways(searcher, config, taxoReader);

  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  bq.add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
  bq.add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.MUST_NOT);
  DrillDownQuery ddq = new DrillDownQuery(config, bq.build());
  ddq.add("field", "foo");
  ddq.add("author", bq.build());
  ddq.add("dim", bq.build());
  DrillSidewaysResult r = ds.search(null, ddq, 10);
  assertEquals(0, r.hits.totalHits.value);

  writer.close();
  IOUtils.close(searcher.getIndexReader(), taxoReader, taxoWriter, dir, taxoDir);
}
 
Example 29
Source Project: lucene-solr   Source File: TestParallelDrillSideways.java    License: Apache License 2.0 5 votes vote down vote up
protected DrillSideways getNewDrillSidewaysScoreSubdocsAtOnce(IndexSearcher searcher, FacetsConfig config,
        TaxonomyReader taxoReader) {
  return new DrillSideways(searcher, config, taxoReader, null, executor) {
    @Override
    protected boolean scoreSubDocsAtOnce() {
      return true;
    }
  };
}
 
Example 30
Source Project: lucene-solr   Source File: TestMultipleIndexFields.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDefault() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  // create and open an index writer
  RandomIndexWriter iw = new RandomIndexWriter(random(), indexDir, newIndexWriterConfig(
      new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
  // create and open a taxonomy writer
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
  FacetsConfig config = getConfig();

  seedIndex(tw, iw, config);

  IndexReader ir = iw.getReader();
  tw.commit();

  // prepare index reader and taxonomy.
  TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);

  // prepare searcher to search against
  IndexSearcher searcher = newSearcher(ir);

  FacetsCollector sfc = performSearch(tr, ir, searcher);

  // Obtain facets results and hand-test them
  assertCorrectResults(getTaxonomyFacetCounts(tr, config, sfc));

  assertOrdinalsExist("$facets", ir);

  iw.close();
  IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}