休眠搜索ManyToMany与空间搜索

时间:2019-01-28 13:37:18

标签: hibernate jpa geospatial hibernate-search

我有一个具有休眠搜索@Spatial数据的实体A,如下所示:

@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<>();
}

实体A与实体B具有ManyToMany关系。 实体A具有对实体B重要的空间信息。现在,我要对实体B执行空间查询,该查询使用实体A的位置信息。

@Entity
@Indexed
public class B {
    @Id
    @GeneratedValue(generator = "uuid2")
    private UUID id;

    @ManyToMany
    @ContainedIn
    private Set<A> multipleA = new HashSet<>();
}

我试图像这样对实体B进行查询:

QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(B.class).get();

    Query luceneQuery = qb.
            spatial()
            .onField("multipleA.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.

是否有一种方法可以对类型B的实体进行空间查询,该查询使用@ManyToMany连接的实体A中的空间数据?

对于我的查询,如果为每个连接的实体A多次返回相同的实体B(如使用联接一样),也是可以接受的。

示例(a_1是最接近的实体A)

Database: b_1 -> (a_1, a_2), b_2 -> (a_1), b_3 -> (a_2, a_3)

查询结果:

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)

1 个答案:

答案 0 :(得分:1)

Hibernate Search不支持多值位置;即使您设法为其编制索引,查询也无法正常工作(它将混合来自不同点的纬度和经度)。

我看到此工作的唯一方法是让您将多对多关联建模为一个实体,对该实体建立索引,然后查询它而不是B

另外,请注意,您收到的错误与另一个问题有关:您定位的字段不存在。如果您希望它存在,则需要在@IndexedEmbedded上添加一个B#multipleA

这里应该可以更好地工作:

@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();