Search code examples
solrfacet

Solr facets on unique values of fields


I have a document structure in Solr that looks something like this (irrelevant fields excluded):

<field name="review_id" type="int" indexed="true" stored="true"/>
<field name="product_id" type="int" indexed="true" stored="true"/>
<field name="product_category" type="string" indexed="true" stored="true" multiValued="true"/>

product_id here is one-to-many wrt review_id

I can get a faceted count of reviews in each category by doing:

/select?q=*:*&rows=0&facet=true&facet.field=product_category

I want to be able to do faceting on the product_category, but get the number of distinct product_id:s instead of the number of review_id:s. Is this possible to do in Solr?


Solution

  • There is no one-to-many in a Solr index. It's not a relational database. The index is either about reviews or about products, and that depends on what you'll be searching for. To quote the Solr wiki about schema design:

    Solr provides one table. Storing a set database tables in an index generally requires denormalizing some of the tables. Attempts to avoid denormalizing usually fail.

    So the first step is fixing the schema design. Only after that (and always keeping the fact above in mind) can you design facets and other stuff.