Joaquín L. Robles
Joaquín L. Robles

Reputation: 6504

How to get many terms matched using Hibernate Search query DSL?

When I search for "cars blue" I get every result that matches "cars" or "blue", but I need to match them both. I've read about setting some defaultOperator to AND but I can't find where to do that, Also I can't use PhraseQuery because the order of the terms in the search query is irrelevant, This is my code so far, thanks!

// create the query using Hibernate Search query DSL
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory()
    .buildQueryBuilder().forEntity(Articulo.class).get();

// a very basic query by keywords
BooleanJunction<BooleanJunction> bool = queryBuilder.bool();

bool.must(queryBuilder.keyword()
    .onFields("description")
    .matching(text)
    .createQuery()
);

Query query = bool.createQuery();

FullTextQuery jpaQuery =
      fullTextEntityManager.createFullTextQuery(query, Articulo.class);

return jpaQuery.getResultList();

Note: I'm using Hibernate Search 5.6.4

Upvotes: 0

Views: 362

Answers (1)

Guillaume Smet
Guillaume Smet

Reputation: 10539

I think you're looking for the Simple query string feature.

See http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#_simple_query_string_queries for more details about it.

You have an example with .withAndAsDefaultOperator():

Query luceneQuery = mythQB
    .simpleQueryString()
    .onField("history")
    .withAndAsDefaultOperator()
    .matching("storm tree")
    .createQuery();

This blog post explaining the rationale of this feature might be helpful too: http://in.relation.to/2017/04/27/simple-query-string-what-about-it/ .

Upvotes: 2

Related Questions