Java Code Examples for org.apache.lucene.document.DateTools#Resolution

The following examples show how to use org.apache.lucene.document.DateTools#Resolution . 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: LuceneMessageSearchIndex.java    From james-project with Apache License 2.0 6 votes vote down vote up
private DateTools.Resolution toResolution(DateResolution res) {
    switch (res) {
    case Year:
        return DateTools.Resolution.YEAR;
    case Month:
        return DateTools.Resolution.MONTH;
    case Day:
        return DateTools.Resolution.DAY;
    case Hour:
        return DateTools.Resolution.HOUR;
    case Minute:
        return DateTools.Resolution.MINUTE;
    case Second:
        return DateTools.Resolution.SECOND;
    default:
        return DateTools.Resolution.MILLISECOND;
    }
}
 
Example 3
Source File: QueryParserBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the date resolution that is used by RangeQueries for the given field.
 * Returns null, if no default or field specific date resolution has been set
 * for the given field.
 *
 */
public DateTools.Resolution getDateResolution(String fieldName) {
  if (fieldName == null) {
    throw new IllegalArgumentException("Field must not be null.");
  }

  if (fieldToDateResolution == null) {
    // no field specific date resolutions set; return default date resolution instead
    return this.dateResolution;
  }

  DateTools.Resolution resolution = fieldToDateResolution.get(fieldName);
  if (resolution == null) {
    // no date resolutions set for the given field; return default date resolution instead
    resolution = this.dateResolution;
  }

  return resolution;
}
 
Example 4
Source File: FieldDateResolutionFCListener.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void buildFieldConfig(FieldConfig fieldConfig) {
  DateTools.Resolution dateRes = null;
  Map<CharSequence, DateTools.Resolution> dateResMap = this.config.get(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP);

  if (dateResMap != null) {
    dateRes = dateResMap.get(
        fieldConfig.getField());
  }

  if (dateRes == null) {
    dateRes = this.config.get(ConfigurationKeys.DATE_RESOLUTION);
  }

  if (dateRes != null) {
    fieldConfig.set(ConfigurationKeys.DATE_RESOLUTION, dateRes);
  }

}
 
Example 5
Source File: QueryParserBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the date resolution used by RangeQueries for a specific field.
 *
 * @param fieldName field for which the date resolution is to be set
 * @param dateResolution date resolution to set
 */
public void setDateResolution(String fieldName, DateTools.Resolution dateResolution) {
  if (fieldName == null) {
    throw new IllegalArgumentException("Field must not be null.");
  }

  if (fieldToDateResolution == null) {
    // lazily initialize HashMap
    fieldToDateResolution = new HashMap<>();
  }

  fieldToDateResolution.put(fieldName, dateResolution);
}
 
Example 6
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1, false);
  String endDate = getLocalizedDate(2002, 1, 4, false);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  PrecedenceQueryParser qp = new PrecedenceQueryParser(new MockAnalyzer(random()));

  Map<CharSequence, DateTools.Resolution> fieldMap = new HashMap<>();
  // set a field specific date resolution
  fieldMap.put(monthField, DateTools.Resolution.MONTH);
  qp.setDateResolutionMap(fieldMap);

  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);

  // set second field specific date resolution
  fieldMap.put(hourField, DateTools.Resolution.HOUR);
  qp.setDateResolutionMap(fieldMap);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.HOUR);
}
 
Example 7
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertDateRangeQueryEquals(PrecedenceQueryParser qp, String field,
    String startDate, String endDate, Date endDateInclusive,
    DateTools.Resolution resolution) throws Exception {
  assertQueryEquals(qp, field, field + ":[" + escapeDateString(startDate)
      + " TO " + escapeDateString(endDate) + "]", "["
      + getDate(startDate, resolution) + " TO "
      + getDate(endDateInclusive, resolution) + "]");
  assertQueryEquals(qp, field, field + ":{" + escapeDateString(startDate)
      + " TO " + escapeDateString(endDate) + "}", "{"
      + getDate(startDate, resolution) + " TO "
      + getDate(endDate, resolution) + "}");
}
 
Example 8
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1);
  String endDate = getLocalizedDate(2002, 1, 4);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.clear();
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  StandardQueryParser qp = new StandardQueryParser();

  Map<CharSequence, DateTools.Resolution> dateRes =  new HashMap<>();
  
  // set a field specific date resolution    
  dateRes.put(monthField, DateTools.Resolution.MONTH);
  qp.setDateResolutionMap(dateRes);

  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);

  // set second field specific date resolution
  dateRes.put(hourField, DateTools.Resolution.HOUR);
  qp.setDateResolutionMap(dateRes);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two
  // fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.HOUR);
}
 
Example 9
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 10
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(String s, DateTools.Resolution resolution) throws Exception {
  // we use the default Locale since LuceneTestCase randomizes it
  DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  return getDate(df.parse(s), resolution);
}
 
Example 11
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 12
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 13
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(String s, DateTools.Resolution resolution) throws Exception {
  // we use the default Locale since LuceneTestCase randomizes it
  DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  return getDate(df.parse(s), resolution);      
}
 
Example 14
Source File: QueryParserConfig.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public Builder dateResolution(DateTools.Resolution value) {
  dateResolution = value;
  return this;
}
 
Example 15
Source File: StandardQueryParser.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the default {@link Resolution} used for certain field when
 * no {@link Resolution} is defined for this field.
 * 
 * @param dateResolution the default {@link Resolution}
 */
@Override
public void setDateResolution(DateTools.Resolution dateResolution) {
  getQueryConfigHandler().set(ConfigurationKeys.DATE_RESOLUTION, dateResolution);
}
 
Example 16
Source File: StandardQueryParser.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the default {@link Resolution} used for certain field when
 * no {@link Resolution} is defined for this field.
 * 
 * @return the default {@link Resolution}
 */
public DateTools.Resolution getDateResolution() {
  return getQueryConfigHandler().get(ConfigurationKeys.DATE_RESOLUTION);
}
 
Example 17
Source File: StandardQueryParser.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the field to {@link Resolution} map used to normalize each date field.
 * 
 * @return the field to {@link Resolution} map
 */
public Map<CharSequence, DateTools.Resolution> getDateResolutionMap() {
  return getQueryConfigHandler().get(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP);
}
 
Example 18
Source File: StandardQueryParser.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the {@link Resolution} used for each field
 * 
 * @param dateRes a collection that maps a field to its {@link Resolution}
 */
public void setDateResolutionMap(Map<CharSequence, DateTools.Resolution> dateRes) {
  getQueryConfigHandler().set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, dateRes);
}
 
Example 19
Source File: CommonQueryParserConfiguration.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the default {@link Resolution} used for certain field when
 * no {@link Resolution} is defined for this field.
 * 
 * @param dateResolution the default {@link Resolution}
 */
public void setDateResolution(DateTools.Resolution dateResolution);
 
Example 20
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 votes vote down vote up
public abstract void setDateResolution(CommonQueryParserConfiguration cqpC, CharSequence field, DateTools.Resolution value);