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

The following examples show how to use org.apache.lucene.document.DateTools.Resolution. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: StandardQueryConfigHandler.java    License: Apache License 2.0 6 votes vote down vote up
public StandardQueryConfigHandler() {
  // Add listener that will build the FieldConfig.
  addFieldConfigListener(new FieldBoostMapFCListener(this));
  addFieldConfigListener(new FieldDateResolutionFCListener(this));
  addFieldConfigListener(new PointsConfigListener(this));
  
  // Default Values
  set(ConfigurationKeys.ALLOW_LEADING_WILDCARD, false); // default in 2.9
  set(ConfigurationKeys.ANALYZER, null); //default value 2.4
  set(ConfigurationKeys.DEFAULT_OPERATOR, Operator.OR);
  set(ConfigurationKeys.PHRASE_SLOP, 0); //default value 2.4
  set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, false); //default value 2.4
  set(ConfigurationKeys.FIELD_BOOST_MAP, new LinkedHashMap<String, Float>());
  set(ConfigurationKeys.FUZZY_CONFIG, new FuzzyConfig());
  set(ConfigurationKeys.LOCALE, Locale.getDefault());
  set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD, MultiTermQuery.CONSTANT_SCORE_REWRITE);
  set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, new HashMap<CharSequence, DateTools.Resolution>());
  
}
 
Example 2
Source Project: lucene-solr   Source File: FieldDateResolutionFCListener.java    License: 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 3
Source Project: lucene-solr   Source File: TestStandardQP.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setDateResolution(CommonQueryParserConfiguration cqpC,
    CharSequence field, Resolution value) {
  assert (cqpC instanceof StandardQueryParser);
  StandardQueryParser qp = (StandardQueryParser) cqpC;
  qp.getDateResolutionMap().put(field, value);
}
 
Example 4
Source Project: lucene-solr   Source File: TestQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setDateResolution(CommonQueryParserConfiguration cqpC,
    CharSequence field, Resolution value) {
  assert (cqpC instanceof QueryParser);
  QueryParser qp = (QueryParser) cqpC;
  qp.setDateResolution(field.toString(), value);
}
 
Example 5
Source Project: cxf   Source File: LuceneQueryVisitor.java    License: Apache License 2.0 5 votes vote down vote up
private Query createRangeQuery(Class<?> cls, String name, Object value, ConditionType type) {

        boolean minInclusive = type == ConditionType.GREATER_OR_EQUALS || type == ConditionType.EQUALS;
        boolean maxInclusive = type == ConditionType.LESS_OR_EQUALS || type == ConditionType.EQUALS;

        if (String.class.isAssignableFrom(cls) || Number.class.isAssignableFrom(cls)) {
            Query query = null;

            if (Double.class.isAssignableFrom(cls)) {
                query = createDoubleRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Float.class.isAssignableFrom(cls)) {
                query = createFloatRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Long.class.isAssignableFrom(cls)) {
                query = createLongRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else {
                query = createIntRangeQuery(name, value, type, minInclusive, maxInclusive);
            }

            return query;
        } else if (Date.class.isAssignableFrom(cls)) {
            final Date date = getValue(Date.class, getFieldTypeConverter(), value.toString());
            final String luceneDateValue = getString(Date.class, getFieldTypeConverter(), date);

            if (type == ConditionType.LESS_THAN || type == ConditionType.LESS_OR_EQUALS) {
                return TermRangeQuery.newStringRange(name, null, luceneDateValue, minInclusive, maxInclusive);
            }
            return TermRangeQuery.newStringRange(name, luceneDateValue,
                DateTools.dateToString(new Date(), Resolution.MILLISECOND), minInclusive, maxInclusive);
        } else {
            return null;
        }
    }
 
Example 6
Source Project: Lottery   Source File: LuceneContent.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获得Lucene格式的Document
 * 
 * @param c
 *            文章对象
 * @return
 */
public static Document createDocument(Content c) {
	Document doc = new Document();
	doc.add(new Field(ID, c.getId().toString(), Field.Store.YES,
			Field.Index.NOT_ANALYZED));
	doc.add(new Field(SITE_ID, c.getSite().getId().toString(),
			Field.Store.NO, Field.Index.NOT_ANALYZED));
	doc.add(new Field(RELEASE_DATE, DateTools.dateToString(c
			.getReleaseDate(), Resolution.DAY), Field.Store.NO,
			Field.Index.NOT_ANALYZED));
	Channel channel = c.getChannel();
	while (channel != null) {
		doc.add(new Field(CHANNEL_ID_ARRAY, channel.getId().toString(),
				Field.Store.NO, Field.Index.NOT_ANALYZED));
		channel = channel.getParent();
	}
	doc.add(new Field(TITLE, c.getTitle(), Field.Store.NO,
			Field.Index.ANALYZED));
	if (!StringUtils.isBlank(c.getTxt())) {
		doc.add(new Field(CONTENT, c.getTxt(), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	if(c.getAttr()!=null&&StringUtils.isNotBlank(c.getAttr().get("workplace"))){
		doc.add(new Field(WORKPLACE, c.getAttr().get("workplace"), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	if(c.getAttr()!=null&&StringUtils.isNotBlank(c.getAttr().get("category"))){
		doc.add(new Field(CATEGORY, c.getAttr().get("category"), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	return doc;
}
 
Example 7
Source Project: lucene-solr   Source File: TermRangeQueryNodeProcessor.java    License: 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 Project: cxf   Source File: DefaultParamConverterProvider.java    License: 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;
}
 
Example 9
/**
 * 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 10
Source Project: lucene-solr   Source File: StandardQueryParser.java    License: 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 11
Source Project: lucene-solr   Source File: StandardQueryParser.java    License: 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 12
Source Project: lucene-solr   Source File: StandardQueryParser.java    License: 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 13
Source Project: lucene-solr   Source File: StandardQueryParser.java    License: 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);
}