<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SwitchQueryParser &#8211; Solr.pl</title>
	<atom:link href="https://solr.pl/en/tag/switchqueryparser/feed/" rel="self" type="application/rss+xml" />
	<link>https://solr.pl/en/</link>
	<description>All things to be found - Blog related to Apache Solr &#38; Lucene projects - https://solr.apache.org</description>
	<lastBuildDate>Thu, 12 Nov 2020 12:59:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Switch Query Parser &#8211; quick look</title>
		<link>https://solr.pl/en/2013/06/03/switch-query-parser-quick-look/</link>
					<comments>https://solr.pl/en/2013/06/03/switch-query-parser-quick-look/#respond</comments>
		
		<dc:creator><![CDATA[Rafał Kuć]]></dc:creator>
		<pubDate>Mon, 03 Jun 2013 11:59:28 +0000</pubDate>
				<category><![CDATA[Solr]]></category>
		<category><![CDATA[4.2]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[query parser]]></category>
		<category><![CDATA[solr]]></category>
		<category><![CDATA[switch]]></category>
		<category><![CDATA[SwitchQueryParser]]></category>
		<guid isPermaLink="false">http://sematext.solr.pl/?p=556</guid>

					<description><![CDATA[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&#8217;t be talking about all]]></description>
										<content:encoded><![CDATA[<p>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&#8217;t be talking about all the query parsers available in Solr, but we will take a quick look at one of them &#8211; the <em>SwitchQueryParser</em> introduced in Solr 4.2.</p>
<p><span id="more-556"></span></p>
<h3>Logic behind the parser</h3>
<p>The logic of the&nbsp;<em>SwitchQueryParser</em> is quite simple &#8211; allow processing a simple logic on the Solr side and add it as a sub-query. For example let&#8217;s say that we have an application that understand the following four values of the&nbsp;<em>priceRange</em> field:</p>
<ul>
<li><em>cheap</em> &#8211; when the price of the product (indexed in the&nbsp;<em>price</em> field) is lower than 10$,</li>
<li><em>average</em> &#8211; when the price is between 10 and 30$,</li>
<li><em>expensive</em> &#8211; when the product price is higher than&nbsp; 30$,</li>
<li><em>all</em> &#8211; in case we want to return all the documents without looking at the price.</li>
</ul>
<p>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 <em>SwitchQueryParser</em>.</p>
<h3>Our query</h3>
<p>Let&#8217;s assume that our application will be able to send the following query:
</p>
<pre class="brush:bash">http://localhost:8983/solr/collection1/price?q=*:*&amp;priceRange=cheap</pre>
<p>We want the above query to return all the documents (<em>q=*:*</em>) narrowed to only those that are cheap, which in our case it will mean that they have price lower than 10$ (<em>priceRange=cheap </em>parameter).</p>
<h3>Solr configuration</h3>
<p>Of course we don&#8217;t want to send the <em>price</em> range in the query, because that wouldn&#8217;t make much sense for us. Because of that we decided to alter the <em>solrconfig.xml</em> file and add a new SearchHandler with the name of /<em>price</em>, which is configured as follows:
</p>
<pre class="brush:xml">&lt;requestHandler name="/price"&gt;
 &lt;lst name="defaults"&gt;
  &lt;str name="priceRange"&gt;all&lt;/str&gt;
 &lt;/lst&gt;
 &lt;lst name="appends"&gt;
  &lt;str name="fq"&gt;{!switch case.all='price:[* TO *]' case.cheap='price:[0 TO 10]' case.average='price:[10 TO 30]' case.expensive='price:[30 TO *]' v=$priceRange}&lt;/str&gt;
 &lt;/lst&gt;
&lt;/requestHandler&gt;</pre>
<p>As you can see the configuration of our SearchHandler consist of two elements. In the <em>defaults</em> section we defined the default value for the <em>priceRange</em> parameter, which is <em>all</em>. In addition to that, we&#8217;ve defined filter (<em>fq</em>) which is using the <em>SwitchQueryParser</em> (<em>!switch</em>). For each of the possible values the <em>priceRange</em> parameter can take (<em>v=$priceRange</em>) we defined a filter on the <em>price</em> field using the following expression &#8211;&nbsp;<em>case.priceRangeValue</em><em>=filter</em>. So, when the value of the <em>priceRange</em> parameter in the query will be equal to <strong><em>cheap</em></strong> than Solr will use the filter defined by the <em>case.<strong>cheap </strong></em>part of the filter definition, when the <em>priceRange</em> parameter value will be equal to <strong><em>expensive </em></strong>than Solr will use the filter defined by the <em>case.<strong>expensive </strong></em>and so on.</p>
<h3>What to remember about</h3>
<p>There is one crucial thing to remember when using the described parser. In our case, if we would <em>priceRange</em> parameter value different than the four mentioned it will result in Solr error.</p>
<h3>Summary</h3>
<p>In my opinion the <em>SwitchQueryParser</em> 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 <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://solr.pl/en/2013/06/03/switch-query-parser-quick-look/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
