Logical Queries in Hibernate Search

I am trying to understand queries generally in Hibernate Search. I have some problems understanding the forEntity(...) method. This is what the documentation says:

See how to use the API. First you need to create a query builder that is bound to this indexed type of object. This QueryBuilder will know which analyzer to use and which field bridge to use. You can create several QueryBuilders (one for each entity type that is at the root of your query). You get a QueryBuilder from SearchFactory.

From the section: 5.1.2. Build a Lucene query with a Hibernate DSL search query

 QueryBuilder mythQB = searchFactory.buildQueryBuilder().forEntity( Myth.class ).get(); 

Above, you see that you must name the entity. What should you do when you want to create your own query builder to create a logical query inside the "root" query? What should you also tie?

Say I want to have a boolean query that must match either Apples or Pie. These are two different objects, so currently I have two different query builders for them. But I need a third to create a logical query. Should this be tied to the Object class?

+5
source share
2 answers

If you want to be able to return multiple objects from a single query, you can use QueryBuilder , as you said, but you must specify multiple objects in a createFullTextQuery call. For example, if you have a Book object and a Movie object, and you want to find all books and movies whose names begin with d , you can use the following query:

 QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get(); Query query = queryBuilder.keyword().wildcard().onField("title").matching("d*").createQuery(); org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(query, Book.class, Movie.class); 

Note that the query builder is only created with the Book tag, but both Book and Movie are specified in the createFullTextQuery call.

+3
source

Logical operators are called instead of OR because of the names that they have in the Lucene API and documentation, and because it is more appropriate: this not only affects the logical decision, but also affects the result of the result.

For example, if you are looking for Fiat or blue cars, cars branded Fiat & blue will also be returned and have a higher score than blue but not Fiat cars.

This may seem cumbersome because it is software and provides many detailed options. A simpler alternative is to use a simple string for your query and use QueryParser to create the query. As a rule, a parser is useful for analyzing user input, it is easier for a programmer to deal with clearly defined fields; for example, if you have the collection you mentioned, it's easy to build it in a for loop.

 Collection<String> namesCollection = getNames(); // Contains "billy" and "bob", for example StringBuilder names = new StringBuilder(100); for(String name : namesCollection) { names.append(name).append(" "); // Never mind the space at the end of the resulting string. } QueryBuilder b = fts.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get(); Query luceneQuery = b.bool() .should( // Searches for multiple possible values in the same field b.keyword().onField("firstName").matching( sb.toString() ).createQuery() ) .must(b.keyword().onField("lastName").matching("thornton").createQuery()) .createQuery(); 
+1
source

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


All Articles