SOLR - How to have facet counts restricted to rows returned in resultset

user489895 picture user489895 · Oct 28, 2010 · Viewed 7.9k times · Source

/select/?q=*:*&rows=100&facet=on&facet.field=category

I have around 100 000 documents indexed. But I return only 100 documents using rows=100. The facet counts returned for category, however return the counts for all documents indexed.

Can we somehow restrict the facets to the result set returned? i.e 100 rows only?

Answer

Hugo Zaragoza picture Hugo Zaragoza · Jun 29, 2011

I don't think it is possible in any direct manner, as was pointed out by Pascal.

I can see two ways to achieve this:

  1. Method I: do the counting yourself visiting the 100 results returned. This is very easy and fast if they are categorical fields, but harder if they are text fields that need to be tokenized, etc.

  2. Method II: do two passes:

    1. Do a normal query without facets (you only need to request doc ids at this point)
    2. Collect all the IDs of the documents returned
    3. Do a second query for all fields and facets, adding a filter to restrict result to those IDs collected in setp 2. Something like:
      select/?q=:&facet=on&facet.field=category&fq=id:(312 OR 28 OR 1231 ...)

The first is way more efficient and I would recommend for non-textual filds. The second is computationally expensive but has the advantage of working for all types od fields.