For entity class I declare two analyzers, one is for indexing, another is for searching(query builder).
@AnalyzerDefs({
@AnalyzerDef(name = "nameAnalyzer",
tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(
factory = EdgeNGramFilterFactory.class, // Generate prefix tokens
params = {
@Parameter(name = "minGramSize", value = "1"),
@Parameter(name = "maxGramSize", value = "8")
}
)
}
),
@AnalyzerDef(name = "nameAnalyzer_query",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
}
),
})
For entity field, I just declare like this:
@Column(nullable = false, length = 50)
@Field(name = "firstName", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer"))
private String firstName;
For query building, if I want to override field with a new analyzer like:
EntityContext entityContext = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
.forEntity(Contact.class);
entityContext = entityContext.overridesForField("firstName", "nameAnalyzer_query");
It will throw non pointer exceptions, I check the log, it says the entity context could be found for “nameAnalyzer_query” analyzer.
But When I add a new @filed in that entity class like
@Column(nullable = false, length = 50)
@Fields({
@Field(name = "firstName", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer")),
@Field(name = "firstNameSearchField", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer_query"))
})
private String firstName;
It works !!!
My question is
- what is the reason for that?
- Adding a new field annotation is the correct way to solve that?
I am using 5.9.1.Final for hibernate search.