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

The following examples show how to use org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts. 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: 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 4
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 5
/** 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 6
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 7
@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 8
Source Project: orientdb-lucene   Source File: LuceneNativeFacet.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(), 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 9
Source Project: orientdb-lucene   Source File: LuceneResultSet.java    License: Apache License 2.0 4 votes vote down vote up
private void fetchFacet() {
  if (queryContext.facet) {
    FacetsCollector facetsCollector = new FacetsCollector(true);

    try {

      String[] pathFacet = null;
      if (queryContext.isDrillDown()) {
        DrillDownQuery drillDownQuery = new DrillDownQuery(queryContext.getFacetConfig(), query);
        String[] path = queryContext.getDrillDownQuery().split(":");
        pathFacet = path[1].split("/");
        drillDownQuery.add(path[0], pathFacet);
        FacetsCollector.search(queryContext.searcher, drillDownQuery, PAGE_SIZE, facetsCollector);
      } else {
        FacetsCollector.search(queryContext.searcher, query, PAGE_SIZE, facetsCollector);
      }

      Facets facets = new FastTaxonomyFacetCounts(queryContext.reader, queryContext.getFacetConfig(), facetsCollector);

      FacetResult facetResult = null;
      if (pathFacet != null) {
        facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField(), pathFacet);
      } else {
        facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField());
      }

      if (facetResult != null) {
        List<ODocument> documents = new ArrayList<ODocument>();
        // for (FacetResult facetResult : res) {

        ODocument doc = new ODocument();

        doc.field("childCount", facetResult.childCount);
        doc.field("value", facetResult.value);
        doc.field("dim", facetResult.dim);
        List<ODocument> labelsAndValue = new ArrayList<ODocument>();
        for (LabelAndValue labelValue : facetResult.labelValues) {
          ODocument doc1 = new ODocument();
          doc1.field("label", labelValue.label);
          doc1.field("value", labelValue.value);
          labelsAndValue.add(doc1);

        }
        doc.field("labelsValue", labelsAndValue);
        documents.add(doc);
        queryContext.context.setVariable("$facet", documents);
      }
      // }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 
Example 10
Source Project: lumongo   Source File: LumongoSegment.java    License: Apache License 2.0 4 votes vote down vote up
private void searchWithFacets(FacetRequest facetRequest, Query q, IndexSearcher indexSearcher, TopDocsCollector<?> collector,
		SegmentResponse.Builder segmentReponseBuilder) throws Exception {
	FacetsCollector facetsCollector = new FacetsCollector();
	indexSearcher.search(q, MultiCollector.wrap(collector, facetsCollector));

	Facets facets = new FastTaxonomyFacetCounts(taxoReader, facetsConfig, facetsCollector);

	for (CountRequest countRequest : facetRequest.getCountRequestList()) {

		String label = countRequest.getFacetField().getLabel();

		if (!indexConfig.existingFacet(label)) {
			throw new Exception(label + " is not defined as a facetable field");
		}

		if (countRequest.hasSegmentFacets()) {
			if (indexConfig.getNumberOfSegments() == 1) {
				log.info("Segment facets is ignored with segments of 1 for facet <" + label + "> on index <" + indexName + ">");
			}
			if (countRequest.getSegmentFacets() < countRequest.getMaxFacets()) {
				throw new IllegalArgumentException("Segment facets must be greater than or equal to max facets");
			}
		}

		int numOfFacets;
		if (indexConfig.getNumberOfSegments() > 1) {
			if (countRequest.getSegmentFacets() != 0) {
				numOfFacets = countRequest.getSegmentFacets();
			}
			else {
				numOfFacets = countRequest.getMaxFacets() * 8;
			}

		}
		else {
			numOfFacets = countRequest.getMaxFacets();
		}

		FacetResult facetResult = null;

		try {

			if (indexConfig.getNumberOfSegments() > 1) {
				if (countRequest.hasSegmentFacets() && countRequest.getSegmentFacets() == 0) {
					//TODO: this not ideal
					numOfFacets = taxoReader.getSize();
				}
			}

			facetResult = facets.getTopChildren(numOfFacets, label);
		}
		catch (UncheckedExecutionException e) {
			Throwable cause = e.getCause();
			if (cause.getMessage().contains(" was not indexed with SortedSetDocValues")) {
				//this is when no data has been indexing into a facet or facet does not exist
			}
			else {
				throw e;
			}
		}
		FacetGroup.Builder fg = FacetGroup.newBuilder();
		fg.setCountRequest(countRequest);

		if (facetResult != null) {

			for (LabelAndValue subResult : facetResult.labelValues) {
				FacetCount.Builder facetCountBuilder = FacetCount.newBuilder();
				facetCountBuilder.setCount(subResult.value.longValue());
				facetCountBuilder.setFacet(subResult.label);
				fg.addFacetCount(facetCountBuilder);
			}
		}
		segmentReponseBuilder.addFacetGroup(fg);
	}
}