简体   繁体   English

QueryDSL和使用Lucene分析仪的休眠搜索

[英]QueryDSL & Hibernate-Search with Lucene Analyzers

I configured Hibernate-Search to use my custom analyzer when indexing my entities. 我将Hibernate-Search配置为在索引实体时使用我的自定义分析器。 However when I try and search with QueryDSL's Hibernate-Search integration, it doesn't find entities, but if I use straight hibernate-search it finds something. 但是,当我尝试使用QueryDSL的Hibernate-Search集成进行搜索时,它没有找到实体,但是如果我使用直接的hibernate-search,它将找到一些东西。

@AnalyzerDef(name = "customanalyzer",
    tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
    filters = {
            @TokenFilterDef(factory = LowerCaseFilterFactory.class),
            @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
                    @Parameter(name = "language", value = "English")
            })
    })
@Analyzer(definition = "customanalyzer")
public abstract class Post extends BaseEntity {}

I indexed an entity with a title of "the quick brown fox jumped over the lazy dog". 我索引了一个标题为“快速的棕色狐狸跳过了懒狗”的实体。

These work… 这些工作...

List articlePosts = fullTextEntityManager.createFullTextQuery(queryBuilder.keyword().onFields("title").matching("jumped").createQuery(), ArticlePost.class).getResultList(); // list of 2
List articlePosts = fullTextSession.createFullTextQuery(queryBuilder.keyword().onFields("title").matching("jumped").createQuery(), ArticlePost.class).getResultList(); // list of 2

This does not… 这不...

SearchQuery<ArticlePost> query = new SearchQuery<ArticlePost>(this.entityManagerFactory.createEntityManager().unwrap(HibernateEntityManager.class).getSession(), post);
List articlePosts = query.where(post.title.contains("jumped")).list() // empty list

But a search with how it is likely stored in Lucene (probable result of SnowballPorter), then it works… 但是搜索它可能存储在Lucene中的情况(SnowballPorter的可能结果),然后它就可以工作了……

SearchQuery<ArticlePost> query = new SearchQuery<ArticlePost>(this.entityManagerFactory.createEntityManager().unwrap(HibernateEntityManager.class).getSession(), post);
List articlePosts = query.where(post.title.contains("jump")).list() // list of 2

So it seems like when using QueryDSL, that the analyzer isn't being run before it does the query. 因此,当使用QueryDSL时,似乎没有在执行查询之前运行分析器。 Can anyone confirm this is the problem, and is there anyway to have them automatically run before QueryDSL runs the query? 任何人都可以确认这是问题所在,在QueryDSL运行查询之前,是否有任何方法可以让它们自动运行?

Regarding your question, the analyzer is applied per default when using the query DSL. 关于您的问题,使用查询DSL时默认情况下将应用分析器。 In most cases it makes sense to use the same analyzer for indexing and searching. 在大多数情况下,使用相同的分析器进行索引和搜索是有意义的。 For this reason the analyzer is applied per default unless 'ignoreAnalyzer' is used. 因此,除非使用“ ignoreAnalyzer”,否则默认情况下将应用分析器。

Why your second example does not work I cannot tell you. 为什么您的第二个示例不起作用,我无法告诉您。 SearchQuery is not part of the Hibernate Search or ORM API. SearchQuery不是Hibernate Search或ORM API的一部分。 It must be an internal class of your application. 它必须是您的应用程序的内部类。 What's happening in this class? 这堂课怎么了? Which type of query is it using? 它使用哪种查询?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM