@Override public Query getQuery() { return matchingContextSupport.rangeMatchingContext().from( lower ).to( upper ).createQuery(); } }
@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 ); }
@Override protected Query getLessOrEqualsQuery() { return matchingContextSupport.rangeMatchingContext().below( value ).createQuery(); }
@Override protected Query getGreaterOrEqualsQuery() { return matchingContextSupport.rangeMatchingContext().above( value ).createQuery(); }
@Test @Category(SkipOnElasticsearch.class) // This only works because of a Lucene-specific hack in org.hibernate.search.bridge.util.impl.NumericFieldUtils.createNumericRangeQuery public void testRangeQueryBelowIgnoreFieldBridge() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); calendar.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); calendar.set( 10 + 1800, 2, 12, 0, 0, 0 ); Date to = calendar.getTime(); Query query = monthQb .range() .onField( "estimatedCreation" ) .ignoreFieldBridge() .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .below( DateTools.round( to, DateTools.Resolution.MINUTE ) ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 3 ); }
@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 @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 ); }
public void testQueryingRangeWithAnd() { NumericType type1 = new NumericType(10, 20); NumericType type2 = new NumericType(20, 10); NumericType type3 = new NumericType(10, 10); cache.put(key1, type1); cache.put(key2, type2); cache.put(key3, type3); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class) .get().range().onField("num1").andField("num2").below(20).excludeLimit().createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(3, found.size()); //<------ All entries should be here, because andField is executed as SHOULD; assert found.contains(type1); assert found.contains(type2); assert found.contains(type3); NumericType type4 = new NumericType(11, 10); cache.put("newKey", type4); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(4, found.size()); assert found.contains(type3); assert found.contains(type2); assert found.contains(type1); assert found.contains(type4); //@TODO write here another case with not-matching entries }
public void testBooleanQueriesShouldNot() { loadTestingData(); Query subQuery1 = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get() .keyword().onField("name").boostedTo(0.5f) .matching("Goat").createQuery(); Query subQuery2 = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get() .range().onField("age").boostedTo(2f).below(20).createQuery(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .should(subQuery1).should(subQuery2).createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(3, found.size()); assert found.get(0).equals(person1); assert found.get(1).equals(person2); assert found.get(2).equals(person3); subQuery1 = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get() .keyword().onField("name").boostedTo(3.5f) .matching("Goat").createQuery(); query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .should(subQuery1).should(subQuery2).createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(3, found.size()); assert found.get(0).equals(person2); assert found.get(1).equals(person3); assert found.get(2).equals(person1); }
RangeMatchingContext rangeMatchingContext() { if ( fieldBridge != null ) { return ( (ConnectedRangeMatchingContext) builder.range().onField( propertyName ) ).withFieldBridge( fieldBridge ).ignoreAnalyzer(); } else { return builder.range().onField( propertyName ); } } }
@Override protected Query getStrictlyLessQuery() { return matchingContextSupport.rangeMatchingContext().below( value ).excludeLimit().createQuery(); }
@Override protected Query getStrictlyGreaterQuery() { return matchingContextSupport.rangeMatchingContext().above( value ).excludeLimit().createQuery(); } }
@Test public void testRangeQueryBelow() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); calendar.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); calendar.set( 10 + 1800, 2, 12, 0, 0, 0 ); Date to = calendar.getTime(); Query query = monthQb .range() .onField( "estimatedCreation" ) .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .below( to ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 3 ); query = monthQb.range() .onField( "raindropInMm" ) .below( 0.24d ) .createQuery(); assertTrue( query.getClass().isAssignableFrom( NumericRangeQuery.class ) ); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 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 ); }
@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 @Category(SkipOnElasticsearch.class) // This only works because of a Lucene-specific hack in org.hibernate.search.bridge.util.impl.NumericFieldUtils.createNumericRangeQuery public void testRangeQueryAboveIgnoreFieldBridge() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); calendar.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); calendar.set( 10 + 1900, 2, 12, 0, 0, 0 ); Date to = calendar.getTime(); Query query = monthQb .range() .onField( "estimatedCreation" ) .ignoreFieldBridge() .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .above( DateTools.round( to, DateTools.Resolution.MINUTE ) ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2 ); }
public void testQueryingRangeBelowWithLimit() throws ParseException { loadTestingData(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class) .get().range().onField("age").below(30).createQuery(); CacheQuery<?> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<?> found = cacheQuery.list(); assertEquals(3, found.size()); assert found.contains(person1); assert found.contains(person2); assert found.contains(person3); assert !found.contains(person4) : "This should not contain object person4"; person4 = new Person(); person4.setName("Mighty Goat"); person4.setBlurb("Also eats grass"); person4.setAge(28); cache.put("mighty", person4); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assert found.size() == 4 : "Size of list should be 4"; assert found.contains(person1); assert found.contains(person2); assert found.contains(person3); assert found.contains(person4) : "This should now contain object person4"; }
@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 ); }