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.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
 

5 Responses to Using the Filter Query in Solr (fq)

  1. Varun says:

    Thanks! Exactly what I was looking for. It is quite similar to doing a IN query in SQL.

  2. Sai says:

    Same as Varun – exactly what I was looking for

  3. Hasnain says:

    Thank you for the post, I am actually trying to insert another query in fq, what would be the syntax?
    I’ve tried using _query_ but it doesnt work the way I want it to. Here is what im trying

    fq=_query_:”{!lucene fl=id qf=name} video”

    any suggestions on this?
    thanks in advance

  4. pranav says:

    Is it possible to filter out results with the condition — do not include documents whose title contains [pills, spam, money] ? How would this be implemented?

  5. Venkat says:

    Hi Pranav,

    Did you get any solution for filtering results with condition.. I am looking for something similar. Please let me know. Thx.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>