Womprat Colorscheme for Vim

My main “IDE” is vim in a terminal. For years, I used the default 16-color colorscheme, but recently I was inspired by a few blog posts about vim colorschemes and solarized in particular. I tried solarized for a bit, but I didn’t like that I had to change my entire terminal color scheme, and I didn’t like the blue background (just weird) and grey text (too low-contrast).

After trying out a few other colorschemes, I decided to create my own. I started out with David Liang’s wombat256mod, which is in turn based on wombat by Lars Nielsen. With a few hours of tweaking, I had something that (IMHO) looked pretty good on CoffeeScript, PHP, Scala, and diffs.

Continue reading

Lucene Scoring and elasticsearch’s _all Field

At work, I’ve been building a search application using elasticsearch. In my first post on the topic, I talked about indexing and analysis and ignored scoring entirely. Scoring is a very complex topic, but I’ll try to address some of the basics in this post. I’ll also cover a specific scoring-related issue with elasticsearch’s _all field.

Continue reading

Using Scalatra’s halt() in a try/catch Block

Between Scala’s Option and lift’s Box (I use lift-mongorecord with Scalatra), I rarely use try {...} catch {...} in Scala. But on one occasion, I did end up using it to handle any problems that might come up while talking to an external service.

try {
  // call external service
} catch {
  case e => halt(500, e.getMessage)
}

Things worked fine at first. Later, I needed to add an authorization check in the try {...} block, which did a halt() if the check failed.

try {
  // call external service
  if (!authorized) {
    halt(401, "Authorization required")
  }
} catch {
  case e => halt(500, e.getMessage)
}

To my surprise, my app returned a 500 instead of the expected 401 if the external service returned something that failed the authorization check. After puzzling over it a bit, I remembered reading that Scalatra’s halt() works by throwing an exception under the hood. (Actually it throws a subclass of Throwable, more on that in a bit.) When the inner halt() is called, the 401 is caught by the catch {...} block and converted into a 500.

Continue reading

Scala’s DynamicVariable

For most of my career, I’ve built web applications in scripting languages like PHP or Ruby. In both of these environments, a new instance of your controller is instantiated for every request, so it’s trivial to set an instance variable in preDispatch() or before and use it later in the main action.

This year I became the author (and maintainer!) of a Scala REST service at work. We already had a few other services written in Scalatra, so I used it for my service, too. Last week, I needed to add a set of result filters to all of the actions in a controller. Request parameters dictate which result filters are applied. Simple enough, I thought — I’ll just create the filters in a before() block and assign it to an instance varible. Then in each action, I can retrieve them and pass them to the appropriate model.

If you’re a seasoned Java developer reading this, you’re probably shaking your head right now. But if you work primarily with PHP or Ruby, the problem might not be as obvious.

Continue reading

Testing Lucene Analyzers with elasticsearch

Up until now, I’ve been using elasticsearch as a way to speed up filtering, not as a search engine for human use. Using elasticsearch for filtering is relatively easy — all the inputs are normalized strings, dates, or numbers, documents either match or they don’t, and the order in which documents are returned is specified by the client.

My latest project is to build a search application for human use. Building search for humans is a lot harder. Instead of filtering by known fields with specific values, you have to match free text. Instead of a structured query, you get one text box. The result the user was searching for should appear in the first few results, and because Google is ubiquitous, users expect the results to be just as good.

Continue reading