Search in sleep mode

I am just starting with Hibernate Search. The code I use to perform 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 query terms are interpreted as terms in a clause (OR). I would like them to be interpreted as compound terms.

+3
source share
3 answers

If you use a query parser, you can do it like this:

    QueryParser queryParser = new QueryParser("all", new GermanSnowBallAnalyzer());
    queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    Query luceneQuery = queryParser.parse(QueryParser.escape(keyword));
+3
source

Since you are using the Hibernate DSL search query, you can write your query as:

Query luceneQuery = qb
    .bool()
      .must( qb.keyword().onField("title").matching(queryString).createQuery() )
      .must( qb.keyword().onField("description").matching(queryString).createQuery() )
    .createQuery();

Note that the query string is not parsed using the Lucene query parser. It should contain terms as they are searched (analyzers are used!)

+2
source

Hibernate Search , , "keyword()" , OR.

The links above have similar questions, hope this helps: Keyword Search (OR, AND) in Lucene https://forum.hibernate.org/viewtopic.php?f=9&t=1008903&start=0

+1
source

Source: https://habr.com/ru/post/1783828/


All Articles