You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@solr.apache.org by Vishal Patel <vi...@outlook.com> on 2025/12/15 05:38:11 UTC

Solr 8.9 – How to run JSON Facet on grouped results (group-by + latest-doc selection in multi-shard collection)?

Hello Solr Community,
We are trying to combine result grouping with the JSON Facet API in Solr 8.9.0 on a distributed (multi-shard) collection, and we are unsure whether Solr supports this use case.

Environment

  *   Solr version: 8.9.0 , Collection: forms , Shards: 2 shards, 1 replica per shard (total 4 Solr nodes) , ZooKeeper ensemble: 7 nodes , Schema fields: id, form_id, project_id, msg_creation_date, string_form_code, status_id


Use Case
We want to:

  1.  Group documents by form_id
  2.  Select only the latest document per group, using msg_creation_date desc
  3.  Run JSON Facet aggregations on the grouped result set, not on the full document set

Here is a simplified query showing the intention:
q=project_id:(100 101)&fl=id&start=0&rows=1&sort=updated desc,id desc&group=true&group.field=form_id&group.sort=msg_creation_date desc&group.main=true&json.facet={string_form_code:{type:terms,field:string_form_code,limit:-1,facet:{project_id:{type:terms,field:project_id,limit:-1,facet:{status_id:{type:terms,field:status_id,limit:-1}}}}}}&shards.tolerant=true

Problem

  *   Grouping and JSON Facet execute independently.
JSON Facet always evaluates on the full matching document set, not on the documents returned after grouping.
  *   We attempted the collapse  approach, but not working in multi-shards.

Our requirement is:
Facet counts must be based only on the grouped results (one latest document per form_id).

Questions

  1.  What is the recommended approach in Solr for:
“group by field → pick latest doc → facet on this reduced set”, especially in multi-shard collections?
  2.  Is it necessary to run two separate queries (one for grouping and one for faceting)?
  3.  Is this behaviour a known limitation of Solr 8.x, and has it changed or improved in more recent Solr versions?

Any guidance, recommended patterns, or best practices would be greatly appreciated.

Thank you!
Vishal Patel