Record lastRecord = ...; QueryBuilder queryBuilder = fullTextSession .getSearchFactory() .buildQueryBuilder() .forEntity(Record.class).get(); Query query = queryBuilder.range() .onField( "created" ) .above( lastRecord.getCreated() ) .createQuery(); FullTextQuery fullTextQuery = fulllTextSession.createFullTextQuery( query, Record.class );
//look for users whos id is 1 and createDate is more than lastMonth Date lastMonth = ...; QueryBuilder userQB = searchFactory.buildQueryBuilder().forEntity( User.class ).get(); Query luceneQuery = userQB .bool() .must( userQB.keyword().onField("id").matching("1").createQuery() ) .must( userQB.range().onField("creationDate").above(lastMonth) .createQuery() ) .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 ); }
@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 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2070") public void testSearchDateWithoutFieldBridge() throws Exception { QueryBuilder qb = helper.queryBuilder( ContainerEntity.class ); Query q = qb.range().onField( "emb.date" ) .above( initCalendar( 2007, Calendar.JANUARY, 14 ).getTime() ) .createQuery(); assertQuery( q ).matchesExactlyIds( 1L ); }
@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(); }
@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(); }
private List<Object> getYoungerThan(SearchManager sm, int age) { QueryBuilder queryBuilder = sm.buildQueryBuilderForClass(Person.class).get(); Query query = queryBuilder.range().onField("age").below(age).createQuery(); return sm.getQuery(query, Person.class).list(); }
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(); }
@SuppressWarnings("unchecked") private List<ArrayBridgeTestEntity> findResultsWithRangeQuery(String fieldName, Object start) { QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder() .forEntity( ArrayBridgeTestEntity.class ).get(); Query query = queryBuilder.range().onField( fieldName ).above( start ).createQuery(); return fullTextSession.createFullTextQuery( query, ArrayBridgeTestEntity.class ).list(); }
@SuppressWarnings("unchecked") private List<IterableBridgeTestEntity> findResultsWithRangeQuery(String fieldName, Object start) { QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder() .forEntity( IterableBridgeTestEntity.class ).get(); Query query = queryBuilder.range().onField( fieldName ).above( start ).createQuery(); return fullTextSession.createFullTextQuery( query, IterableBridgeTestEntity.class ).list(); }
@SuppressWarnings("unchecked") private List<MapBridgeTestEntity> findResultsWithRangeQuery(String fieldName, Object start) { QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder() .forEntity( MapBridgeTestEntity.class ).get(); Query query = queryBuilder.range().onField( fieldName ).above( start ).createQuery(); return fullTextSession.createFullTextQuery( query, MapBridgeTestEntity.class ).list(); }
@Test public void testRangeQueryAboveInclusive() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); // test the limits, inclusive Query query = monthQb .range() .onField( "estimatedCreation" ) .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .above( february ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2 ); }
@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" ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2656") public void testNumericRangeQueryWithFieldTypeOverriddenByFieldBridge() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .range() .onField( "monthBase0" ) .ignoreFieldBridge().ignoreAnalyzer() .below( 1 ).excludeLimit() .createQuery(); helper.assertThat( query ).from( Month.class ).matchesUnorderedIds( 1 ); }
@Test public void testRangeQueryAboveExclusive() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); // test the limits, exclusive Query query = monthQb .range() .onField( "estimatedCreation" ) .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .above( february ).excludeLimit() .createQuery(); helper.assertThat( query ).from( Month.class ).matchesNone(); }