0

情景

班干部、班办、班办职员。

班级办公室是一个空间实体,可以搜索并按预期返回结果。

Office-Employee 之间的多对多关系与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) 上执行我的搜索。

但听起来我无法让它工作!我查了教程,找不到这样的例子。

  • 是否可以像我解释的那样使用当前索引的实体?
  • 我是否必须在 OfficeEmployee 中包含 @Spatial ?但这需要单独建立新的索引,我想使用当前索引的索引。
  • 当我运行搜索时,它说我需要检查@Spatial 和@SpatialFieldBridge,即使我这样注释,结果也是空的。
  • 如果我的空间实体正在实现坐标并且没有单独的坐标字段,@ContainedIn 应该放在哪里?

谁能指出我正确的方向?

4

0 回答 0