[英]Hibernate-search 5.0 spatial - Not sure it is storing lat/lon in hashes
[英]Hibernate-search 5.0 spatial return no result
我正在創建一個功能,使用戶可以搜索附近的停車場,該停車場在用戶當前位置周圍10公里處。
我正在將Spring Hibernate與MySQL5InnoDBDialect一起使用。 而且我嘗試通過遵循本教程https://docs.jboss.org/hibernate/search/5.0/reference/en-US/html/spatial.html#spatial通過休眠搜索5.0.0.Final庫實現此功能-查詢距離
但是,我觸發查詢后未返回任何結果。 我正在使用休眠核心4.3.7
休眠設置
注意:文件系統提供程序=文件系統,索引庫= lucene /
<!-- Hibernate SessionFactory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan" value="com.jinheng.fyp.bean" />
<!-- Naming strategy -->
<beans:property name="namingStrategy" ref="myNaming" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
<beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
<beans:prop key="hibernate.format_sql">${hibernate.show_sql}</beans:prop>
<beans:prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</beans:prop>
<beans:prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</beans:prop>
<beans:prop key="hibernate.default_batch_fetch_size">15</beans:prop>
<beans:prop key="hibernate.use_sql_comments">true</beans:prop>
<beans:prop key="hibernate.default-lazy">true</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
<beans:prop key="hibernate.connection.CharSet">utf8</beans:prop>
<beans:prop key="hibernate.connection.characterEncoding">utf8</beans:prop>
<beans:prop key="hibernate.connection.useUnicode">true</beans:prop>
<beans:prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</beans:prop>
<beans:prop key="hibernate.hbm2ddl.import_files_sql_extractor">org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor</beans:prop>
<beans:prop key="hibernate.jdbc.use_streams_for_binary">false</beans:prop>
<beans:prop key="hibernate.connection.isolation">4096</beans:prop>
<beans:prop key="hibernate.search.default.directory_provider">${hibernate.search.default.directory_provider}</beans:prop>
<beans:prop key="hibernate.search.default.indexBase">${hibernate.search.default.indexBase}</beans:prop>
<beans:prop key="hibernate.search.lucene_version">4.1.0</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
停車場課
@Entity
@Spatial(spatialMode = SpatialMode.HASH)
@Indexed
public class Lot extends BasicTable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer lotID;
private String lotName;
private String lotType;
@Longitude
private Double longitude;
@Latitude
private Double latitude;
private String address;
private String operationHour;
private Integer capacity;
private String availability;
//getsetmethods
查詢DAO類
@Repository
@Transactional
public class MapDAOImpl extends GenericDAO implements MapDAO {
@SuppressWarnings("unchecked")
@Override
public List<Lot> getNearbyLot(Double latitude, Double longitude, Integer radius) {
FullTextSession fullTextSession = Search.getFullTextSession(getSessionFactory());
QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lot.class).get();
org.apache.lucene.search.Query luceneQuery = builder.spatial().within(radius, Unit.KM).ofLatitude(latitude).andLongitude(longitude).createQuery();
org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, Lot.class);
//this code works
// org.hibernate.Query hibernateQuery = getSessionFactory().createSQLQuery("SELECT * FROM LOT").addEntity(Lot.class);
List<Lot> result = hibernateQuery.list();
return result;
}
}
它埋在文檔中
SpatialContext.onField():基本上,如果您不提供onField(),它將無法正常工作,它不會為您找到要搜索的字段的名稱。 這是一種方法:
org.apache.lucene.search.Query luceneQuery = spatialContext.onField("physical").within(100, Unit.KM).ofLatitude(centerLatitude).andLongitude(centerLongitude).createQuery();
這里的“物理”是添加到@Spatial @Latitude和@Longitude的屬性“名稱”的值(如https://docs.jboss.org/hibernate/search/5.0/reference/en-US/html中所示/spatial.html#spatial-multiplecoordinates )
我的例子:
@javax.persistence.Entity @Indexed @Spatial(name="physical")
public class Tutorial implements IBusiness {
@Latitude(of="physical") @Column
private Double latitude;
@Longitude(of="physical") @Column
private Double longitude;
[... cut ...]
它為我工作,希望對以后的讀者有所幫助。 Lucene是一個很棒的API,Hibernate也是,但是它們太寬泛了……可能是時候重構文檔了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.