シナリオ
クラス従業員、クラス オフィス、クラス OfficeEmployee。
Class officeは空間エンティティであり、検索可能で、期待どおりの結果を返します。
Office-Employee 間の manyToMany 関係は、クラスOfficeEmplyeeにマップされます。
今、ある範囲内の特定の人に基づいて検索を実行する必要があります。つまり、範囲内のオフィスと、それらのオフィスに存在する従業員を確認する必要があります。つまり、OfficeEmployeeエンティティを検索します。
3 つのクラスすべてに索引が付けられます。
オフィス従業員
// 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) で検索を実行したいと考えています。
しかし、私はそれを機能させることができないようです!チュートリアルをチェックしましたが、そのような例は見つかりませんでした。
- 私が説明したように、現在インデックス付けされているエンティティを使用することは可能ですか?
- OfficeEmployee に @Spatial を含める必要がありますか? ただし、それには新しいインデックス作成が別途必要になるため、現在インデックス付けされているものを使用したいと考えています。
- 検索を実行すると、@Spatial と @SpatialFieldBridge を確認する必要があると表示され、そのように注釈を付けても、結果は空です。
- 空間エンティティが座標を実装していて、座標用の別のフィールドがない場合、 @ContainedIn はどこに配置する必要がありますか?
誰でも私を正しい方向に向けることができますか?