Hibernate 搜索 - 通配符和 space
Hibernate Search - Wildcard and space
我正在将 Hibernate Search 与多个 words/fields 一起使用,并且在我启用通配符之前它按预期工作。
这是我的实体分析器:
@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)
关键字查询将始终匹配包含任何提供的关键字的文档。通配符查询不应用分析。所以两者都不适合你。
使用简单的 QueryString 查询,并强制默认运算符为 "and":
fullTextQuery = queryBuilder
.simpleQueryString()
.onFields("firstName", "lastName", "extraName", "biography")
.withAndAsDefaultOperator()
.matching(criteria)
.createQuery();
我正在将 Hibernate Search 与多个 words/fields 一起使用,并且在我启用通配符之前它按预期工作。 这是我的实体分析器:
@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)
关键字查询将始终匹配包含任何提供的关键字的文档。通配符查询不应用分析。所以两者都不适合你。
使用简单的 QueryString 查询,并强制默认运算符为 "and":
fullTextQuery = queryBuilder
.simpleQueryString()
.onFields("firstName", "lastName", "extraName", "biography")
.withAndAsDefaultOperator()
.matching(criteria)
.createQuery();