In my project we are using hibernate search 4.5 with lucene-analyzers and solar.
I provide a text field to my clients. When they type in a phrase I would like to find all User
entities whose names include the given phrase.
For example consider having list of entries in database with following titles:
[ Alan Smith, John Cane, Juno Taylor, Tom Caner Junior ]
jun
should return Juno Taylor
and Tom Caner Junior
an
should return Alan Smith
, John Cane
and Tom Caner Junior
@AnalyzerDef(name = "customanalyzer", tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class), filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = { @Parameter(name = "language", value = "English") })
})
@Analyzer(definition = "customanalyzer")
public class Student implements Serializable {
@Column(name = "Fname")
@Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
private String fname;
@Column(name = "Lname")
@Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
private String lname;
}
I have tried with wildcard search but
Query luceneQuery = mythQB
.keyword()
.wildcard()
.onFields("fname")
.matching("ju*")
.createQuery();
How can I achieve this?
First, you didn't assign the analyzer to your field, so it isn't used currently. You should use @Field.analyzer.
Second, to answer your question, this kind of text is best analyzed with an
EdgeNGramFilter
. You should add this filter to your analyzer definition.EDIT: Also, to avoid queries such as "sathya" to match "sanchana" for instance, you should use a different analyzer when querying.
Below is a full example.
And then specifically mention that you want to use this "query" analyzer when building your query:
See also: https://stackoverflow.com/a/43047342/6692043
By the way, if your data includes only first and last names, you shouldn't use stemming (
SnowballPorterFilterFactory
): it will only make the search less accurate for no good reason.Why not use a standard
TypedQuery
?(where
String term
is your search-term)Didn't test this one, but something like this should do the trick.