Java Code Examples for org.apache.lucene.facet.FacetResult

The following examples show how to use org.apache.lucene.facet.FacetResult. 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: RangeFacetsExample.java    License: Apache License 2.0 6 votes vote down vote up
/** User runs a query and counts facets. */
public FacetResult search() throws IOException {

  // Aggregates the facet counts
  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 facets = new LongRangeFacetCounts("timestamp", fc,
                                           PAST_HOUR,
                                           PAST_SIX_HOURS,
                                           PAST_DAY);
  return facets.getTopChildren(10, "timestamp");
}
 
Example 2
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 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 '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 4
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 5
Source Project: lucene-solr   Source File: SimpleFacetsExample.java    License: Apache License 2.0 6 votes vote down vote up
/** Runs the search and drill-down examples and prints the results. */
public static void main(String[] args) throws Exception {
  System.out.println("Facet counting example:");
  System.out.println("-----------------------");
  SimpleFacetsExample example = new SimpleFacetsExample();
  List<FacetResult> results1 = example.runFacetOnly();
  System.out.println("Author: " + results1.get(0));
  System.out.println("Publish Date: " + results1.get(1));
  
  System.out.println("Facet counting example (combined facets and search):");
  System.out.println("-----------------------");
  List<FacetResult> results = example.runSearch();
  System.out.println("Author: " + results.get(0));
  System.out.println("Publish Date: " + results.get(1));
  
  System.out.println("Facet drill-down example (Publish Date/2010):");
  System.out.println("---------------------------------------------");
  System.out.println("Author: " + example.runDrillDown());

  System.out.println("Facet drill-sideways example (Publish Date/2010):");
  System.out.println("---------------------------------------------");
  for(FacetResult result : example.runDrillSideways()) {
    System.out.println(result);
  }
}
 
Example 6
Source Project: lucene-solr   Source File: SimpleSortedSetFacetsExample.java    License: Apache License 2.0 6 votes vote down vote up
/** User runs a query and counts facets. */
private List<FacetResult> search() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);

  // Aggregatses the facet counts
  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
  Facets facets = new SortedSetDocValuesFacetCounts(state, fc);

  List<FacetResult> results = new ArrayList<>();
  results.add(facets.getTopChildren(10, "Author"));
  results.add(facets.getTopChildren(10, "Publish Year"));
  indexReader.close();
  
  return results;
}
 
Example 7
Source Project: lucene-solr   Source File: SimpleSortedSetFacetsExample.java    License: Apache License 2.0 6 votes vote down vote up
/** User drills down on 'Publish Year/2010'. */
private FacetResult drillDown() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);

  // Now user drills down on Publish Year/2010:
  DrillDownQuery q = new DrillDownQuery(config);
  q.add("Publish Year", "2010");
  FacetsCollector fc = new FacetsCollector();
  FacetsCollector.search(searcher, q, 10, fc);

  // Retrieve results
  Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
  FacetResult result = facets.getTopChildren(10, "Author");
  indexReader.close();
  
  return result;
}
 
Example 8
Source Project: lucene-solr   Source File: DistanceFacetsExample.java    License: Apache License 2.0 6 votes vote down vote up
/** User runs a query and counts facets. */
public FacetResult search() throws IOException {

  FacetsCollector fc = new FacetsCollector();

  searcher.search(new MatchAllDocsQuery(), fc);

  Facets facets = new DoubleRangeFacetCounts("field", getDistanceValueSource(), fc,
                                             getBoundingBoxQuery(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, 10.0),
                                             ONE_KM,
                                             TWO_KM,
                                             FIVE_KM,
                                             TEN_KM);

  return facets.getTopChildren(10, "field");
}
 
Example 9
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 10
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 11
Source Project: lucene-solr   Source File: TaxonomyFacets.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<FacetResult> getAllDims(int topN) throws IOException {
  int[] children = getChildren();
  int[] siblings = getSiblings();
  int ord = children[TaxonomyReader.ROOT_ORDINAL];
  List<FacetResult> results = new ArrayList<>();
  while (ord != TaxonomyReader.INVALID_ORDINAL) {
    String dim = taxoReader.getPath(ord).components[0];
    FacetsConfig.DimConfig dimConfig = config.getDimConfig(dim);
    if (dimConfig.indexFieldName.equals(indexFieldName)) {
      FacetResult result = getTopChildren(topN, dim);
      if (result != null) {
        results.add(result);
      }
    }
    ord = siblings[ord];
  }

  // Sort by highest value, tie break by dim:
  Collections.sort(results, BY_VALUE_THEN_DIM);
  return results;
}
 
