I am using Hibernate Search with multiple words/fields and it works as expected until I enable Wildcards. This is my Entity Analyzer:
@AnalyzerDef(name = "autocompleteAnalyzer",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
// remove accents
@TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
// lower case
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
// Start with same root
@TokenFilterDef(
factory = SnowballPorterFilterFactory.class,
params = { @Parameter(name = "language", value = "English") })
})
And this is my query:
if(criteria.length() > 0) {
fullTextQuery = queryBuilder
.keyword()
// .wildcard() => not in use
.onFields("firstName", "lastName", "extraName", "biography")
.matching(criteria)
.createQuery();
Scenario 01
If I search for something like "John Smith", I do get the following query out:
+((firstName:john firstName:smith)
(lastName:john lastName:smith)
(extraName:john extraName:smith)
(biography:john biography:smith))
Which finds anybody named John or Smith.
Scenario 02 If I search for "John*", because I want to find anybody where the name start with John (Johnny, Johson) I need to enable wildcards as following:
fullTextQuery = queryBuilder
.keyword()
.wildcard()
Which works when I type "John*" by returning the following query:
+(firstName:john* lastName:john* extraName:john* biography:john*)
But it doesn't work anymore when I type something like "John* Smith" because it doesn't split the words, which I believe is due to the Wildcard option:
+(firstName:john* smith lastName:john* smith extraName:john* smith biography:john* smith)
Keyword queries will always match documents that contain any of the provided keywords. Wildcard queries do not apply analysis. So neither is a good fit in your case.
Use a simpleQueryString query, and force the default operator to "and":
fullTextQuery = queryBuilder
.simpleQueryString()
.onFields("firstName", "lastName", "extraName", "biography")
.withAndAsDefaultOperator()
.matching(criteria)
.createQuery();
Documentation: https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#_simple_query_string_queries
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.