use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class ConcatenateExpression method getValue.
@Override
public Comparable getValue() {
DateMathParser parser = new DateMathParser();
parser.setNow((Date) delegates[0].getValue());
try {
for (int count = 1; count < delegates.length; count++) {
Comparable dComp = delegates[count].getValue();
if (dComp == null) {
return null;
}
parser.setNow(parser.parseMath((String) dComp));
}
return parser.getNow();
} catch (ParseException e) {
e.printStackTrace();
return parser.getNow();
}
}
use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class TestTrie method testTrieFacet_PrecisionStep.
@Test
public void testTrieFacet_PrecisionStep() throws Exception {
// Future protect - assert 0<precisionStep<64
checkPrecisionSteps("tint");
checkPrecisionSteps("tfloat");
checkPrecisionSteps("tdouble");
checkPrecisionSteps("tlong");
checkPrecisionSteps("tdate");
// For tdate tests
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
DateMathParser dmp = new DateMathParser(DateMathParser.UTC);
for (int i = 0; i < 10; i++) {
long l = Integer.MAX_VALUE + i * 1L;
// index 10 days starting with today
String d = format.format(i == 0 ? dmp.parseMath("/DAY") : dmp.parseMath("/DAY+" + i + "DAYS"));
assertU(adoc("id", String.valueOf(i), "tint", String.valueOf(i), "tlong", String.valueOf(l), "tfloat", String.valueOf(i * i * 31.11f), "tdouble", String.valueOf(i * 2.33d), "tdate", d));
}
for (int i = 0; i < 5; i++) {
long l = Integer.MAX_VALUE + i * 1L;
String d = format.format(i == 0 ? dmp.parseMath("/DAY") : dmp.parseMath("/DAY+" + i + "DAYS"));
assertU(adoc("id", String.valueOf((i + 1) * 10), "tint", String.valueOf(i), "tlong", String.valueOf(l), "tfloat", String.valueOf(i * i * 31.11f), "tdouble", String.valueOf(i * 2.33d), "tdate", d));
}
assertU(commit());
SolrQueryRequest req = req("q", "*:*", "facet", "true", "rows", "15", "facet.field", "tint", "facet.field", "tlong", "facet.field", "tfloat", "facet.field", "tdouble");
testFacetField(req, "tint", "0", "2");
testFacetField(req, "tint", "5", "1");
testFacetField(req, "tlong", String.valueOf(Integer.MAX_VALUE), "2");
testFacetField(req, "tlong", String.valueOf(Integer.MAX_VALUE + 5L), "1");
testFacetField(req, "tfloat", String.valueOf(31.11f), "2");
testFacetField(req, "tfloat", String.valueOf(5 * 5 * 31.11f), "1");
testFacetField(req, "tdouble", String.valueOf(2.33d), "2");
testFacetField(req, "tdouble", String.valueOf(5 * 2.33d), "1");
}
use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class DateFormatEvaluator method evaluateString.
/**
* NOTE: declared as a method to allow for extensibility
* @lucene.experimental
* @return the result of evaluating a string
*/
protected Date evaluateString(String datemathfmt, Locale locale, TimeZone tz) {
//TODO refactor DateMathParser.parseMath a bit to have a static method for this logic.
if (datemathfmt.startsWith("NOW")) {
datemathfmt = datemathfmt.substring("NOW".length());
}
try {
DateMathParser parser = new DateMathParser(tz);
// thus do *not* use SolrRequestInfo
parser.setNow(new Date());
return parser.parseMath(datemathfmt);
} catch (ParseException e) {
throw wrapAndThrow(SEVERE, e, "Invalid expression for date");
}
}
use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class FileListEntityProcessor method getDate.
/**
* Get the Date object corresponding to the given string.
*
* @param dateStr the date string. It can be a DateMath string or it may have a evaluator function
* @return a Date instance corresponding to the input string
*/
private Date getDate(String dateStr) {
if (dateStr == null)
return null;
Matcher m = PLACE_HOLDER_PATTERN.matcher(dateStr);
if (m.find()) {
Object o = context.resolve(m.group(1));
if (o instanceof Date)
return (Date) o;
dateStr = (String) o;
} else {
dateStr = context.replaceTokens(dateStr);
}
m = Evaluator.IN_SINGLE_QUOTES.matcher(dateStr);
if (m.find()) {
String expr = m.group(1);
//TODO refactor DateMathParser.parseMath a bit to have a static method for this logic.
if (expr.startsWith("NOW")) {
expr = expr.substring("NOW".length());
}
try {
// DWS TODO: is this TimeZone the right default for us? Deserves explanation if so.
return new DateMathParser(TimeZone.getDefault()).parseMath(expr);
} catch (ParseException exp) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Invalid expression for date", exp);
}
}
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT).parse(dateStr);
} catch (ParseException exp) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Invalid expression for date", exp);
}
}
use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class TestVariableResolver method dateNamespaceWithExpr.
@Test
public void dateNamespaceWithExpr() throws Exception {
VariableResolver vri = new VariableResolver();
vri.setEvaluators(new DataImporter().getEvaluators(Collections.<Map<String, String>>emptyList()));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
DateMathParser dmp = new DateMathParser(TimeZone.getDefault());
String s = vri.replaceTokens("${dataimporter.functions.formatDate('NOW/DAY','yyyy-MM-dd HH:mm')}");
assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ROOT).format(dmp.parseMath("/DAY")), s);
}
Aggregations