Adding Sphinx to your Java website with jsphinx

I’ve been using Sphinx on my FilmDev website to search user’s recipes and it’s been working really well.

So well that I wanted to add it to my Java websites too.

Setting up Sphinx on a rails site is made very easy thanks to the Thinking Sphinx plugin.

Unfortunately there is no such plugin for Java so setting it up requires a little more work (though not too much).

First off I downloaded and configured Sphinx until I could call search on the command line and get results back from my database.

I then grabbed the sphinxapi.jar from the downloaded package and dropped it into my WEB-INF/lib directory.

The Java source for that jar is included in the downloaded package – plus a file called “test.java” that I used as the starting point for my own code.

The test.java code works but is fairly basic, I’ve expanded upon it a fair bit and have put it in a github project called jsphinx.

Feel free to grab this code and use and amend as appropriate for your own site.

I encourage you to share any changes you make by forking it on github.

Bear in mind it’s coded against the 0.9.9-release version, I have no idea if it works with the 2.0.1-beta version.

The code includes examples for doing weighting, filtering and ordering.

The command object also supports pagination.

I’m using the code on this blog right now and it works great.

The final thing in that code is something to handle delta indexing.

That’s enough of an involved topic to warrant another blog post…

Leave a Reply

Your email address will not be published. Required fields are marked *