Switch Query Parser – quick look

The number of different query parsers in Solr was always an amusement for me. Can someone here say how many of them are currently available and say what are those? Anyway, in this entry we won’t be talking about all the query parsers available in Solr, but we will take a quick look at one of them – the SwitchQueryParser introduced in Solr 4.2.

Logic behind the parser

The logic of the SwitchQueryParser is quite simple – allow processing a simple logic on the Solr side and add it as a sub-query. For example let’s say that we have an application that understand the following four values of the priceRange field:

  • cheap – when the price of the product (indexed in the price field) is lower than 10$,
  • average – when the price is between 10 and 30$,
  • expensive – when the product price is higher than  30$,
  • all – in case we want to return all the documents without looking at the price.

We want to have this logic stored in Solr somehow in order not to change our application or its configuration every time we want to change the above ranges. For this purpose we will use the SwitchQueryParser.

Our query

Let’s assume that our application will be able to send the following query:


We want the above query to return all the documents (q=*:*) narrowed to only those that are cheap, which in our case it will mean that they have price lower than 10$ (priceRange=cheap parameter).

Solr configuration

Of course we don’t want to send the price range in the query, because that wouldn’t make much sense for us. Because of that we decided to alter the solrconfig.xml file and add a new SearchHandler with the name of /price, which is configured as follows:

<requestHandler name="/price">
 <lst name="defaults">
  <str name="priceRange">all</str>
 <lst name="appends">
  <str name="fq">{!switch case.all='price:[* TO *]' case.cheap='price:[0 TO 10]' case.average='price:[10 TO 30]' case.expensive='price:[30 TO *]' v=$priceRange}</str>

As you can see the configuration of our SearchHandler consist of two elements. In the defaults section we defined the default value for the priceRange parameter, which is all. In addition to that, we’ve defined filter (fq) which is using the SwitchQueryParser (!switch). For each of the possible values the priceRange parameter can take (v=$priceRange) we defined a filter on the price field using the following expression – case.priceRangeValue=filter. So, when the value of the priceRange parameter in the query will be equal to cheap than Solr will use the filter defined by the case.cheap part of the filter definition, when the priceRange parameter value will be equal to expensive than Solr will use the filter defined by the case.expensive and so on.

What to remember about

There is one crucial thing to remember when using the described parser. In our case, if we would priceRange parameter value different than the four mentioned it will result in Solr error.


In my opinion the SwitchQueryParser is a nice addition, although it is rather a feature that will be used by the minority of the users. However taking into consideration that is can help implementing some very basic logic and because it is simple (and thus not hungry for resources) there will be users which will find this nice query parser useful 🙂

Leave a Reply

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