0

I have a book database. Books have genre ids as integers, and also age ranges in integers age_min and age_max.

When I make a query, I'd like to know 1) a list of distinct genre ids that are in the result and 2) any of those results are between different 1 or more age ranges (0-4, 5-7, etc.).

I'd like a list of genre_ids as well as how many books are for ages 0-4, 5-7, 7-10, and so on.

I can use facet.field for the genre_ids and that works fine, but I cannot figure out to have that in addition to the different age ranges.

My query for JUST genre_id facet looks like this:

http://localhost:8983/solr/core0/select/?wt=json&q=all_genres%3A%22fiction+-+fantasy%22&rows=1&start=0&defType=edismax&facet=true&facet.field=genre_id

So, not only tell me the genre_ids that make up the results but also how many of those books are ages 0-4, 5-7, etc..

Something like:

"facet_fields":{
      "genre_id":{
        "38":998,
        "638":915,
      "ages":{
        "0-4":4,
        "5-7":10,

and so forth.

Is there a way to get all this information from one solr query?

TomServo
  • 11
  • 3
  • For the `ages` facets - have you seen the [Range Faceting](https://lucene.apache.org/solr/guide/6_6/faceting.html#Faceting-RangeFaceting) support? – MatsLindh Nov 19 '19 at 19:58
  • Yes, I've looked at that but it's not clear to me how to use it in combination with my genre_id facet. I think I do understand that in order to have multiple facet ranges, for multiple age ranges, I'd have to use keys as described [here](https://stackoverflow.com/questions/12580100/multiple-ranges-in-a-facet-in-solr) – TomServo Nov 19 '19 at 20:11
  • The "keys" you reference is just the field name. To configure the facet for genre_id, use `f.genre_id.`. To configure the facet for `ages`, use `f.ages.` instead of `facet.`. – MatsLindh Nov 19 '19 at 20:45
  • Here's my current query for the genre_id facet: http://localhost:8983/solr/core0/select/?wt=json&q=all_genres%3A%22fiction+-+fantasy%22&rows=1&start=0&defType=edismax&facet=true&facet.field=genre_id& If you have the time, how would I alter this to add the ages facet? I tried a few things and they didn't seem to work, I must have something wrong or am misunderstanding how to configure. – TomServo Nov 19 '19 at 21:52
  • Add something like `facet.field=ages&f.ages.facet.range.start=0&f.ages.facet.range.gap=4` or something similar to get started and to see if it works properly, then start expanding it if you need custom intervals (you could also use facet queries in that case). – MatsLindh Nov 20 '19 at 07:55
  • https://stackoverflow.com/questions/58983807/how-do-i-correctly-index-spanish-language-documents-using-solr – user2020099 Nov 22 '19 at 05:18

1 Answers1

0

I was overthinking my problem a bit, I just needed to use facet queries.

    facet.field=genre_id 

(to get the distinct genre_ids with counts)

    facet.query=age_min:[*+TO+4] 
    facet.query=age_min:[4+TO+7]+AND+age_max:[7+TO+99]
    facet.query=age_min:[7+TO+10]+AND+age_max:[10+TO+99]
    facet.query=age_min:[13+TO+99]+AND+age_max:[13+TO+99]

(to get counts for each age range)

The result:

  "facet_counts":{
    "facet_queries":{
      "age_min:[* TO 4]":6,
      "age_min:[4 TO 7] AND age_max:[7 TO 99]":16},
      ....
    "facet_fields":{
      "genre_id":[
        "818",740,
        "51",707,
        "1637",373,
       ....

I only wish I could give a 'name' to the facet_queries, but other than that this is my working solution.

TomServo
  • 11
  • 3