3
votes

Is there a way to get the count of different facets in solr?

This is an example facet:

  • Camera: 20
  • Computer: 80
  • Monitor: 40
  • Laptop: 120
  • Tablet: 30

What I need to get here is "5" as in 5 different electronic items. Of course I can count the facet but some of them have many items inside and fetching and counting them really slow it down.

5
Have you actually tried faceting? Try creating a sample index with documents like: {"id":1,"manf":"Acer","type":"monitor","desc":"19 inch MOnitor"} . Then do a query for 19 inch monitor in desc. Extra tip: use string fields for faceting. - Jesvin Jose
I think you don't understand what I'm asking. But it's OK. What Jayendra said is what I want. - m3rg

5 Answers

4
votes

You need to apply Solr Patch SOLR-2242 to get the Facet distinct count.

3
votes

In SOLR 5.1 will be a "JSON Facet API" with function "unique(state)". http://yonik.com/solr-facet-functions/

2
votes

Not an exact answer to this question, but if facet distinct count doesn't work for anyone, you can get that information using group.ngroups:

group = true
group.field = [field you are faceting on]
group.ngroups = true

The disadvantage of this approach is that if you want ungrouped results, you will have to run the query a second time.

0
votes

Of course the patch is recommended, but the cheesy way to do this for a simple facet, as you have here, is just to use the length of the facet divided by 2. For example, since you are faceting on type:

facetCount = facet_counts.facet_fields.type.length/2

0
votes

Use this GET request:

http://localhost:8983/solr/core_name/select?json.facet={x:"unique(electronic_items)"}&q=*:*&rows=0