Lucene Hibernate索引空间实体 场景

Lucene Hibernate索引空间实体 场景,lucene,jpa-2.0,spatial,hibernate-search,hibernate-spatial,Lucene,Jpa 2.0,Spatial,Hibernate Search,Hibernate Spatial,班级员工,班级办公室,班级办公室员工 班级办公室是一个空间实体,可以按预期搜索并返回结果 Office Employee之间的多个关系映射到类OfficeEmployee 现在我需要在一定范围内对某些人执行搜索。换句话说,我必须检查范围内的办公室以及存在于这些办公室的员工,即搜索OfficeEmployee实体 所有这三个类都编制了索引 公务员 班级办公室 @JsonIgnoreProperties(ignoreUnknown=true) @Spatial(name=“office\u loca

班级员工,班级办公室,班级办公室员工

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

Office Employee之间的多个关系映射到类OfficeEmployee

现在我需要在一定范围内对某些人执行搜索。换句话说,我必须检查范围内的办公室以及存在于这些办公室的员工,即搜索OfficeEmployee实体

所有这三个类都编制了索引

公务员 班级办公室
@JsonIgnoreProperties(ignoreUnknown=true)
@Spatial(name=“office\u location\u poi”,spatialMode=spatialMode.HASH)
@索引
@实体
@可嵌入
公共类Office实现了可序列化的{
//一些属性、getter、setter。。
@包含
@OneToMany(mappedBy=“office”,cascade=CascadeType.ALL)
私人公务员名单;
@纬度
双纬度;
@经度
双经度;
公共坐标getLocation(){
返回新坐标(){
@凌驾
公共双纬度(){
返回纬度;
}
@凌驾
公共双getLongitude(){
返回经度;
}
};
}
@凌驾
公共双纬度(){
返回纬度;
}
@凌驾
公共双getLongitude(){
返回经度;
}
}
查询:
final QueryBuilder builder=fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(OfficeEmployee.class).get();
双中心纬度=searchTerm.lat;
双中心经度=searchTerm.lng;
org.apache.lucene.search.Query luceneQuery=builder.spatial().onField(“office”)。内(searchTerm.distance,Unit.KM)
高度(中心纬度)
.和经度(中心经度)
.createQuery();
org.hibernate.search.jpa.FullTextQuery hibQuery=fullTextEntityManager.createFullTextQuery(luceneQuery,OfficeEmployee.class);
//分类
排序距离排序=新排序(
新距离SortField(中心纬度、中心经度,“办公室位置”);
hibQuery.setSort(距离排序);
hibQuery.setProjection(FullTextQuery.SPATIAL_DISTANCE,FullTextQuery.THIS);
hibQuery.setFirstResult(0);
hibQuery.setMaxResults(20);
//结果
Listresults=hibQuery.getResultList();
问题 现在我想在关系表(OfficeEmployee)上执行搜索

但听起来好像我不能让它工作!我查阅了教程,没有找到这样的例子

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

有人能给我指一下正确的方向吗?

我一开始不理解你的域模型。为什么您需要一个专门的OfficeEmployee实体?为什么不仅仅是员工和办公室?你还说你得到了一个错误/异常?这是什么样子的?谢谢@Hardy,员工可能属于/存在于多个地点,办公室可以有多个员工,换句话说,由于此规范,关系是多个的。作为一种设计模式,我们决定使用单独的TableOfficeEmployee。我收到的异常来自Hibernate,它告诉我嵌入索引的用法不正确。我找不到一个例子来解释如何在这种情况下嵌入和已经索引的实体(空间);这将自动创建所需的中间表,无需将此表映射到实体,除非您的关联本身具有其他属性。
// 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();