我有一个在不同地理位置出售的车辆索引,我希望找到所有不同的制造商,他们在特定地点和特定价格的半径内有可用的汽车数量。
我编写的查询代码似乎对前5次尝试完美无缺,然后结果似乎“坚持”在第5个查询的纬度,经度和半径上。更改price元素会更改结果,但仅限于第5个查询的位置和半径。
我不确定是否必须“刷新”任何内容,否则这是一个配置问题,但查询在5次尝试后停止工作似乎很奇怪。我很困惑,无法在网上找到这个问题,所以任何帮助或见解都会受到高度赞赏。
这是查询...
private List<VehicleManufacturer> getManufacturers( double valuation , double latitude , double longitude, int radius ) throws Exception {
long startTIme = new Date().getTime() ;
List<VehicleManufacturer> returnedManufacturers = new ArrayList<VehicleManufacturer> () ;
FullTextEntityManager manufacturerFullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
QueryBuilder manufacturerBuilder = manufacturerFullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(AvailableVehicle.class).get() ;
// CREATE SPATIAL AND TOP PRICE FULL TEXT QUERY
org.apache.lucene.search.Query manufacturingSpatialQuery = manufacturerBuilder.spatial().within( radius , Unit.KM ).ofLatitude( latitude ).andLongitude( longitude ).createQuery();
org.apache.lucene.search.Query manufacturingValuationRoof = NumericRangeQuery.newDoubleRange("valuation", 0d , valuation , true, true);
org.apache.lucene.search.Query manufacturingValuationAndSpatialQuery = manufacturerBuilder.bool().must(manufacturingValuationRoof).must(manufacturingSpatialQuery).createQuery() ;
FullTextQuery manufacturingFullTextQuery = manufacturerFullTextEntityManager.createFullTextQuery(manufacturingValuationAndSpatialQuery, AvailableVehicle.class);
// FACET REQUEST
FacetingRequest manufacturingFacetingRequest = manufacturerBuilder.facet().name( "manufacturerFaceting" ).onField("manufacturer").discrete().orderedBy( FacetSortOrder.FIELD_VALUE ).includeZeroCounts(false).createFacetingRequest();
FacetManager facetManager = manufacturingFullTextQuery.getFacetManager();
facetManager.enableFaceting(manufacturingFacetingRequest);
List<Facet> facets = facetManager.getFacets( "manufacturerFaceting" );
for (Facet manuacturingFacet : facets) {
VehicleManufacturer manufacturer = new VehicleManufacturer( manuacturingFacet.getValue() , manuacturingFacet.getCount() ) ;
returnedManufacturers.add( manufacturer ) ;
}
System.out.println( "MANUFACTURERS count: " + returnedManufacturers.size() + " time: " + ( new Date().getTime() - startTIme ) + "ms latitude:" + latitude + " longitude: " + longitude + " radius: " + radius ) ;
return returnedManufacturers ;
}
...这里是精简的索引对象类......
@Entity
@Indexed( index="AvailableVehicle" )
@Spatial(spatialMode = SpatialMode.RANGE )
@Table(name = "car_available_vehicle")
public class AvailableVehicle implements Coordinates {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID" )
private int id ;
@OneToOne()
@JoinColumn( name="VEHICLE_ID" )
private Vehicle vehicle ;
@Facet()
@Column(name = "MANUFACTURER" )
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
private String manufacturer ;
@Facet()
@Column(name = "MODEL" )
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
private String model ;
@Column(name = "LONGITUDE" )
private Double longitude ;
@Column(name = "LATITUDE" )
private Double latitude ;
@Column(name = "PRICE" )
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
@NumericField( forField="price" )
private double price ;
@Column(name = "VALUATION" )
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
@NumericField( forField="valuation" )
private double valuation ;
..这里是hibernate maven依赖...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.7.0.Final</version>
</dependency>
感谢您阅读