【发布时间】:2020-07-25 13:15:23
【问题描述】:
我有一个带有 hibernate、lucene 和 hibernate-search 的 Java 后端。现在我想做一个模糊查询,但不是 0、1 或 2,我想允许查询和预期结果之间有更多的“差异”(以补偿例如长词中的拼写错误)。有没有办法实现这一目标?稍后将根据查询的长度计算允许的最大差异。
我想要的是自动完成搜索并纠正错误的字母。此自动完成应该只搜索给定查询后面的缺失字符,而不是前面的。如果查询前面的字符与条目相比缺失,则应计为差异。
示例:
此示例中允许的最大不同字符数为 2。
fooo 应该匹配
fooo (no difference)
fooobar (only characters added -> autocomplete)
fouubar (characters added and misspelled -> autocomplete and spelling correction)
fooo 不应该匹配
barfooo (we only allow additional characters behind the query, but this example is less important)
fuuu (more than 2 differences)
这是我当前的 SQL 查询代码:
FullTextEntityManager fullTextEntityManager = this.sqlService.getFullTextEntityManager();
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(MY_CLASS.class).overridesForField("name", "foo").get();
Query query = queryBuilder.keyword().fuzzy().withEditDistanceUpTo(2).onField("name").matching("QUERY_TO_MATCH").createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, MY_CLASS.class);
List<MY_CLASS> results = fullTextQuery.getResultList();
注意事项:
1. 我使用org.apache.lucene.analysis.ngram.EdgeNGramFilterFactory 进行索引,但这不应该有任何改变。
2.这是使用自定义框架,不是开源的。你可以忽略sqlService,它只提供FullTextEntityManager并处理hibernate周围的所有事情,每次都不需要自定义代码。
3. 此代码确实有效,但仅适用于withEditDistanceUpTo(2),这意味着QUERY_TO_MATCH 与数据库或索引中的匹配条目之间最多存在2 个“差异”。缺少的字符也算作差异。
4.withEditDistanceUpTo(2)不接受大于2的值。
有没有人有任何想法来实现这一目标?
【问题讨论】:
标签: java hibernate lucene hibernate-search