The contrib module, velocity
, nicknamed
Solritas, is a simple template engine that lets you build user interfaces directly in Solr using
Apache Velocity, a very simple macro language to generate the HTML. It's similar to JSP or PHP, but with a simpler syntax consisting of just a handful of commands. It is very simple to pick up, as you can see in the following snippet of code, for rendering the HTML that displays the ID and name of an artist pulled from the first Solr document in a list of results:
#set($doc = $response.results.get(0)) #set($id = $doc.getFieldValue("id")) <div>ID: $id</div> <div>Name: #field('a_name')</div>
When a Velocity template is invoked, Solritas places some objects, indicated with a $
character, into a rendering context that you can use, such as $response
and $request
. In the preceding example, you can see that the first result in the response is assigned to the $doc
object variable using the #set
command. Java methods such as getFieldValue()
are easily called in Velocity, allowing you to access the full power of Java within a scripting environment that is evaluated at runtime. Velocity also supports building your own functions, such as the #field()
function for displaying a field from a document.
You can try out an interface optimized for searching for MusicBrainz artists by browsing to http://localhost:8983/solr/mbartists/browse
. This web interface supports faceted browsing, autocompletion of queries, boosting of artists based on how recent the release is, "More Like This" based on artist name, and even "Did You Mean" spell checking!
When the browser invokes the URL, Solr hands the request off to a request handler with the name, /browse
, which is a search request handler that works like any other. The point where the request takes a different turn is in rendering the response, which in Solr is configured with the wt
parameter. Short for
writer type, the choices are better known as response writers. Instead of letting it default to XML, it's set to velocity
. The Velocity response writer uses the v.layout
and v.template
parameters to determine which template file to use for the overall page layout as well as what template for the specific page to render. The templates are located in conf/velocity/
relative to the Solr core, and they end in .vm
. To use another directory, set the v.base_dir
parameter. Note that the use of parameters to choose the template allows you to override it in the URL if desired.
<?xml version="1.0"?> <requestHandler name="/browse" class="solr.SearchHandler"> <lst name="defaults"> <str name="wt">velocity</str> <str name="v.template">browse</str> <str name="v.layout">layout</str> <str name="title">MusicBrainz</str> <str name="defType">edismax</str> <str name="mm">1</str> <str name="q.alt">*:*</str> <str name="rows">10</str> <str name="fl">*,score</str> <str name="qf">a_name^1.5 a_member_name^1.0</str> <str name="pf">a_name^1.5 a_member_name^1.0</str> <str name="mlt.qf">a_name^1.5 a_member_name^1.0</str> <str name="mlt.fl">a_name,a_member_name</str> <int name="mlt.count">3</int> <int name="mlt.mintf">1</int> <int name="mlt.mindf">2</int> <str name="mlt.boost">true</str> <str name="facet">on</str> <str name="facet.field">a_type</str> <str name="facet.field">type</str> <str name="facet.mincount">1</str> <str name="facet.range">a_release_date_latest</str> <str name="f.a_release_date_latest.facet.range.start">NOW/YEAR-10YEARS</str> <str name="f.a_release_date_latest.facet.range.end">NOW</str> <str name="f.a_release_date_latest.facet.range.gap">+1YEAR</str> <str name="f.a_release_date_latest.facet.range.other">before</str> <str name="f.a_release_date_latest.facet.range.other">after</str> <str name="spellcheck">on</str> <str name="spellcheck.dictionary">a_spell</str> <str name="spellcheck.collate">true</str> <str name="hl">on</str> <str name="hl.fl">a_name a_member_name</str> <str name="f.a_name.hl.fragsize">0</str> <str name="f.a_name.hl.alternateField">a_name</str> </lst> <arr name="last-components"> <str>spellcheck</str> </arr> </requestHandler>
Although it is good to impress your boss by quickly building a remarkably full-featured search interface using Solritas, there are some cons to keep in mind:
solrconfig.xml
and hardcode them in the Velocity template and that means you'll have to customize the templates to fit your schema. This can be a bit of a gotcha!/browse
out-of-the-box interface has reached that complexity point since there is no strong MVC model to follow.However, some aspects of what I really love about Solritas are:
http://localhost:8983/solr/mbartists/select?limit=1&q=corgan&qt=mb_artists&wt=velocity&v.template=fragment
returns a small fragment of HTML rendered by the completely standalone Velocity template fragment.vm
:To learn more about building your own Velocity-based interface, look at the example code in /configsets/mbtype/conf/velocity
. The example application that ships with Solr also has some good examples of exposing Solr features, such as spatial search using Velocity. You can get more information about the list of tools and objects added to the rendering context from the Solr wiki at http://wiki.apache.org/solr/VelocityResponseWriter. More information about Velocity is available at http://velocity.apache.org/.
18.216.34.146