Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I did, though it's a bit heavy on memory:</p> <p>What you need is to create in advance a bunch of <a href="http://java.sun.com/javase/6/docs/api/java/util/BitSet.html" rel="noreferrer"><code>BitSet</code></a>s, one for each category, containing the doc id of all the documents in a category. Now, on search time you use a <a href="http://hudson.zones.apache.org/hudson/job/Lucene-trunk/javadoc//org/apache/lucene/search/HitCollector.html" rel="noreferrer">HitCollector</a> and check the doc ids against the BitSets.</p> <p>Here's the code to create the bit sets:</p> <pre><code>public BitSet[] getBitSets(IndexSearcher indexSearcher, Category[] categories) { BitSet[] bitSets = new BitSet[categories.length]; for(int i=0; i&lt;categories.length; i++) { Query query = categories[i].getQuery(); final BitSet bitset = new BitSet() indexSearcher.search(query, new HitCollector() { public void collect(int doc, float score) { bitSet.set(doc); } }); bitSets[i] = bitSet; } return bitSets; } </code></pre> <p>This is just one way to do this. You could probably use <a href="http://hudson.zones.apache.org/hudson/job/Lucene-trunk/javadoc/org/apache/lucene/index/TermDocs.html" rel="noreferrer">TermDocs</a> instead of running a full search if your categories are simple enough, but this should only run once when you load the index anyway.</p> <p>Now, when it's time to count categories of search results you do this:</p> <pre><code>public int[] getCategroryCount(IndexSearcher indexSearcher, Query query, final BitSet[] bitSets) { final int[] count = new int[bitSets.length]; indexSearcher.search(query, new HitCollector() { public void collect(int doc, float score) { for(int i=0; i&lt;bitSets.length; i++) { if(bitSets[i].get(doc)) count[i]++; } } }); return count; } </code></pre> <p>What you end up with is an array containing the count of every category within the search results. If you also need the search results, you should add a TopDocCollector to your hit collector (yo dawg...). Or, you could just run the search again. 2 searches are better than 30.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload