final Integer iterations = 2000;
final Integer warmUp = 50;
Random random = new Random( 42 );
for ( int i = 0; i < iterations; i++ ) {
Point center = Point.fromDegrees( random.nextDouble() * 2 + 44 , random.nextDouble() * 2 + 3 );
double radius = 25.0d;
Rectangle boundingBox = Rectangle.fromBoundingCircle( center, radius );
query = queryBuilder.bool()
.must(
queryBuilder.range()
.onField( "latitude" )
.from( boundingBox.getLowerLeft().getLatitude() )
.to( boundingBox.getUpperRight().getLatitude() )
.createQuery()
)
.must(
queryBuilder.range()
.onField( "longitude" )
.from( boundingBox.getLowerLeft().getLongitude() )
.to( boundingBox.getUpperRight().getLongitude() )
.createQuery()
)
.createQuery();
hibQuery = fullTextSession.createFullTextQuery( query, POI.class );
hibQuery.setProjection( "id", "name" );
startTime = System.nanoTime();
try {
doubleRangeDocsFetched += hibQuery.getResultSize();
}
finally {
endTime = System.nanoTime();
}
duration = endTime - startTime;
if ( i > warmUp ) {
doubleRangeTotalDuration += duration;
}
session.clear();
query = queryBuilder.bool()
.must(
queryBuilder.range()
.onField( "latitude" )
.from( boundingBox.getLowerLeft().getLatitude() )
.to( boundingBox.getUpperRight().getLatitude() )
.createQuery()
)
.must(
queryBuilder.range()
.onField( "longitude" )
.from( boundingBox.getLowerLeft().getLongitude() )
.to( boundingBox.getUpperRight().getLongitude() )
.createQuery()
)
.createQuery();
org.apache.lucene.search.Query filteredQuery = new ConstantScoreQuery(
SpatialQueryBuilderFromCoordinates.buildDistanceFilter(
new QueryWrapperFilter( query ),
center,
radius,
"location"
)
);
hibQuery = fullTextSession.createFullTextQuery( filteredQuery, POI.class );
hibQuery.setProjection( "id", "name" );
startTime = System.nanoTime();
try {
distanceDoubleRangeDocsFetched += hibQuery.getResultSize();
}
finally {
endTime = System.nanoTime();
}
duration = endTime - startTime;
if ( i > warmUp ) {
distanceDoubleRangeTotalDuration += duration;
}
rangeResults = hibQuery.list();
session.clear();
luceneQuery = SpatialQueryBuilderFromCoordinates.buildSpatialHashQuery( center, radius, "location" );
hibQuery = fullTextSession.createFullTextQuery( luceneQuery, POI.class );
hibQuery.setProjection( "id", "name" );
startTime = System.nanoTime();
try {
spatialHashDocsFetched += hibQuery.getResultSize();
}
finally {
endTime = System.nanoTime();
}
duration = endTime - startTime;
if ( i > warmUp ) {
spatialHashTotalDuration += duration;
}
session.clear();
luceneQuery = SpatialQueryBuilderFromCoordinates.buildSpatialQueryByHash( center, radius, "location" );
hibQuery = fullTextSession.createFullTextQuery( luceneQuery, POI.class );
hibQuery.setProjection( "id", "name" );
startTime = System.nanoTime();
try {
spatialDocsFetched += hibQuery.getResultSize();
}
finally {
endTime = System.nanoTime();
}
duration = endTime - startTime;
if ( i > warmUp ) {
spatialTotalDuration += duration;
}
spatialHashResults = hibQuery.list();
session.clear();
if ( rangeResults.size() != spatialHashResults.size() ) {
luceneQuery = SpatialQueryBuilderFromCoordinates.buildDistanceQuery( center, radius, "location" );
hibQuery = fullTextSession.createFullTextQuery( luceneQuery, POI.class );
hibQuery.setProjection( "id", "name" );
System.out.println(
">>>>> Different numbers of documents fetched for point (" +
Double.toString( center.getLatitude()) +
"," +
Double.toString( center.getLongitude()) +
") and radius " +
Double.toString( radius )
);
System.out.println( "Range results : " + rangeResults );
System.out.println( "Spatial Hash results : " + spatialHashResults );