redZebra2012
redZebra2012

Reputation: 53

QueryDSL & Hibernate-Search with Lucene Analyzers

I configured Hibernate-Search to use my custom analyzer when indexing my entities. 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.

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

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. Can anyone confirm this is the problem, and is there anyway to have them automatically run before QueryDSL runs the query?

Upvotes: 2

Views: 1850

Answers (1)

Hardy
Hardy

Reputation: 19119

Regarding your question, the analyzer is applied per default when using the query 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.

Why your second example does not work I cannot tell you. SearchQuery is not part of the Hibernate Search or ORM API. It must be an internal class of your application. What's happening in this class? Which type of query is it using?

Upvotes: 0

Related Questions