Hibernate搜索多个空间搜索
我有一个具有Hibernate Search@Spatial data的实体A,如下所示:Hibernate搜索多个空间搜索,hibernate,jpa,geospatial,hibernate-search,Hibernate,Jpa,Geospatial,Hibernate Search,我有一个具有Hibernate Search@Spatial data的实体A,如下所示: @Entity @Spatial(spatialMode = SpatialMode.HASH) @Indexed public class A { @Id @GeneratedValue(generator = "uuid2") private UUID id; private Double latitude; private Double longitud
@Entity
@Spatial(spatialMode = SpatialMode.HASH)
@Indexed
public class A {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
private Double latitude;
private Double longitude;
@Spatial(spatialMode = SpatialMode.HASH)
@SortableField
public Coordinates getLocation() {
return new Coordinates() {
@Override
public Double getLatitude() {
return latitude;
}
@Override
public Double getLongitude() {
return longitude;
}
};
}
@ManyToMany(fetch = FetchType.EAGER)
@IndexedEmbedded
private Set<B> multipleB = new HashSet<>();
}
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(AToB.class).get();
Query luceneQuery = qb.
spatial()
.onField("a.location")
.within(kmRadius, Unit.KM)
.ofLatitude(centerLatitude)
.andLongitude(centerLongitude)
.createQuery();
然而,问题是,当我这样查询时,会得到以下异常:
The field 'B#multipleA.location' used for the spatial query is not configured as spatial field. Check the proper use of @Spatial respectively SpatialFieldBridge.
有没有办法对使用@ManyToMany连接实体a的空间数据的B类实体进行空间查询?
对于我的查询,如果为每个连接的实体A多次返回相同的实体B(就像连接一样),这也是可以接受的
示例(a_1是最近的实体a)
查询结果:
b_1 (a_1 is the closest)
b_2 (a_1)
b_1 (a_2 is the second closest)
b_3 (a_2)
b_3 (a_3)
Hibernate搜索中不支持多值位置;即使您设法为它编制索引,查询也无法正常工作(它将从不同的点混合纬度和经度) 我能看到这种效果的唯一方法是,将多对多关联建模为一个实体,为该实体编制索引,并查询它,而不是
B
另外,请注意,您得到的错误与另一个问题有关:您要定位的字段不存在。如果您希望它存在,则需要在B#multipleA
上添加一个@IndexedEmbedded
以下是一些应该更有效的方法:
@Entity
@Spatial(spatialMode = SpatialMode.HASH)
@Indexed
public class A {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
private Double latitude;
private Double longitude;
@Latitude
public Double getLatitude() {
return latitude;
}
@Longitude
public Double getLongitude() {
return longitude;
}
@OneToMany(mappedBy = "a")
@ContainedIn
private Set<AToB> multipleB = new HashSet<>();
}
@Entity
@Indexed
public class AToB {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
@ManyToOne
@IndexedEmbedded(includePaths = "location") // includePaths will be necessary to avoid infinite recursion if you really want an @IndexedEmbedded from A to B
private A a;
@ManyToOne
@IndexedEmbedded
private B b;
}
@Entity
public class B {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
@OneToMany(mappedBy = "b")
@ContainedIn
private Set<AToB> multipleA = new HashSet<>();
}
Hibernate搜索中不支持多值位置;即使您设法为它编制索引,查询也无法正常工作(它将从不同的点混合纬度和经度) 我能看到这种效果的唯一方法是,将多对多关联建模为一个实体,为该实体编制索引,并查询它,而不是
B
另外,请注意,您得到的错误与另一个问题有关:您要定位的字段不存在。如果您希望它存在,则需要在B#multipleA
上添加一个@IndexedEmbedded
以下是一些应该更有效的方法:
@Entity
@Spatial(spatialMode = SpatialMode.HASH)
@Indexed
public class A {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
private Double latitude;
private Double longitude;
@Latitude
public Double getLatitude() {
return latitude;
}
@Longitude
public Double getLongitude() {
return longitude;
}
@OneToMany(mappedBy = "a")
@ContainedIn
private Set<AToB> multipleB = new HashSet<>();
}
@Entity
@Indexed
public class AToB {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
@ManyToOne
@IndexedEmbedded(includePaths = "location") // includePaths will be necessary to avoid infinite recursion if you really want an @IndexedEmbedded from A to B
private A a;
@ManyToOne
@IndexedEmbedded
private B b;
}
@Entity
public class B {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
@OneToMany(mappedBy = "b")
@ContainedIn
private Set<AToB> multipleA = new HashSet<>();
}
谢谢,你的解决方案解决了我的问题。谢谢,你的解决方案解决了我的问题。
@Entity
@Spatial(spatialMode = SpatialMode.HASH)
@Indexed
public class A {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
private Double latitude;
private Double longitude;
@Latitude
public Double getLatitude() {
return latitude;
}
@Longitude
public Double getLongitude() {
return longitude;
}
@OneToMany(mappedBy = "a")
@ContainedIn
private Set<AToB> multipleB = new HashSet<>();
}
@Entity
@Indexed
public class AToB {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
@ManyToOne
@IndexedEmbedded(includePaths = "location") // includePaths will be necessary to avoid infinite recursion if you really want an @IndexedEmbedded from A to B
private A a;
@ManyToOne
@IndexedEmbedded
private B b;
}
@Entity
public class B {
@Id
@GeneratedValue(generator = "uuid2")
private UUID id;
@OneToMany(mappedBy = "b")
@ContainedIn
private Set<AToB> multipleA = new HashSet<>();
}
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(AToB.class).get();
Query luceneQuery = qb.
spatial()
.onField("a.location")
.within(kmRadius, Unit.KM)
.ofLatitude(centerLatitude)
.andLongitude(centerLongitude)
.createQuery();