班级员工,班级办公室,班级员工。
班级办公室是一个空间实体,可以搜索并按预期返回结果。
Office-Employee之间的manyToMany关系映射到Class OfficeEmplyee 。
现在我需要根据某些范围内的某些人进行搜索。换句话说,我必须检查范围内的办公室以及存在于这些办公室的员工,即搜索 OfficeEmployee 实体。
所有三个类都被编入索引。
// reference Spatial indexed entity Office
@IndexedEmbedded
@ManyToOne (cascade = CascadeType.MERGE)
@JoinColumn(name="office")
private Office office;
// reference to employee
@IndexedEmbedded
@JsonIgnore
@ManyToOne (cascade = CascadeType.MERGE)
@JoinColumn(name="employee")
private Employee employee;
@JsonIgnoreProperties(ignoreUnknown=true)
@Spatial(name = "office_location_poi", spatialMode = SpatialMode.HASH )
@Indexed
@Entity
@Embeddable
public class Office implements Serializable,Coordinates {
// some attributes , getters , setters..
@ContainedIn
@OneToMany(mappedBy="office", cascade=CascadeType.ALL)
private List<OfficeEmployee > officeEmployees;
@Latitude
double latitude;
@Longitude
double longitude;
public Coordinates getLocation() {
return new Coordinates() {
@Override
public Double getLatitude() {
return latitude;
}
@Override
public Double getLongitude() {
return longitude;
}
};
}
@Override
public Double getLatitude() {
return latitude;
}
@Override
public Double getLongitude() {
return longitude;
}
}
final QueryBuilder builder = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity( OfficeEmployee.class ).get();
double centerLatitude = searchTerm.lat;
double centerLongitude =searchTerm.lng;
org.apache.lucene.search.Query luceneQuery = builder.spatial().onField("office").within(searchTerm.distance, Unit.KM)
.ofLatitude(centerLatitude)
.andLongitude(centerLongitude)
.createQuery();
org.hibernate.search.jpa.FullTextQuery hibQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, OfficeEmployee.class);
// sort
Sort distanceSort = new Sort(
new DistanceSortField(centerLatitude, centerLongitude, "office_location_poi"));
hibQuery.setSort(distanceSort);
hibQuery.setProjection(FullTextQuery.SPATIAL_DISTANCE, FullTextQuery.THIS);
hibQuery.setFirstResult(0);
hibQuery.setMaxResults(20);
// results
List<Office>results =hibQuery.getResultList();
现在我想在关系表(OfficeEmployee)上执行搜索。
但听起来我无法让它发挥作用!我检查了教程,但找不到这样的例子。
任何人都可以指出我正确的方向吗?