Example 12
Source Project: lucene-solr   Source File: TestTaxonomyFacetCounts.java    License: 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 13
Source Project: lumongo   Source File: FacetStorageTest.java    License: Apache License 2.0 6 votes vote down vote up
/** User drills down on 'Publish Year/2010'. */
private FacetResult drillDown() throws IOException {
	DirectoryReader indexReader = DirectoryReader.open(directory);
	IndexSearcher searcher = new IndexSearcher(indexReader);
	SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);
	
	// Now user drills down on Publish Year/2010:
	DrillDownQuery q = new DrillDownQuery(config);
	q.add("Publish Year", "2010");
	FacetsCollector fc = new FacetsCollector();
	FacetsCollector.search(searcher, q, 10, fc);
	
	// Retrieve results
	Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
	FacetResult result = facets.getTopChildren(10, "Author");
	indexReader.close();
	
	return result;
}
 
Example 14
Source Project: lumongo   Source File: FacetStorageTest.java    License: Apache License 2.0 6 votes vote down vote up
/** Runs the search and drill-down examples and prints the results. */
public static void main(String[] args) throws Exception {
	init();

	System.out.println("Facet counting example:");
	System.out.println("-----------------------");
	FacetStorageTest example = new FacetStorageTest();
	List<FacetResult> results = example.runSearch();
	System.out.println("Author: " + results.get(0));
	System.out.println("Publish Year: " + results.get(1));
	
	System.out.println("\n");
	System.out.println("Facet drill-down example (Publish Year/2010):");
	System.out.println("---------------------------------------------");
	System.out.println("Author: " + example.runDrillDown());
}
 
Example 15
/** 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 16
/** Runs the search and drill-down examples and prints the results. */
public static void main(String[] args) throws Exception {
  System.out.println("Facet counting example:");
  System.out.println("-----------------------");
  FacetResult result = new ExpressionAggregationFacetsExample().runSearch();
  System.out.println(result);
}
 
Example 17
/** 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 18
/** Runs the search example and prints the results. */
public static void main(String[] args) throws Exception {
  System.out.println("Facet counting over multiple category lists example:");
  System.out.println("-----------------------");
  List<FacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
  System.out.println("Author: " + results.get(0));
  System.out.println("Publish Date: " + results.get(1));
}
 
Example 19
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 20
Source Project: lucene-solr   Source File: SimpleSortedSetFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
/** Runs the search and drill-down examples and prints the results. */
public static void main(String[] args) throws Exception {
  System.out.println("Facet counting example:");
  System.out.println("-----------------------");
  SimpleSortedSetFacetsExample example = new SimpleSortedSetFacetsExample();
  List<FacetResult> results = example.runSearch();
  System.out.println("Author: " + results.get(0));
  System.out.println("Publish Year: " + results.get(0));

  System.out.println("\n");
  System.out.println("Facet drill-down example (Publish Year/2010):");
  System.out.println("---------------------------------------------");
  System.out.println("Author: " + example.runDrillDown());
}
 
Example 21
Source Project: lucene-solr   Source File: AssociationsFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
/** Runs the sum int/float associations examples and prints the results. */
public static void main(String[] args) throws Exception {
  System.out.println("Sum associations example:");
  System.out.println("-------------------------");
  List<FacetResult> results = new AssociationsFacetsExample().runSumAssociations();
  System.out.println("tags: " + results.get(0));
  System.out.println("genre: " + results.get(1));
}
 
Example 22
@Test
public void testSimple() throws Exception {
  List<FacetResult> results = new SimpleSortedSetFacetsExample().runSearch();
  assertEquals(2, results.size());
  assertEquals("dim=Author path=[] value=5 childCount=4\n  Lisa (2)\n  Bob (1)\n  Frank (1)\n  Susan (1)\n", results.get(0).toString());
  assertEquals("dim=Publish Year path=[] value=5 childCount=3\n  2010 (2)\n  2012 (2)\n  1999 (1)\n", results.get(1).toString());
}
 
