Refine search
/** * Converts a Date to a string suitable for indexing. * * @param date the date to be converted * @param resolution the desired resolution, see * {@link #round(Date, DateTools.Resolution)} * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter, * depending on <code>resolution</code>; using GMT as timezone */ public static String dateToString(Date date, Resolution resolution) { return timeToString(date.getTime(), resolution); }
private void addTextField(Document document, String name, Date value) { if (value != null) { addTextField(document, name, DateTools.dateToString(value, DATE_TIME_RES)); } }
/** * Converts a string produced by <code>timeToString</code> or * <code>dateToString</code> back to a time, represented as the * number of milliseconds since January 1, 1970, 00:00:00 GMT. * * @param dateString the date string to be converted * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT * @throws ParseException if <code>dateString</code> is not in the * expected format */ public static long stringToTime(String dateString) throws ParseException { return stringToDate(dateString).getTime(); }
part1 = DateTools.dateToString(df.parse(part1), resolution); } catch (Exception e) { } Calendar cal = Calendar.getInstance(timeZone, locale); cal.setTime(d2); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); d2 = cal.getTime(); part2 = DateTools.dateToString(d2, resolution); } catch (Exception e) { }
@Test @Category(SkipOnElasticsearch.class) // Elasticsearch uses a specific encoding for dates public void testDateBridgeStringEncoding() throws Exception { Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "Europe/Rome" ), Locale.ROOT ); //for the sake of tests c.set( 2000, Calendar.DECEMBER, 15, 3, 43, 2 ); c.set( Calendar.MILLISECOND, 5 ); Date date = new Date( c.getTimeInMillis() ); Cloud cloud = new Cloud(); cloud.setDateDay( date ); cloud.setChar2( 's' ); // Avoid errors with PostgreSQL ("invalid byte sequence for encoding "UTF8": 0x00") org.hibernate.Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist( cloud ); s.flush(); tx.commit(); tx = s.beginTransaction(); FullTextSession session = Search.getFullTextSession( s ); TermQuery termQuery = new TermQuery( new Term( "dateDayStringEncoding", DateTools.dateToString( date, DateTools.Resolution.DAY ) ) ); List result = session.createFullTextQuery( termQuery ).list(); assertEquals( "Date not found or not properly truncated", 1, result.size() ); tx.commit(); s.close(); }
@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 ); }
Calendar calendar = Calendar.getInstance(); calendar.setTime(DateTools.stringToDate(value)); return calendar; } else if (bridge instanceof StringEncodingDateBridge || bridge instanceof NumericEncodingDateBridge || bridge instanceof ElasticsearchDateBridge) { return DateTools.stringToDate(value); } else { return value;
@TestForIssue(jiraKey = "HSEARCH-1513") @Test @Category(SkipOnElasticsearch.class) // The Elasticsearch backend does not support custom Lucene filters. public void testCachedEmptyFilters() { FullTextQuery ftQuery = fullTextSession.createFullTextQuery( query, Driver.class ); ftQuery.enableFullTextFilter( "bestDriver" ); Calendar calendar = GregorianCalendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.ROOT ); calendar.set( Calendar.YEAR, 2001 ); long from = DateTools.round( calendar.getTime().getTime(), DateTools.Resolution.YEAR ); calendar.set( Calendar.YEAR, 2005 ); long to = DateTools.round( calendar.getTime().getTime(), DateTools.Resolution.YEAR ); Filter dateFilter = NumericRangeFilter.newLongRange( "delivery", from, to, true, true ); ftQuery.setFilter( dateFilter ); assertEquals( "Should select only liz", 1, ftQuery.getResultSize() ); ftQuery = fullTextSession.createFullTextQuery( query, Driver.class ); ftQuery.enableFullTextFilter( "bestDriver" ); ftQuery.enableFullTextFilter( "cached_empty" ); assertEquals( "two filters, one is empty, should not match anything", 0, ftQuery.getResultSize() ); }
/** * Limit a calendar resolution. * * @param calendar The calendar whose resolution is to be limited * @param resolution The desired resolution of the date to be returned * @return the calendar with all values more precise than {@code resolution} set to 0 or 1 * * @see DateTools#round(Date, Resolution) */ public static Calendar round(Calendar calendar, Resolution resolution) { final Calendar calInstance = (Calendar) calendar.clone(); /* * Make sure we keep the timezone: use a cloned version of the calendar * to set the rounded time on it, not a new calendar instance. */ calInstance.setTime( DateTools.round( calInstance.getTime(), resolution ) ); return calInstance; }
@Test public void testDslWithCalendar() throws Exception { Calendar year = createCalendar(); year.setTime( DateTools.round( FIRST_EDITION.getTime(), DateTools.Resolution.YEAR ) ); Query query = queryBuilder().keyword().onField( "since" ).matching( year ).createQuery(); helper.assertThat( query ).from( Fair.class ).matchesExactlyIds( LUCCA_ID ); }
/** * Adds the calendar value to the document as the named field. The calendar * value is converted to an indexable string value using the * {@link DateTools} class. * * @param doc * The document to which to add the field * @param fieldName * The name of the field to add * @param value * The value for the field to add to the document. */ private void addCalendarValue(final Document doc, final String fieldName, final Calendar value) { doc.add(createFieldWithoutNorms(fieldName, DateTools.dateToString(value.getTime(), DateTools.Resolution.MILLISECOND), false)); }
@Test public void testDslWithDate() throws Exception { Date nextEventDate = DateTools.round( NEXT_EVENT.getTime(), DateTools.Resolution.DAY ); Query query = queryBuilder().keyword().onField( "startDate" ).matching( nextEventDate ).createQuery(); helper.assertThat( query ).from( Fair.class ).matchesExactlyIds( LUCCA_ID ); }
address.setStreet2( "Peachtnot Rd NE" ); Calendar c = GregorianCalendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.ROOT ); //for the sake of tests c.set( 2009, Calendar.NOVEMBER, 15 ); Date date = new Date( c.getTimeInMillis() ); address.setDateCreated( date ); s.persist( address ); long searchTimeStamp = DateTools.round( date.getTime(), DateTools.Resolution.DAY ); BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); booleanQueryBuilder.add(
/** * Limit a date's resolution. For example, the date <code>2004-09-21 13:50:11</code> * will be changed to <code>2004-09-01 00:00:00</code> when using * <code>Resolution.MONTH</code>. * * @param resolution The desired resolution of the date to be returned * @return the date with all values more precise than <code>resolution</code> * set to 0 or 1 */ public static Date round(Date date, Resolution resolution) { return new Date(round(date.getTime(), resolution)); }
/** * @see org.opencms.search.I_CmsSearchDocument#getFieldValueAsDate(java.lang.String) */ public Date getFieldValueAsDate(String fieldName) { String contentDate = getFieldValueAsString(fieldName); if (contentDate != null) { try { return new Date(DateTools.stringToTime(contentDate)); } catch (ParseException e) { // ignore and assume the given field name does not refer a date field } } return null; }
part1 = DateTools.dateToString(df.parse(part1), resolution); } catch (Exception e) { } Calendar cal = Calendar.getInstance(timeZone, locale); cal.setTime(d2); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); d2 = cal.getTime(); part2 = DateTools.dateToString(d2, resolution); } catch (Exception e) { }
@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 ); }
Calendar calendar = Calendar.getInstance(); calendar.setTime(DateTools.stringToDate(value)); return calendar; } else if (bridge instanceof StringEncodingDateBridge || bridge instanceof NumericEncodingDateBridge || bridge instanceof ElasticsearchDateBridge) { return DateTools.stringToDate(value); } else { return value;
@TestForIssue(jiraKey = "HSEARCH-1513") @Test @Category(SkipOnElasticsearch.class) // The Elasticsearch backend does not support custom Lucene filters. public void testCachedEmptyFilters() { FullTextQuery ftQuery = fullTextSession.createFullTextQuery( query, Driver.class ); ftQuery.enableFullTextFilter( "bestDriver" ); Calendar calendar = GregorianCalendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.ROOT ); calendar.set( Calendar.YEAR, 2001 ); long from = DateTools.round( calendar.getTime().getTime(), DateTools.Resolution.YEAR ); calendar.set( Calendar.YEAR, 2005 ); long to = DateTools.round( calendar.getTime().getTime(), DateTools.Resolution.YEAR ); Filter dateFilter = NumericRangeFilter.newLongRange( "delivery", from, to, true, true ); ftQuery.setFilter( dateFilter ); assertEquals( "Should select only liz", 1, ftQuery.getResultSize() ); ftQuery = fullTextSession.createFullTextQuery( query, Driver.class ); ftQuery.enableFullTextFilter( "bestDriver" ); ftQuery.enableFullTextFilter( "cached_empty" ); assertEquals( "two filters, one is empty, should not match anything", 0, ftQuery.getResultSize() ); }
/** * Limit a calendar resolution. * * @param calendar The calendar whose resolution is to be limited * @param resolution The desired resolution of the date to be returned * @return the calendar with all values more precise than {@code resolution} set to 0 or 1 * * @see DateTools#round(Date, Resolution) */ public static Calendar round(Calendar calendar, Resolution resolution) { final Calendar calInstance = (Calendar) calendar.clone(); /* * Make sure we keep the timezone: use a cloned version of the calendar * to set the rounded time on it, not a new calendar instance. */ calInstance.setTime( DateTools.round( calInstance.getTime(), resolution ) ); return calInstance; }