2
votes

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?

1
Did you try the direct full text query also via the Session or only via the EntityManager?Timo Westkämper
I hadn't, but I just tested, and added the results into my example. With either Session or EntityManager, I still get the correct results with native Hibernate-Search. I expanded my example to show if I'm using QueryDSL, I can get it to work if I search on how it is likely stored in Lucene after the analyzers are applied.redZebra2012
Ok, so I think I figured it out, or at least a way to get the queries I am looking for. Looks like you work for Mysema, so I'll run this by you before I answer my own question. Queries created with QueryDSL for Hibernate-Search seem to be created with explicit equality when passed off to Hibernate-Search. However, QueryDSL's Hibernate-Search module also provides a new predicate LuceneUtils.fuzzyLike(propertyPath, value) that gives me the results I'm looking for. I suspect I'm required to use that instead of object.propertyPath.like(value) when querying Lucene.redZebra2012
Maybe the fuzzy like query could be used be used internally for stringPath.matches(something) ?Timo Westkämper

1 Answers

0
votes

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?