gpt4 book ai didi

java - Hibernate 搜索 - 通配符和空格

转载 作者:行者123 更新时间:2023-12-02 09:58:08 26 4
gpt4 key购买 nike

我正在将 Hibernate Search 与多个单词/字段一起使用,并且在启用通配符之前它会按预期工作。这是我的实体分析器:

@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)

最佳答案

关键字查询将始终与包含任何提供的关键字的文档相匹配。通配符查询不应用分析。所以这两种情况都不适合您的情况。

使用 simpleQueryString 查询,并强制默认运算符为“and”:

        fullTextQuery = queryBuilder
.simpleQueryString()
.onFields("firstName", "lastName", "extraName", "biography")
.withAndAsDefaultOperator()
.matching(criteria)
.createQuery();

文档:https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#_simple_query_string_queries

关于java - Hibernate 搜索 - 通配符和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55845778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com