{"id":225,"date":"2011-03-28T09:20:29","date_gmt":"2011-03-28T07:20:29","guid":{"rendered":"http:\/\/sematext.solr.pl\/?p=225"},"modified":"2020-11-11T09:21:20","modified_gmt":"2020-11-11T08:21:20","slug":"solr-1-4-local-params","status":"publish","type":"post","link":"https:\/\/solr.pl\/en\/2011\/03\/28\/solr-1-4-local-params\/","title":{"rendered":"Solr 1.4: Local Params"},"content":{"rendered":"<p>Several months ago, during one of the projects I have tried to construct a query with optimal faceting.  The problem was that we need filters (<em>fq<\/em>) in the query but in the same time we need a faceting that was not filtered. To some point it was not possible in Solr &#8211; you had to make two queries.  But now, you can do it with one query. Let&#8217;s meet <em>LocalParams<\/em>.<\/p>\n\n\n<!--more-->\n\n\n<h3>The begining<\/h3>\n<p><em>LocalParams <\/em>lets you tag part of the query, for example:\n<\/p>\n<pre class=\"brush:xml\">q=*:*&amp;fq={!tag=city}city:warszawa<\/pre>\n<p>After  sending the above query to Solr it will remember (in the context of the  same query) that tag named <em>city <\/em>covers the filter with a value of <em>city: warszawa<\/em>. Now  suppose that we want to send a query to get all documents that have the <em>warszawa<\/em> value in the <em>city<\/em> field (lets suppose that the <em>city <\/em>field is  multi valued). In addition, we want to Solr to show us the number of documents in all cities. Nothing could be simpler with the use of <em>LocalParams <\/em>&#8211; just use the parameter <em>{!ex}<\/em> in the faceting. This could possibly be done by sending the following query to Solr:\n<\/p>\n<pre class=\"brush:xml\">q=*:*&amp;fq={!tag=miasto}city:warszawa&amp;facet=true&amp;facet.field={!ex=miasto}city<\/pre>\n<p>The  above query will return documents that have the <em>warszawa <\/em>value in the <em> city <\/em>field and faceting calculated on the <em>city <\/em>field which is not  narowed by the filter query, because we excluded that filter.<\/p>\n<h3>Query parser type<\/h3>\n<p>Of course, the use of <em>LocalParams <\/em>is not limited to faceting. One of the interesting examples is a type handler definition. If we use <em>standard<\/em> query parser, we can change its type for a particular query with this structure:\n<\/p>\n<pre class=\"brush:xml\">q={!type=dismax qf=city}warszawa<\/pre>\n<p>The  above query will use the <em>dismax <\/em>query parser and pass the <em>qf=city<\/em> parameter to it along with the <em>warszawa<\/em> value in the <em>q<\/em> parameter.<\/p>\n<h3>Logical operator<\/h3>\n<p>Changing the default logical operator using <em>LocalParams <\/em>is also very simple:\n<\/p>\n<pre class=\"brush:xml\">q={!q.op=AND}warszawa praga<\/pre>\n<p>The above query uses the AND logical operator for terms contained in the parameter <em>q<\/em>, which are <em>warszawa<\/em> and <em>praga<\/em>.<\/p>\n<h3>Parameter dereferencing<\/h3>\n<p>Additionally, by using <em>LocalParams <\/em>we can achieve an interesting feature &#8211; the parameters dereferencing. For example, if we want to use your own parameter names in queries, we can do it this way:\n<\/p>\n<pre class=\"brush:xml\">q={!type=dismax qf=city v=$pmiasto}&amp;pmiasto=warszawa<\/pre>\n<p>Solr  does not support default parameter named <em>pmiasto<\/em>, but with the key <em>v<\/em> (meaning value) and use of the reference operator <em>$<\/em> it was possible to  transfer the value of such a named parameter to the query.<\/p>\n<p>I recommend that everyone who wants to use this feature in your application to play with <em>LocalParams <\/em>and find out if they fit your needs.<\/p>\n<h3>More information<\/h3>\n<p>Actual information about <em>LocalParams<\/em> and Solr can be found at Apache Solr wiki at the following address: <a href=\"http:\/\/wiki.apache.org\/solr\/LocalParams\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/wiki.apache.org\/solr\/LocalParams<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Several months ago, during one of the projects I have tried to construct a query with optimal faceting. The problem was that we need filters (fq) in the query but in the same time we need a faceting that was<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[27],"tags":[313,175,191,190,192,164],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-solr-en","tag-1-4","tag-faceting-2","tag-local-2","tag-local-params-2","tag-params-2","tag-solr-2"],"_links":{"self":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":1,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/225\/revisions\/226"}],"wp:attachment":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}