Wednesday, July 15, 2009

An application of Function Queries in Solr

Suppose you have a set of Solr documents. You wish to push the subset of documents having a particular field with value X to the top of the search results, effectively superseding Solr's default scoring mechanism. For example, your index contains a collection of computers for sale and the particular field's name is "operatingsystemtype" . Possible values for this field are: 1 (Windows XP), 2 (Vista), 5 (Mac OS 10), and 7 (Debian). You wish to push all the documents with operatingsystemtype = 5 to the top.

One way to implement this is to embed a _val_ hook right after the q parameter in the HTTP query string you send to Solr. For example: http://search.buycomputers.com/select/?q=(price:[1000 TO 1500])+_val_="map(operatingsystemtype,5,5,1000)"&fl=id,name,price,details.
  • Note the '+' instead of the usual '&'.
  • The map function will map all values between 5 and 5 inclusive to 1000. (See here for more details.) Although this 1000 is normalized to a smaller value, it is added to the document's score. Basically, all documents with operatingsystemtype = 5 will have a very large constant added to their score, effectively pushing all these documents to the top.
  • Please note that this method relies on Solr's default sort (i.e. score desc). If you provide a sort parameter, thus overriding the default order, this method won't work.
  • This is called a Function Query in Solr parlance.

Wednesday, June 17, 2009

Tip: Eclipse auto-compile errors

If your Eclipse project has auto-compile errors popping up all over the place, and Eclipse can't even comprehend simple Java imports, make sure rt.jar, the Java run-time library, is in your project's Libraries path. Go to Project -> Properties -> Java Build Path -> Libraries and make sure rt.jar is there. If it isn't, add it. This fixed the problem for me.

Friday, June 12, 2009

Tips: Spring MVC and messages.properties

Tip #1:

If a message in your messages.properties file contains single quotes, you may find that these do not display properly. The trick is to use Unicode hex values instead. That said, avoid using \u0027, the Unicode hex value for apostrophe, as it may not work correctly. (It didn't for me.) Use \u2018 and \u2019 instead. These are, respectively, the left single quotation mark and the right single quotation mark.

So, for example, instead of...
errors.required.parameter=Required parameter '{0}' is not present.
... use...
errors.required.parameter=Required parameter \u2018{0}\u2019 is not present.

Tip #2:

If you pass a number parameter into a message in your messages.properties file, it will automatically be formatted as a number and commas will be inserted. For example, 12300 will be displayed as 12,300. If this number is an identifier, and you don't want commas to be inserted, convert the number to a string before passing it to your translation mechanism.

Example:

The message definition is:
errors.proposal.not.found=Proposal {0} not found.

Code:
Long proposalId = 227128;
String errorMessage= org.springframework.context.MessageSource.getMessage
("errors.proposal.not.found", new Object[] {
proposalId}, Locale.getDefault());

Error message:
Proposal 227,128 not found.

Change your code to:
org.springframework.context.MessageSource.getMessage
("errors.proposal.not.found", new Object[] {
proposalId.toString()}, Locale.getDefault());

Now the error message is:
Proposal 227128 not found.

Tuesday, June 9, 2009

User-friendly binding error messages in Spring MVC

In some cases, Spring's binding error messages are not fit to display to the user. How can we get around this? If we override SimpleFormController.onBindAndValidate() in our controller, in the hopes of modifying the BindException parameter accordingly, we'll be hard-pressed to find methods allowing us to do so.

One way to display user-friendly binding error messages is to override SimpleFormController.onBindAndValidate() as follows:

@Override
protected void onBindAndValidate(HttpServletRequest request, Object command,
BindException errors) throws Exception {
if (errors.hasErrors()) {
Map errorsMap = new HashMap();
List errorList = errors.getAllErrors();
for (FieldError error : errorList) {
String msg = "";
try {
msg = this.translate(error.getCode(), error.getArguments());
} catch (Exception e) {}
if (msg.contains("quantity")) {
msg = msg.replace(error.getField(), "quantity");
} else if (msg.contains("unitPrice")) {
msg = msg.replace(error.getField(), "unit price");
}
msg += ".";
errorsMap.put(error.getField(), msg);
}
request.setAttribute("errorsMap", errorsMap);
}
}
This creates a map of user-friendly error messages, keyed by the field names with which the error messages are associated. You can then use it in your JSP, possibly with the tag.

Monday, April 20, 2009

Tip: Velocity

If you are using the #if #else #end construct provided by Velocity, remember to put a space after the #else. If you don't, Velocity will ignore the #else and consider the statement to be a simple #if #end.

Thursday, April 16, 2009

Use BigDecimal for financial calculations

It is common to use the Double object or the double primitive type for financial calculations. Since you have no control over rounding behavior, mistakes may arise in your calculations. You might charge customers more (or less) than they owe. Use BigDecimal to solve this problem. For a clear, thorough explanation, see this document.

Monday, April 6, 2009

Tip: Tomcat startup exception

If, while starting up Tomcat, you get this error - SEVERE: Exception loading sessions from persistent storage - try to delete the SESSIONS.ser from your $CATALINA_HOME/work directory.