A few months ago, at the Blacklight Summit, I learned that Blacklight defines certain settings in solrconfig.xml
to serve as shortcuts for a group of fields with different boost values. For
example, in our Blacklight installation we have a setting for author_qf
that references four specific author fields with different boost values.
In this case author_qf
is a shortcut that we use when issuing searches by author. By referencing author_qf
in our request to Solr we don’t have to list all four author fields (author_unstem_search, author_addl_unstem_search, author_t, and author_addl_t) and their boost values, Solr is smart enough to use those four fields when it notices author_qf
in the query. You can see the exact definition of this field in our GitHub repository.
Although the Blacklight project talks about this feature in their documentation page and our Blacklight instance takes advantage of it via the Blacklight Advanced Search plugin I had never really quite understood how this works internally in Solr.
LocalParams
Turns out Blacklight takes advantage of a feature in Solr called LocalParams. This feature allows us to customize individual values for a parameter on each request:
LocalParams stands for local parameters: they provide a way to “localize” information about a specific argument that is being sent to Solr. In other words, LocalParams provide a way to add meta-data to certain argument types such as query strings. https://wiki.apache.org/solr/LocalParams
The syntax for LocalParams is p={! k=v }
where p
is the parameter to localize, k
is the setting to customize, and v
the value for the setting. For example, the following
uses LocalParams to customize the q
parameter of a search. In this case it forces the query field qf
parameter to use the author
field when it searches for “jane”.
Dereferencing
When using LocalParams you can also use dereferencing to tell the parser to use an already defined value as the value for a LocalParam. For example, the following example shows how to use the already defined value (author_qf
) when setting the value for the qf
in the LocalParams. Notice how the value is prefixed with a dollar-sign to indicate dereferencing:
When Solr sees the $author_qf
it replaces it with the four author fields that we defined for it and sets the qf
parameter to use the four author fields.
You can see how Solr handles dereferencing if you pass debugQuery=true
to your Solr query and inspect the debug.parsedquery
in the response. The previous query would return something along the lines of
Notice how Solr dereferenced (i.e. expanded) author_qf
to the four author fields that we have configured in our solrconfig.xml
with the corresponding boost values.
It’s worth noticing that dereferencing only works if you use the eDisMax parser in Solr.
There are several advantages to using this Solr feature that come to mind. One is that your queries are a bit shorter since we are passing an alias (author_qf
) rather than all four fields and their boost values, this makes reading the query a bit clearer. The second advantage is that you can change the definition for the author_qf
field on the server (say to add include a new author field in your Solr index) and the client applications automatically will use the definition when you reference author_qf
.