Reputation: 2141
I configured Hibernate Search with Apache Lucene to more easly search enitites by given coordinates. Now I'm wondering how I can add more criteria to search entities. I'm also using Spring Data project and Query DSL. I'm trying to use Predicate
class from QueryDSL to keep my app consistent. There is any posibility to do this?
My Place.class
@Getter
@Setter
@Entity
@Indexed
@Spatial
public class Place implements Serializable {
private static final long serialVersionUID = -8379536848917838560L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "place_id")
private Long id;
//...
@Longitude
private Double lng;
@Latitude
private Double lat;
//...
}
My repository class
@PersistenceContext(type = PersistenceContextType.EXTENDED)
EntityManager em;
@Override
@Transactional
public List findAll(Coordinates coordinates, Predicate predicate, Pageable pageable, int maxDistance) {
FullTextEntityManager fullTextSession = Search.getFullTextEntityManager(em);
QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Place.class).get();
org.apache.lucene.search.Query luceneQuery = builder
.spatial()
.within(maxDistance, Unit.KM)
.ofCoordinates(coordinates)
.createQuery();
FullTextQuery jpaQuery = fullTextSession.createFullTextQuery(luceneQuery, Place.class);
return jpaQuery.getResultList();
}
Upvotes: 0
Views: 1056
Reputation: 6107
I'm assuming you're not asking about combining multiple full-text queries with a Boolean Query - as I hope the docs have enough examples about that - but that you're asking about adding SQL based restrictions.
In practice it is possible to restrict a FullTextQuery
further by applying a Criteria
, however while this seems to work, this feature was not intentional (fun story behind this!), is poorly tested and is not efficient at all: avoid if possible. It wasn't removed as some people really like it.
You can find an example - and warnings about the limitations - in Example "5.12. Specifying FetchMode on a query": - http://docs.jboss.org/hibernate/search/5.6/reference/en-US/html_single/
A better solution is to run a full-text-query by restricting only on indexed fields, so that the query can be fully resolved by Lucene only.
Sometimes it helps to encode additional fields in the index, somehow "pre-tagging" Lucene Document which you want to match. This is where people need to get a bit creative, and you might write some custom FieldBridge
and ClassBridge
implementations.
Then you apply a Full-Text Filter, or combine multiple queries with boolean queries: see section "5.1.2.8. Combining queries".
Upvotes: 1