Bucket
Bucket

Reputation: 7521

Grouping Facets in Solr

In my Solr index I have two different types of items, A and B, in my index that have distinct fields foo and bar, respectively, but similar values that I need to group together for faceting.

A:
  foo: /* could be "abc", "def" or "ghi" */

B:
  bar: /* could be "abc", "ghi", or "jkl" */

It's easy enough to get the facet information for each of these fields separately:

http://myServer:<port>/<SolrPath>/q=<query>&facet.field=foo&facet.field=bar

Which gives me:

"facet_count": {
    "facet_fields": {
        "foo": ["abc", 10, "def", 20 "ghi", 30],
        "bar": ["abc", 3, "ghi", 8, "jkl", 1]
    }
}

Is there a way in Solr to specify that I want the fields A.foo and B.bar to be "lumped together" into the same facet? In other words, I need to make the facet information in the response looks like this:

"facet_count": {
    "facet_fields": {
        "foo": ["abc", 13, "def", 20 "ghi", 38, "jkl", 1]
    }
}

Upvotes: 0

Views: 359

Answers (1)

Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23084

No, my advice would be to index the values into a single field. Using copy field directives this would look like this (in schema.xml):

<copyField source="foo" dest="foobar" />
<copyField source="bar" dest="foobar" />

The would preserve the original foo and bar fields. To get your combined facets, simply facet on the new field:

?q=*:*
&facet=true
&facet.field=foobar

Edit: it might be possible with facet queries, but only if the list of unique values is small and limited, and you're willing to write a separate facet query for each value. Even then, the results will look different (count per query instead an array of field value, count).

Upvotes: 1

Related Questions