【发布时间】:2018-08-21 11:57:44
【问题描述】:
我有组织和坐标类。 坐标如下:
@Entity
@Table(name = "COORDINATES")
@Indexed
@Spatial
public class Coordinates implements Serializable {
private static final long serialVersionUID = 1L;
public Coordinates() {}
@Id
@Column(name = "id", nullable = false)
@SequenceGenerator(name = "generator", sequenceName = "COORDINATES_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
private Long id;
@Field
@Latitude
@Column(name = "LATITUDE", nullable = false)
private Double latitude;
@Field
@Longitude
@Column(name = "LONGITUDE", nullable = false)
private Double longitude;
我将注解@IndexedEmbedded 放在Organization 的坐标字段中(即@Indexed + @Entity + @Table)。 为什么我的查询中无法识别空间特征?我有以下例外:
org.hibernate.search.exception.SearchException:HSEARCH000131: 用于空间的字段“organization.Organization#coordinates” 查询未配置为空间字段。检查正确使用 @Spatial 分别位于 SpatialFieldBridge org.hibernate.search.query.dsl.impl.ConnectedSpatialQueryBuilder.createSpatialQuery(ConnectedSpatialQueryBuilder.java:63) 在 org.hibernate.search.query.dsl.impl.ConnectedSpatialQueryBuilder.createQuery(ConnectedSpatialQueryBuilder.java:38)
编辑 1
我尝试向 Spatial 注释添加名称,但收到错误消息,提示找不到 @Latitude 和 @Longitude。
所以我尝试实现Hibernate的坐标,将@Entity更改为@Embeddable,在Organization类中将@IndexedEmbedded更改为@Spatial
coordinates 属性,在我的 Coordinates 类中删除 @Latitude + @Longitude,覆盖 getLatitude() 和 getLongitude()。现在我有以下异常:
org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne on com.(...).model.organization.Organization.coordinates 引用一个未知实体: com.(...).model.localization.Coordinates
我正在使用:
- 休眠核心:5.2.12.Final
- hibernate-jpamodelgen:5.2.12.Final
- hibernate-search-orm : 5.8.2.Final
- hibernate-search-elasticsearch:5.8.2.Final
我与其他开发人员交谈过,他们似乎需要在较新版本中更改的功能,因此我们暂时不想更改。
这是组织类中有趣的部分:
@Entity
@Table(name = "ORGANIZATION", uniqueConstraints = {@UniqueConstraint(columnNames = { "CODE" }) })
@Indexed
public class Organization implements Serializable, FileEntity {
// lots of attributes, getters, setters, constructor
@Spatial(name = "location", spatialMode = SpatialMode.RANGE)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "COORDINATES_ID")
private Coordinates coordinates;
}
以及修改后的坐标类:
@Embeddable
@Table(name = "COORDINATES")
@Indexed
public class Coordinates implements org.hibernate.search.spatial.Coordinates, Serializable {
// serial, constructor, id get+set
@Field
@Column(name = "LATITUDE", nullable = false)
private Double latitude;
@Field
@Column(name = "LONGITUDE", nullable = false)
private Double longitude;
@Override
public Double getLatitude() {
return this.latitude;
}
@Override
public Double getLongitude() {
return this.longitude;
}
}
编辑 2
我在@Latitude 和@Longitude 中添加了“(of = "location")”。 我仍然有错误:
错误信息: {"root_cause":[{"type":"query_shard_exception","reason":"失败 查找 geo_point 字段 [coordinates.location]" (...)
也许它来自查询? 查询:
final BooleanJunction bool = queryBuilder.bool().must(queryBuilder.bool()
.should(...)
.should(...)
.should(queryBuilder
.spatial()
.onField("coordinates.location")
.within(12, Unit.KM)
.ofLatitude(form.getLatitude())
.andLongitude(form.getLongitude())
.createQuery())
.createQuery())
.must(...)
.must(...)
.must(...);
final FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(bool.createQuery(), Organization.class);
fullTextQuery.setMaxResults(form.getMaximumNumberOfResult());
fullTextQuery.setProjection(FullTextQuery.SPATIAL_DISTANCE, FullTextQuery.THIS);
fullTextQuery.setSpatialParameters(form.getLatitude(), form.getLongitude(), "coordinates.location");
fullTextQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SCORE);
Organization 中的“coordinates”字段是@IndexedEmbedded,Coordinates 类是你写的。
【问题讨论】:
标签: hibernate-search hibernate-spatial