@Test public void testRangeQueryFromTo() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); calendar.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); calendar.set( 1900, 2, 12, 0, 0, 0 ); calendar.set( Calendar.MILLISECOND, 0 ); Date from = calendar.getTime(); calendar.set( 1910, 2, 12, 0, 0, 0 ); Date to = calendar.getTime(); Query query = monthQb .range() .onField( "estimatedCreation" ) .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .from( from ) .to( to ).excludeLimit() .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@Test @Category(SkipOnElasticsearch.class) // This only works because of a Lucene-specific hack in org.hibernate.search.bridge.util.impl.NumericFieldUtils.createNumericRangeQuery public void testRangeQueryFromToIgnoreFieldBridge() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); calendar.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); calendar.set( 1900, 2, 12, 0, 0, 0 ); calendar.set( Calendar.MILLISECOND, 0 ); Date from = calendar.getTime(); calendar.set( 1910, 2, 12, 0, 0, 0 ); Date to = calendar.getTime(); Query query = monthQb .range() .onField( "estimatedCreation" ) .ignoreFieldBridge() .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .from( DateTools.round( from, DateTools.Resolution.MINUTE ) ) .to( DateTools.round( to, DateTools.Resolution.MINUTE ) ) .excludeLimit() .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
.get().range().onField("age").from(20).to(30).createQuery();
.onField( "latitude" ) .from( boundingBox.getLowerLeft().getLatitude() ) .to( boundingBox.getUpperRight().getLatitude() ) .createQuery() .onField( "longitude" ) .from( boundingBox.getLowerLeft().getLongitude() ) .to( boundingBox.getUpperRight().getLongitude() ) .createQuery() .onField( "latitude" ) .from( boundingBox.getLowerLeft().getLatitude() ) .to( boundingBox.getUpperRight().getLatitude() ) .createQuery() .onField( "longitude" ) .from( boundingBox.getLowerLeft().getLongitude() ) .to( boundingBox.getUpperRight().getLongitude() ) .createQuery()
public void testQueryingRange() throws ParseException { loadTestingData(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class) .get().range().onField("age").from(20).excludeLimit().to(30).excludeLimit().createQuery(); CacheQuery<?> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<?> found = cacheQuery.list(); assertEquals(1, found.size()); assert found.contains(person3); assert !found.contains(person4) : "This should not contain object person4"; person4 = new Person(); person4.setName("Mighty Goat"); person4.setBlurb("Mighty Goat also eats grass"); person4.setAge(28); cache.put("mighty", person4); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assert found.size() == 2 : "Size of list should be 3"; assert found.contains(person3); assert found.contains(person4) : "This should now contain object person4"; }
@Override @SuppressWarnings({ "unchecked", "unused" }) protected void execute(FullTextSession fts) { Query q = fts.getSearchFactory() .buildQueryBuilder() .forEntity( Book.class ) .get() .range() .onField( "rating" ) .from( 40.0f ) .to( 60.0f ) .createQuery(); List<Book> result = fts.createFullTextQuery( q, Book.class ) .setSort( new Sort( new SortField( "rating", SortField.Type.FLOAT, true ) ) ) .list(); }
@Test public void numericRangeQueryOnCustomField() { storeData( "title-one", "1" ); storeData( "title-two", "2" ); storeData( "title-three", "3" ); QueryBuilder queryBuilder = helper.queryBuilder( CustomBridgedNumbers.class ); Query query = queryBuilder .range() .onField( "customField" ) .from( 1 ).excludeLimit() .to( 3 ).excludeLimit() .createQuery(); helper.assertThat( query ) .from( CustomBridgedNumbers.class ) .projecting( "title" ) .matchesExactlySingleProjections( "title-two" ); }
@Override public Query getQuery() { return matchingContextSupport.rangeMatchingContext().from( lower ).to( upper ).createQuery(); } }
private Query queryForRangeOnFieldSorted(int min, int max, String fieldName) { ExtendedSearchIntegrator integrator = factoryHolder.getSearchFactory(); QueryBuilder queryBuilder = integrator.buildQueryBuilder().forEntity( Person.class ).get(); return queryBuilder .range() .onField( fieldName ) .from( min ) .to( max ) .createQuery(); }
@Test @TestForIssue(jiraKey = "HSEARCH-1791") public void testUsingRangeQueryOnNumericDocumentIdGeneratesTermRangeQuery() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb.range() .onField( "id" ) .from( 1 ) .to( 3 ) .createQuery(); assertTrue( "A string based TermQuery is expected, but got a " + query.getClass(), query instanceof TermRangeQuery ); }
@Override public Query visit(BetweenExpr betweenExpr) { PropertyValueExpr propertyValueExpr = (PropertyValueExpr) betweenExpr.getLeftChild(); ConstantValueExpr fromValueExpr = (ConstantValueExpr) betweenExpr.getFromChild(); ConstantValueExpr toValueExpr = (ConstantValueExpr) betweenExpr.getToChild(); Comparable fromValue = fromValueExpr.getConstantValueAs(propertyValueExpr.getPrimitiveType(), namedParameters); Comparable toValue = toValueExpr.getConstantValueAs(propertyValueExpr.getPrimitiveType(), namedParameters); return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .from(fromValue).to(toValue).createQuery(); }
@Test public void testNumericRangeQueries() { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .range() .onField( "raindropInMm" ) .from( 0.23d ) .to( 0.24d ) .createQuery(); assertTrue( query.getClass().isAssignableFrom( NumericRangeQuery.class ) ); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1 ); }
@Override public Query visit(BetweenExpr betweenExpr) { PropertyValueExpr propertyValueExpr = (PropertyValueExpr) betweenExpr.getLeftChild(); ConstantValueExpr fromValueExpr = (ConstantValueExpr) betweenExpr.getFromChild(); ConstantValueExpr toValueExpr = (ConstantValueExpr) betweenExpr.getToChild(); Comparable fromValue = fromValueExpr.getConstantValueAs(propertyValueExpr.getPrimitiveType(), namedParameters); Comparable toValue = toValueExpr.getConstantValueAs(propertyValueExpr.getPrimitiveType(), namedParameters); return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .from(fromValue).to(toValue).createQuery(); }
@Test public void verifyExplicitRangeQuery() { Query query = getQueryBuilder() .range() .onField( "age" ) .from( 1 ).excludeLimit() .to( 3 ).excludeLimit() .createQuery(); Assert.assertTrue( query instanceof NumericRangeQuery ); assertProjection( query, "title" ).matchesExactlySingleProjections( "title-two" ); }