Hibernate Search and custom Analyzer (Obsolete)
This knowledge is obsolete in Hibernate Search 3.0 and above. You can define analyzers per entity, property and field using @Analyzer. You cna also define @AnalyzerDefs to customize analyzers.
-- Emmanuel
Problem :
HSearch only allows you to specify a single lucene analyzer via hibernate.search.analyzer. This works in simple scenarios where for example a single StandardAnalyzer is sufficient. However, it does not work when you for example build a multi language application which indexes fields with language specific stemmers. In this case your analyzer has to be flexible enough to switch the stemmer depending on the locale. This is currently not possible with HSEarch.
Objective
- Implement a solution which allows a switch between several lucene analyzers while still only having a single property in the hibernate configuration.
Solution
The solution is to write your own analyzer, eg myApp.lucene.analysis.MyAnalyzer which you configure as default analyzer via hibernate.search.analyzer.
This analyzer then gets its required parameters via a ThreadLocal variable. In this case the current locale. There are many ways of getting the locale into the ThreadLocal. I was using for example a Struts2 interceptor to do this.
Depending on the locale you custom analyzer can then switch the stemmer.
Variations
Sometimes you also need different analyzers depending on the field names. For example in the case where you would like to index books with the fields summary and isbn. In this case you maybe want to use the GermanAnalyzer the summary and a KeywordAnalyzer for the isbn number. In this your custom Analyzer should extend PerFieldAnalyzerWrapper.
public MyAnalyzer() {
super(new SimpleAnalyzer());
this.addAnalyzer("summary", new GermanAnalyzer());
this.addAnalyzer("isbn", new KeywordAnalyzer());
}
Finally, a combination of PerFieldAnalyzerWrapper and ThreadLocal is of course possible.