I have a database of books, each which has a list of subjects attached. I would like to build a query so that when a user searches for a subject, Solr returns a list of subjects that follow the query alphabetically. For example, if the user were to make the query "whale", then I would like Solr to return the list of subjects "whale hunting", "whales", "wheat", etc. I'm only interested in the aggregation of subjects, not in the items that match the query.
However, there is an issue in that a book can have multiple subjects. If a document has subjects "arctic" and "whale", then the query "whale" matches "whale", but "arctic" shows up in my facets as well, which I don't want. I'd like to avoid having to parse through all the results, since that is a potentially expensive operation.
What's the best solution for this? Is there any way to tell Solr to only return facets where the facet is [whale* TO *]? Or can I build my query so that it will only consider the subject that matches to be a part of the facets?
I've looked at and have tinkered a little with the JSON request API, which seems like it could be the solution, but I'm having trouble getting the various filters etc. to work.