Solr Powered ISFDB – Part #7: Simple UI
Join the DZone community and get the full member experience.Join For Free
when we left last time, i had added pseudonym information for authors so that there was pretty good coverage of all the key pieces of information people might want to search on when looking for authors or titles. this week, i’m going to switch tacks and talk a little about one of my least favorite things to work on: user interfaces.
(if you are interested in following along at home, you can checkout the code from github . i’m starting at the blog_6 tag, and as the article progresses i’ll link to specific commits where i changed things, leading up to the blog_7 tag containing the end result of this article.)
disclaimer #1: i am not a ui guy
to start things off, i should make it clear that i don’t really like working on ui stuff. i’m most happy when i’m just hacking away on back-end code that crunches and returns data in a structured manner — so that other people can write ui code to display it in pretty ways. that said, uis are obviously important, and when working with search code/data it tends to be crucial to have some easy mechanism to really take a good look at the data you’ve got to work with while you iterate. i’m happy to read raw xml when i work with solr, but when tuning relevancy, and tweaking configs, you can frequently work a lot faster when you have a simple ui (even if it’s just a quick prototype) that helps you easily view results and facet counts.
solr has a plugin called solritas which can be used to generate uis directly in solr using velocity templates. since i need a simple ui, and since i don’t know anything about velocity, it seems like a good choice for building a basic ui today. (and learning some things in the process)
disclaimer #2: separation of logic and ui
i’m a big believer that a good application should have a clear separation between the application logic and the presentation of the results. while solritas has been used by a lot of people to build some very nice uis for solr, i personally don’t think it’s a good idea to have solr generating webpages that are consumed directly by users. i think of solr as a datastore — a repository of data/logic. i treat it the same way i treat a database, or a nosql repository, or a local file-system. i like it to be walled off from my users, and have them utilize it by accessing an application layer that talks to solr on their behalf.
with that said: there are certainly advantages to letting solr create an html based ui for you directly. when building prototypes, proof of concepts, internal tools, etc… it can definitely be handy to have one less moving part in your system. and for the purposes of this blog, where the primary purpose is to learn about and demonstrate solr features, there is very little downside.
it should also be noted that within solr there is already a decent separation between logic and presentation. solritas is implemented as a queryresponsewriter that has access to the solr response after the solrrequesthandler has processed it. it’s probably possible for a velocity template to contain logic and data processing, but you’d have to go out of your way to do so. so as long as you implement your velocity templates with a separation of concerns in mind, re-implementing them in another application (using the output from another queryresponsewriter) at a later date should be completely straight forward.
ok, enough disclaimers. time to get started.
step #1: reuse some templates
i really knew next to nothing about the velocityresponsewriter (or velocity!) when i got up this morning. what i did know is that you declare it in your solrconfig.xml, and then you point it at some velocity templates. so to start with, i copied the existing templates from the velocityresponsewriter sample configs along with the basic solrconfig.xml changes to use it. note that i also modified my build.xml slightly to copy the jars i’d need into my lib directory.
the result wasn’t the prettiest ui in the planet, but it wasn’t bad considering i didn’t have to write / modify any code…
without even looking at those sample templates, or learning anything
about velocity syntax, i was able to make some improvements just by
modifying my solrconfig.xml and schema.xml. i started be adding some
this gave me a quick and dirty left nav with some facet links so i
can see at a glance how the various documents are organized, but it also
exposed a few limitations in the templates i copied:
- they don’t seem to display “facet.query” counts by default
the template seems to have “/itas” hard coded in them (the path it
was registered to in the sample solrconfig.xml) which means the facet
links and search box won’t work since i picked a different name for the
now i could have dove in and start trying to fix these things, but i happen to know that a lot of work has been done on improving these templates for the next release of solr. even with out upgrading solr, i (was pretty certain i) would be able to upgrade just the templates and get all of their improved goodness. so i tried it , and things definitely got more visually pleasing…
…but there were still some problems…
- the newer templates have hard coded field names for display
- my query facets for filtering out pseudo authors showed up, but they appeared twice (once in the range faceting section?)
the new templates still had the request handler path hard coded, but now it’s hard coded to “/browse”
…so it was time to have to get my hands dirty and learn some velocity!
step #2: customizing templates
i started by changing how the individual results were displayed, so i could get the fields i want returned. poking around in the velocity directory, i found that the template responsible for generating the individual documents was called “doc.mv” (i found it by greping for “price:” since that seemed to be hard coded). the first thing i notice is that velocity directives seem to use a “#” prefix, and everything else looks like regular html … except for echoing variables and function output, that seems to use “$” … oh well. i figured i’d just start hacking on stuff and eventually i’d get the hang of it. (turns out “#” is how you refer to macros, and “$” is how you refer to objects, which can have methods)
i started with something simple: ripping out the “more like this” links. i plan to show off more like this at some point, but not today. (it’s particularly interesting that these mlt links seem like they were suppose to be hidden unless a special param was specified, but they were always showing up for me … all the more reason to rip it out). i also update the “result-title” div so that instead of displaying the “name” field, it displayed the fields “title” and “canonical_name” followed by doc_type. this way i would always have a good “header” for each item regardless of the doc_type. (ultimately i wanted to make the display conditional on the “doc_type”, but i started with baby steps ).
right off the bat, i discovered some problems. apparently i had some title docs with out a “title” field. not sure how that happened, i used a quick query to find all title docs, faceting on whether they have any terms in title field and i discovered that apparently none of the title docs have a title. clearly i introduced a bug a while back and never realized it.
looking at my dih config, i saw that i was most definitely indexing the “title” as an “imdb_url” field … obviously a stupid cut/paste mistake i made a while back and never noticed until now. go figure. once i fixed that i could move on to making other improvements to the fields that were displayed for each of my doc_types (and driving the presentation based on the type of doc).
all of these changes were made just by muddling my way through the velocity syntax using the existing templates as examples and knowing that these are all java objects under the covers. i’m sure a velocity expert would say my changes where very clunky and overly-complicated — but so far so good.
step #3: cleaning up some messes
so now i had a very rudimentary display, but there was still a lot of
“cruft” in the ui left over from the example templates i copied. i
wanted to rip all that out.
this was mostly straight forward
by greping for the text i saw being generated that i didn’t want…
- “boost by price” checkbox
- range facets (not available in solr 1.4.1, and the velocity template has price hard coded)
spatial (not available in solr 1.4.1, and the isfdb data doesn’t have any lat/lon fields
step #4: hyper-linking
at this point, the ui was still by no means pretty — but it was
functional and far easier to read then the raw xml (or json) output from
solr. (even if most fields still weren’t being displayed). the one
remaining thing i really wanted to do before wrapping up the day was to
add some hyper-linking:
- link titles to their authors
- link authors to their titles
link pseudonyms and authors to each other
i’m sure the “right” way to do a lot of this in velocity would be with some reusable macros, but i started simple (it can always be refactored later).
linking to individual authors
(from pseudonyms, or titles that only have one author) was fairly straight forward; as was
linking to the list of titles by an author
in both cases it was just a simple link using a single value from the
current result. linking to all authors of a title, or all pseudonyms of
an author was a bit trickier, because i needed to correlate values from
multiple fields (the “ids” and the “names”) and up until this point i’d
only dealt with simple “foreach” loops in velocity. so i had to go
, and figure out how to
use indexed for loops to build the links
it wasn’t too painful to accomplish. but i did struggle initially
because i was looking at the user guide for the wrong version of
velocity and the variable for accessing the look counter has changed.
conclusion (for now)
and that wraps up this latest installment with the blog_7 tag.
the end result is something that can display simple search results, with
titles and authors linking back and forth between each other. it’s
still pretty ugly, but i definitely think it’s useful…
next time i hope to make further improvements to the basic ui (hopefully with out getting sucked too deep into learning about velocity).
Opinions expressed by DZone contributors are their own.