Using the Filter Query in Solr (Fq)
Filter queries are applied to a result set after it has generated the search results. This can be useful in cases where you want to filter by category or user.
In my use case, I’d like to filter a list of questions by a group of categories on Answerbag. This is used primarily for partner sites who’ve opted into our white label cobrand system and have selected a list of categories to restrict the site to. It affects search results, as the goal is to limit searches to these categories.
Technically, this can be done on 2 levels. First, it might be done in code, where we bring back a massive result set and filter out the questions that don’t satisfy our requirement. Ideally it can be done on the search engine side, which would reduce network overhead and eliminate a few really weird bugs that can crop up as a result.
It turns out this is pretty easy - but you must send it through as a single filter query, and not separate params for each filter.
To limit your filters to certain categories, you’d add something along these lines to your GET request, assuming you’ve stored categories by their numeric primary key:
fq=category_id:(24 79 84 693 694 1270 2309 2821 3284)
As usual, comments and thoughts are encouraged.If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.