I have a chance to use Lucene (3.6.0) to implement a full-text search in one of my recent projects. One of the requirements is to highlight the matched text in the result. The highlighted text should be displayed in the whole paragraph (not just a small text fragment). Here is my snippet to achieve this:

private String getHighlightedField(Query query, Analyzer analyzer, String fieldName, String fieldValue) throws IOException, InvalidTokenOffsetsException {
    Formatter formatter = new SimpleHTMLFormatter("", "");
    QueryScorer queryScorer = new QueryScorer(query);
    Highlighter highlighter = new Highlighter(formatter, queryScorer);
    highlighter.setTextFragmenter(new SimpleSpanFragmenter(queryScorer, Integer.MAX_VALUE));
    highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE);
    return highlighter.getBestFragment(this.analyzer, fieldName, fieldValue);
}
  • By creating a SimpleSpanFragmenter with a very big fragment size, we can display the highlighted text in the whole paragraph or document. Lucene also does a nice thing here for free, by merging all the highlighted text fragments into one big chunk (or our original paragraph/document)
  • query: is the Lucene query you constructed to do the search
  • analyzer: is the Lucene analyzer are used to analyzed the field when you create the index for that field

Happy highlighting!

I've just joined a project as a freelancer to customize a commercial e-commerce solution (so let called it EP). OOTB, the email sending of EP does not support authentication, which is a weird thing for such a cool product like that. Actually, EP does declare an JNDI mail session in the context, but in the code they don't use it. With the source code of EP in hand, things get much easier. First, we declare a JNDI resource in our application context (either in your context file in the conf/Catalina/localhost/ or in your context.xml under META-INF inside your war):
<context>
<Resource name="mail/Session" auth="Container"
type="javax.mail.Session"
username="my.smtp.user"
password="my-secret"
mail.debug="false"
mail.transport.protocol="smtp"
mail.smtp.host= "xxx.xxx.xxx.xxx"
mail.smtp.auth= "true"
mail.smtp.port= "25"
mail.smtp.starttls.enable="true"
description="Global E-Mail Resource"
/>
</context>
And put those in your web.xml file:
<resource-ref>
<description>Email Session</description>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And now, in your Java code, you will use this email session like this:
Context initContext = new InitialContext();
Context context = (Context) initContext.lookup("java:comp/env");
Session mailSession = (Session) context.lookup("mail/Session");
//Your code to send the email goes here
If you get a java.lang.ClassCastException, don't worry, this is a known issue. It's because you have multiple Java Mail jar file and Java Activation Framework jar file in your CLASSPATH!
Hope that may help!
Google App Engine (GAE) has been around for some time. But I don't have time/chance to try it out. A couple of days ago, I registered and activated my account with the help of a close friend in the US because Google uses SMS for the activation process and Vietnam is not in the list of supported countries of GAE right now (or any Vietnamese carriers either).
So what is my first application? It's a IP lookup application. So why? A couple of days ago, my co-worker complained about site providing the same service because it contains too much distracted content. He just want a deadly simple alternative :). Here is my answer http://ip-bot.appspot.com
This is the first draft-unstable-alpha-0 version :), so don't expect to much. I will make it a *feature-rich* site but the simplicity is the top priority. So stay tuned.