In Hibernate Search 5.11 (and since 5.6 or 5.7, IIRC), you can define analyzers programmatically using a LuceneAnalysisDefinitionProvider
.
Implement the interface:
public class CustomAnalysisDefinitionProvider implements LuceneAnalysisDefinitionProvider {
@Override
public void register(LuceneAnalyzerDefinitionRegistryBuilder builder) {
builder.analyzer( "myAnalyzer" )
.tokenizer( KeywordTokenizerFactory.class )
.tokenFilter( ClassicFilterFactory.class )
.tokenFilter( LowerCaseFilterFactory.class )
.tokenFilter( StopFilterFactory.class )
// You can pass parameters like this
.param( "mapping", "org/hibernate/search/test/analyzer/stoplist.properties" )
.param( "ignoreCase", "true" );
// You can define multiple analyzers
builder.analyzer( "otherAnalyzer" )
.tokenizer( ... ) ...
}
}
Then tell Hibernate Search to use it:
# In properties.java
hibernate.search.lucene.analysis_definition_provider = com.mycompany.CustomAnalysisDefinitionProvider;
You're free to do whatever you want in the implementation of register
, so potentially you could check out system properties or even load configuration files.
If you have a limited set of implementations, you can also directly override the definition provider when starting the JVM by setting hibernate.search.lucene.analysis_definition_provider
through the system properties.
See https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#section-programmatic-analyzer-definition for details.
In Hibernate Search 6, APIs are a bit different, but they follow the same core principles: https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html_single/#backend-lucene-analysis , and you can even inject Spring/CDI beans into the analysis configurer (you can't do that in Search 5, or at least Hibernate Search won't help you to do it).