gpt4 book ai didi

java - hibernate 搜索: Indexed data with Ngram filter and while searching it gives incorrect result due to tokenizing while querying

转载 作者:行者123 更新时间:2023-12-02 09:00:05 29 4
gpt4 key购买 nike

我有一个具有此配置的分析仪,

searchMapping//
.analyzerDef(BaseEntity.CUSTOM_SEARCH_INDEX_ANALYZER, WhitespaceTokenizerFactory.class)//
.filter(LowerCaseFilterFactory.class)//
.filter(ASCIIFoldingFilterFactory.class)//
.filter(NGramFilterFactory.class).param("minGramSize", "1").param("maxGramSize", "200");

这就是我的实体字段的配置方式

@Field(analyzer = @Analyzer(definition = CUSTOM_SEARCH_INDEX_ANALYZER))
private String bookName;

这就是我创建搜索查询的方式

queryBuilder.keyword().onField(prefixedPath).matching(matchingString).createQuery()

我有一个值为 bookName="Gulliver"的实体,另一个值为 bookName="xGulliver"的实体;

如果我尝试使用数据bookName = xG进行搜索,那么我会得到两个实体,而我期望的实体只有bookName="xGulliver";还查看了 hibernate-search 生成的查询。

Executing Lucene query '+(+(+(+( bookName:x bookName:xg bookName:g))))

上面的 Lucene 查询是由 Lucene 使用 BooleanJunction::must 条件准备的,我想这意味着它应该匹配所有条件。仍然为什么它给我两个实体数据。这里不太明白。

我还可以在查询时通过使用 KeywordTokenizer 而不是 NGramFilterFactory 来覆盖分析器,但这就像我必须在创建 QueryBuilder 之前覆盖每个字段,这看起来不太好,因为然后我必须覆盖我拥有的所有索引字段100 个字段,其中一些是动态字段,我为每个字段创建单独的查询。

是否有其他方法可以覆盖 5.11 版本中的分析器,或者是否可以在 hibernate-search 6.x 版本中以更简单的方式处理分析器?

我使用的 Hibernate 版本是,

hibernate-search-elasticsearch, hibernate-search-orm = 5.11.4.Final

最佳答案

Above Lucene query is prepared using BooleanJunction::must conditions by Lucene I guess which means it should match all the conditions. Still why its giving me both entity data. I dont understand here.

当您创建keyword时当使用 Hibernate Search 进行查询时,会分析传递给该查询的字符串,如果有多个标记,Hibernate Search 会为每个标记创建一个带有一个“should”子句的 boolean 查询。你可以在这里看到“bookName:x bookName:xg bookName:g”:“bookName”之前没有“+”号,这意味着这些不是“must”子句,而是“should”子句。

I can also override the analyzer while querying by having KeywordTokenizer instead of NGramFilterFactory but this is like I have to override for each and every field before creating QueryBuilder which doesnt looks good because then I have to override all index fields which I have about 100 fields and some are dynamic fields and I create individual query for each field.

确实,这很烦人。

Is there any other way to override the analyzer in 5.11 version

在 5.11 中,我认为没有其他方法可以覆盖分析器。

如果有必要并且您正在使用 Lucene 后端,我相信您应该能够针对此特定查询绕过 Hibernate Search DSL:

  • 获取您想要的分析器:类似 Analyzer analyzer = fullTextSession.getSearchFactory().getAnalyzer("myAnalyzerWithoutNGramTokenFilter") .
  • 分析搜索词:调用 analyzer.tokenStream(...)并使用 TokenStream作为适当的。您将获得 token 列表。
  • 创建 Lucene Query :本质上它将是一个带有 TermQuery 的 boolean 查询。对于每个 token 。
  • 传递结果 Query像往常一样进入 hibernate 搜索。

or is it handled in some other way in hibernate-search 6.x version in easier way?

这在 Hibernate Search 6 中非常简单然后。解决办法有两种:

  • 隐式:在映射中,您不仅可以指定 analyzer (使用 @FullTextField(analyzer = "myAnalyzer") ),但也是 "search" analyzer使用@FullTextField(analyzer = "myAnalyzer", searchAnalyzer = "mySearchAnalyzer") 。索引时将使用“默认”分析器,而搜索(查询)时将使用“搜索”分析器。
  • 明确:在查询时,您可以通过调用 .analyzer("mySearchAnalyzer") 覆盖给定谓词的分析器。在构建谓词时。有一个例子in this section of the documentation .

但请注意,Hibernate Search 6 尚不支持动态字段:HSEARCH-3273 .

关于java - hibernate 搜索: Indexed data with Ngram filter and while searching it gives incorrect result due to tokenizing while querying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60228692/

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