Example usage for org.apache.lucene.document DateTools round

List of usage examples for org.apache.lucene.document DateTools round

Introduction

In this page you can find the example usage for org.apache.lucene.document DateTools round.

Prototype

@SuppressWarnings("fallthrough")
public static long round(long time, Resolution resolution) 

Source Link

Document

Limit a date's resolution.

Usage

From source file:de.u808.simpleinquest.indexer.impl.IndexUpdater.java

License:Apache License

private boolean isDocumentIndexedAndModified(File file, Term uidTerm) throws IOException {
    TermQuery query = new TermQuery(uidTerm);
    Hits hits = this.indexSearcher.search(query);
    if (hits.length() > 0) {
        Document doc = hits.doc(0);
        Date lastModified = new Date(DateTools.round(file.lastModified(), DateTools.Resolution.MINUTE));
        Date storedDate;//from  w w w. j  a  v  a 2  s.com
        try {
            storedDate = DateTools.stringToDate(doc.getField(Indexer.MODIFIED_FIELD_NAME).stringValue());
            if (lastModified.after(storedDate)) {
                log.info("File changed " + file.getAbsolutePath());
                return true;
            } else {
                log.info("File not changed " + file.getAbsolutePath());
                return false;
            }
        } catch (ParseException e) {
            log.error("Date pare Exception", e);
            //zur sicherheit neu indizieren
            return true;
        }
    } else {
        return false;
    }
}

From source file:org.hibernate.search.backend.elasticsearch.impl.ElasticsearchDateBridge.java

License:LGPL

private String convertToString(Date value) {
    return ElasticsearchDateHelper.dateToString(DateTools.round(value, resolution));
}

From source file:org.hibernate.search.bridge.builtin.NumericEncodingDateBridge.java

License:LGPL

@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
    if (value == null) {
        return;/* w w  w  . j  ava 2  s . com*/
    }

    Date date = (Date) value;
    long numericDate = DateTools.round(date.getTime(), resolution);
    luceneOptions.addNumericFieldToDocument(name, numericDate, document);
}

From source file:org.hibernate.search.jsr352.massindexing.test.common.MessageManager.java

License:LGPL

@SuppressWarnings("unchecked")
public List<Message> findMessagesFor(Date date) {
    FullTextEntityManager ftem = Search.getFullTextEntityManager(em);
    Date dateDay = DateTools.round(date, DateTools.Resolution.DAY);
    NumericRangeQuery<Long> numericRangeQuery = NumericRangeQuery.newLongRange("date", dateDay.getTime(),
            dateDay.getTime(), true, true);
    BooleanQuery booleanQuery = (new BooleanQuery.Builder()).add(numericRangeQuery, BooleanClause.Occur.MUST)
            .build();/*w w w  . j a v  a 2  s  . com*/
    return ftem.createFullTextQuery(booleanQuery, Message.class).getResultList();
}

From source file:org.hibernate.search.test.dsl.DSLTest.java

License:LGPL

@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);// w ww  . j  a v a2  s .  c  o  m
    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);
}

From source file:org.hibernate.search.test.dsl.DSLTest.java

License:LGPL

@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();/*  w w  w.j  a  v a  2  s.com*/

    helper.assertThat(query).from(Month.class).matchesExactlyIds(3);
}

From source file:org.hibernate.search.test.dsl.DSLTest.java

License:LGPL

@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();/*from  w  ww  .jav a2 s  .c  om*/
    helper.assertThat(query).from(Month.class).matchesExactlyIds(2);
}

From source file:org.hibernate.search.test.dsl.NumericEncodingQueriesTest.java

License:LGPL

@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);
}

From source file:org.hibernate.search.test.dsl.NumericEncodingQueriesTest.java

License:LGPL

@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);
}

From source file:org.hibernate.search.test.filter.deprecated.FilterTest.java

License:LGPL

@TestForIssue(jiraKey = "HSEARCH-1513")
@Test/*from   ww  w.j  a v a  2 s  .  co  m*/
@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());
}