Example 23
Source Project: lucene-solr   Source File: TestSimpleFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFacetOnly() throws Exception {
  List<FacetResult> results = new SimpleFacetsExample().runFacetOnly();
  assertEquals(2, results.size());
  assertEquals("dim=Author path=[] value=5 childCount=4\n  Lisa (2)\n  Bob (1)\n  Susan (1)\n  Frank (1)\n", results.get(0).toString());
  assertEquals("dim=Publish Date path=[] value=5 childCount=3\n  2010 (2)\n  2012 (2)\n  1999 (1)\n", results.get(1).toString());
}
 
Example 24
Source Project: lucene-solr   Source File: TestSimpleFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() throws Exception {
  List<FacetResult> results = new SimpleFacetsExample().runSearch();
  assertEquals(2, results.size());
  assertEquals("dim=Author path=[] value=5 childCount=4\n  Lisa (2)\n  Bob (1)\n  Susan (1)\n  Frank (1)\n", results.get(0).toString());
  assertEquals("dim=Publish Date path=[] value=5 childCount=3\n  2010 (2)\n  2012 (2)\n  1999 (1)\n", results.get(1).toString());
}
 
Example 25
Source Project: lucene-solr   Source File: TestDistanceFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
public void testSimple() throws Exception {
  DistanceFacetsExample example = new DistanceFacetsExample();
  example.index();
  FacetResult result = example.search();
  assertEquals("dim=field path=[] value=3 childCount=4\n  < 1 km (1)\n  < 2 km (2)\n  < 5 km (2)\n  < 10 km (3)\n", result.toString());
  example.close();
}
 
Example 26
Source Project: lucene-solr   Source File: TestAssociationsFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testExamples() throws Exception {
  List<FacetResult> res = new AssociationsFacetsExample().runSumAssociations();
  assertEquals("Wrong number of results", 2, res.size());
  assertEquals("dim=tags path=[] value=-1 childCount=2\n  lucene (4)\n  solr (2)\n", res.get(0).toString());
  assertEquals("dim=genre path=[] value=-1.0 childCount=2\n  computing (1.62)\n  software (0.34)\n", res.get(1).toString());
}
 
Example 27
@Test
public void testExample() throws Exception {
  List<FacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
  assertEquals(2, results.size());
  assertEquals("dim=Author path=[] value=5 childCount=4\n  Lisa (2)\n  Bob (1)\n  Susan (1)\n  Frank (1)\n", results.get(0).toString());
  assertEquals("dim=Publish Date path=[] value=5 childCount=3\n  2010 (2)\n  2012 (2)\n  1999 (1)\n", results.get(1).toString());
}
 
Example 28
Source Project: lucene-solr   Source File: TestRangeFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() throws Exception {
  RangeFacetsExample example = new RangeFacetsExample();
  example.index();
  FacetResult result = example.search();
  assertEquals("dim=timestamp path=[] value=87 childCount=3\n  Past hour (4)\n  Past six hours (22)\n  Past day (87)\n", result.toString());
  example.close();
}
 
Example 29
Source Project: lucene-solr   Source File: SortedSetDocValuesFacetCounts.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException {
  if (topN <= 0) {
    throw new IllegalArgumentException("topN must be > 0 (got: " + topN + ")");
  }
  if (path.length > 0) {
    throw new IllegalArgumentException("path should be 0 length");
  }
  OrdRange ordRange = state.getOrdRange(dim);
  if (ordRange == null) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" was not indexed");
  }
  return getDim(dim, ordRange, topN);
}
 
Example 30
Source Project: lucene-solr   Source File: SortedSetDocValuesFacetCounts.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<FacetResult> getAllDims(int topN) throws IOException {

  List<FacetResult> results = new ArrayList<>();
  for(Map.Entry<String,OrdRange> ent : state.getPrefixToOrdRange().entrySet()) {
    FacetResult fr = getDim(ent.getKey(), ent.getValue(), topN);
    if (fr != null) {
      results.add(fr);
    }
  }

  // Sort by highest count:
  Collections.sort(results,
                   new Comparator<FacetResult>() {
                     @Override
                     public int compare(FacetResult a, FacetResult b) {
                       if (a.value.intValue() > b.value.intValue()) {
                         return -1;
                       } else if (b.value.intValue() > a.value.intValue()) {
                         return 1;
                       } else {
                         return a.dim.compareTo(b.dim);
                       }
                     }
                   });

  return results;
}