Java Code Examples for org.apache.lucene.document.DateTools#dateToString()

The following examples show how to use org.apache.lucene.document.DateTools#dateToString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LuceneMessageSearchIndex.java    From james-project with Apache License 2.0 6 votes vote down vote up
private Query createQuery(String field, DateOperator dop) throws UnsupportedSearchException {
    Date date = dop.getDate();
    DateResolution res = dop.getDateResultion();
    DateTools.Resolution dRes = toResolution(res);
    String value = DateTools.dateToString(date, dRes);
    switch (dop.getType()) {
    case ON:
        return new TermQuery(new Term(field, value));
    case BEFORE: 
        return new TermRangeQuery(field, DateTools.dateToString(MIN_DATE, dRes), value, true, false);
    case AFTER: 
        return new TermRangeQuery(field, value, DateTools.dateToString(MAX_DATE, dRes), false, true);
    default:
        throw new UnsupportedSearchException();
    }
}
 
Example 2
Source File: Server.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param checkinIn the checkin to set
 */
public void setCheckin(Date checkinIn) {
    if (checkinIn != null) {
        this.checkin = DateTools.dateToString(checkinIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.checkin = null;
    }
}
 
Example 3
Source File: Server.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param registeredIn the registered to set
 */
public void setRegistered(Date registeredIn) {
    if (registeredIn != null) {
        this.registered = DateTools.dateToString(registeredIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.registered = null;
    }
}
 
Example 4
Source File: MtasDocumentIndex.java    From inception with Apache License 2.0 5 votes vote down vote up
private void indexDocument(String aDocumentTitle, long aSourceDocumentId,
        long aAnnotationDocumentId, String aUser, byte[] aBinaryCas)
    throws IOException
{
    // Calculate timestamp that will be indexed
    String timestamp = DateTools.dateToString(new Date(), DateTools.Resolution.MILLISECOND);
    
    log.trace(
            "Indexing document in project [{}]({}). sourceId: {}, annotationId: {}, "
            + "user: {} timestamp: {}",
            project.getName(), project.getId(), aSourceDocumentId, aAnnotationDocumentId,
            aUser, timestamp);
    
    IndexWriter indexWriter = getIndexWriter();
    
    // Prepare bytearray with document content to be indexed
    String encodedCAS = new String(MtasUtils.bytesToChars(aBinaryCas));

    // Create new Lucene document
    Document doc = new Document();
    
    // Add indexed fields
    doc.add(new StringField(FIELD_ID, String.valueOf(aSourceDocumentId) + "/"
            + String.valueOf(aAnnotationDocumentId), Field.Store.YES));
    doc.add(new StringField(FIELD_SOURCE_DOCUMENT_ID, String.valueOf(aSourceDocumentId),
            Field.Store.YES));
    doc.add(new StringField(FIELD_ANNOTATION_DOCUMENT_ID,
            String.valueOf(aAnnotationDocumentId), Field.Store.YES));
    doc.add(new StringField(FIELD_TITLE, aDocumentTitle, Field.Store.YES));
    doc.add(new StringField(FIELD_USER, aUser, Field.Store.YES));
    doc.add(new StringField(FIELD_TIMESTAMP, timestamp, Field.Store.YES));
    doc.add(new TextField(FIELD_CONTENT, encodedCAS, Field.Store.NO));

    // Add document to the Lucene index
    indexWriter.addDocument(doc);

    scheduleCommit();
}
 
Example 5
Source File: Server.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param checkinIn the checkin to set
 */
public void setCheckin(Date checkinIn) {
    if (checkinIn != null) {
        this.checkin = DateTools.dateToString(checkinIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.checkin = null;
    }
}
 
Example 6
Source File: Server.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param registeredIn the registered to set
 */
public void setRegistered(Date registeredIn) {
    if (registeredIn != null) {
        this.registered = DateTools.dateToString(registeredIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.registered = null;
    }
}
 
Example 7
Source File: TermRangeQueryNodeProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
  
  if (node instanceof TermRangeQueryNode) {
    TermRangeQueryNode termRangeNode = (TermRangeQueryNode) node;
    FieldQueryNode upper = termRangeNode.getUpperBound();
    FieldQueryNode lower = termRangeNode.getLowerBound();
    
    DateTools.Resolution dateRes = null;
    boolean inclusive = false;
    Locale locale = getQueryConfigHandler().get(ConfigurationKeys.LOCALE);
    
    if (locale == null) {
      locale = Locale.getDefault();
    }
    
    TimeZone timeZone = getQueryConfigHandler().get(ConfigurationKeys.TIMEZONE);
    
    if (timeZone == null) {
      timeZone = TimeZone.getDefault();
    }
    
    CharSequence field = termRangeNode.getField();
    String fieldStr = null;
    
    if (field != null) {
      fieldStr = field.toString();
    }
    
    FieldConfig fieldConfig = getQueryConfigHandler()
        .getFieldConfig(fieldStr);
    
    if (fieldConfig != null) {
      dateRes = fieldConfig.get(ConfigurationKeys.DATE_RESOLUTION);
    }
    
    if (termRangeNode.isUpperInclusive()) {
      inclusive = true;
    }
    
    String part1 = lower.getTextAsString();
    String part2 = upper.getTextAsString();
    
    try {
      DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
      df.setLenient(true);
      
      if (part1.length() > 0) {
        Date d1 = df.parse(part1);
        part1 = DateTools.dateToString(d1, dateRes);
        lower.setText(part1);
      }
      
      if (part2.length() > 0) {
        Date d2 = df.parse(part2);
        if (inclusive) {
          // The user can only specify the date, not the time, so make sure
          // the time is set to the latest possible time of that date to
          // really
          // include all documents:
          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);
          cal.set(Calendar.MILLISECOND, 999);
          d2 = cal.getTime();
        }
        
        part2 = DateTools.dateToString(d2, dateRes);
        upper.setText(part2);
        
      }
      
    } catch (Exception e) {
      // not a date
      Analyzer analyzer = getQueryConfigHandler().get(ConfigurationKeys.ANALYZER);
      if (analyzer != null) {
        // because we call utf8ToString, this will only work with the default TermToBytesRefAttribute
        part1 = analyzer.normalize(lower.getFieldAsString(), part1).utf8ToString();
        part2 = analyzer.normalize(lower.getFieldAsString(), part2).utf8ToString();
        lower.setText(part1);
        upper.setText(part2);
      }
    }
    
  }
  
  return node;
  
}
 
Example 8
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public String getDate(String s) throws Exception {
  // we use the default Locale since LuceneTestCase randomizes it
  DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  return DateTools.dateToString(df.parse(s), DateTools.Resolution.DAY);
}
 
Example 9
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** for testing DateTools support */
private String getDate(Date d, DateTools.Resolution resolution) {
  return DateTools.dateToString(d, resolution);
}
 
Example 10
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** for testing DateTools support */
private String getDate(Date d, DateTools.Resolution resolution) {
  return DateTools.dateToString(d, resolution);
}
 
Example 11
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** for testing DateTools support */
private String getDate(Date d, DateTools.Resolution resolution) {
   return DateTools.dateToString(d, resolution);
}
 
Example 12
Source File: DefaultParamConverterProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public String toString(final Date value) {
    return value != null ? DateTools.dateToString(value, Resolution.MILLISECOND) : null;
}