Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate search beginner question

I am just beginning with Hibernate Search. The code I am using to do the search is taken from the reference guide:

FullTextEntityManager fullTextEntityManager =
    Search.getFullTextEntityManager(em);
EntityTransaction transaction = em.getTransaction();

try
{
    transaction.begin();

    // create native Lucene query using the query DSL
    // alternatively you can write the Lucene query using the
    // Lucene query parser or the Lucene programmatic API.
    // The Hibernate Search DSL is recommended though
    SearchFactory sf = fullTextEntityManager.getSearchFactory();
    QueryBuilder qb = sf
      .buildQueryBuilder().forEntity(Item.class).get();

    org.apache.lucene.search.Query query = qb
      .keyword()
      .onFields("title", "description")
      .matching(queryString)
      .createQuery();

    // wrap Lucene query in a javax.persistence.Query
    javax.persistence.Query persistenceQuery = 
    fullTextEntityManager.createFullTextQuery(query, Item.class);

    // execute search
    @SuppressWarnings("unchecked")
    List<Item> result = persistenceQuery.getResultList();

    transaction.commit();

    return result;
}
catch (RuntimeException e) 
{
    transaction.rollback();
    throw e;
}

I notice that the query terms are interpreted as terms in a disjunction(OR). I would like them to be interpreted as conjunction terms instead.

like image 995
Panayiotis Karabassis Avatar asked Dec 07 '25 09:12

Panayiotis Karabassis


1 Answers

If you use the Query parser, then you could do it this way:

    QueryParser queryParser = new QueryParser("all", new GermanSnowBallAnalyzer());
    queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    Query luceneQuery = queryParser.parse(QueryParser.escape(keyword));
like image 191
Ralph Avatar answered Dec 09 '25 22:12

Ralph