我正在使用具有多个单词/字段的休眠搜索,并且在启用通配符之前,它可以按预期工作。 这是我的实体分析器:
@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") })
})
这是我的查询:
if(criteria.length() > 0) {
fullTextQuery = queryBuilder
.keyword()
// .wildcard() => not in use
.onFields("firstName", "lastName", "extraName", "biography")
.matching(criteria)
.createQuery();
场景01
如果我搜索“ John Smith”之类的东西,则会得到以下查询:
+((firstName:john firstName:smith)
(lastName:john lastName:smith)
(extraName:john extraName:smith)
(biography:john biography:smith))
找到名字叫John或Smith的人。
场景02 如果我搜索“ John *”,因为我想找到名字以John开头的任何人(Johnny,Johson),则需要启用通配符,如下所示:
fullTextQuery = queryBuilder
.keyword()
.wildcard()
当我通过返回以下查询键入“ John *”时,这将起作用:
+(firstName:john* lastName:john* extraName:john* biography:john*)
但是当我键入“ John * Smith”之类的字词时,它不再起作用了,因为它不会拆分单词,我认为这是由于通配符选项所致:
+(firstName:john* smith lastName:john* smith extraName:john* smith biography:john* smith)
答案 0 :(得分:1)
关键字查询将始终匹配包含任何提供的关键字的文档。通配符查询不应用分析。因此,这也不适合您的情况。
使用simpleQueryString查询,并将默认运算符强制为“ and”:
fullTextQuery = queryBuilder
.simpleQueryString()
.onFields("firstName", "lastName", "extraName", "biography")
.withAndAsDefaultOperator()
.matching(criteria)
.createQuery();