use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class TestVariableResolver method testFunctionNamespace1.
@Test
public void testFunctionNamespace1() throws Exception {
VariableResolver resolver = new VariableResolver();
final List<Map<String, String>> l = new ArrayList<>();
Map<String, String> m = new HashMap<>();
m.put("name", "test");
m.put("class", E.class.getName());
l.add(m);
resolver.setEvaluators(new DataImporter().getEvaluators(l));
ContextImpl context = new ContextImpl(null, resolver, null, Context.FULL_DUMP, Collections.EMPTY_MAP, null, null);
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 = resolver.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);
assertEquals("Hello World", resolver.replaceTokens("${dataimporter.functions.test('TEST')}"));
}
use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class ExpressionTest method dateMathTest.
@Test
public void dateMathTest() throws Exception {
String math = (String) getStatResult("dmr", "cme", VAL_TYPE.STRING);
DateMathParser dateMathParser = new DateMathParser();
dateMathParser.setNow(new Date(Instant.parse((String) getStatResult("dmr", "median", VAL_TYPE.DATE)).toEpochMilli()));
String dateMath = (String) getStatResult("dmr", "dmme", VAL_TYPE.DATE);
assertEquals(getRawResponse(), new Date(Instant.parse(dateMath).toEpochMilli()), dateMathParser.parseMath(math));
math = (String) getStatResult("dmr", "cma", VAL_TYPE.STRING);
dateMathParser = new DateMathParser();
dateMathParser.setNow(new Date(Instant.parse((String) getStatResult("dmr", "max", VAL_TYPE.DATE)).toEpochMilli()));
dateMath = (String) getStatResult("dmr", "dmma", VAL_TYPE.DATE);
assertEquals(getRawResponse(), new Date(Instant.parse(dateMath).toEpochMilli()), dateMathParser.parseMath(math));
}
use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class SolrDeletionPolicy method updateCommits.
private void updateCommits(List<? extends IndexCommit> commits) {
synchronized (this) {
long maxCommitAgeTimeStamp = -1L;
IndexCommit newest = commits.get(commits.size() - 1);
log.debug("newest commit generation = " + newest.getGeneration());
int singleSegKept = (newest.getSegmentCount() == 1) ? 1 : 0;
int totalKept = 1;
// work our way from newest to oldest, skipping the first since we always want to keep it.
for (int i = commits.size() - 2; i >= 0; i--) {
IndexCommit commit = commits.get(i);
// delete anything too old, regardless of other policies
try {
if (maxCommitAge != null) {
if (maxCommitAgeTimeStamp == -1) {
DateMathParser dmp = new DateMathParser(DateMathParser.UTC);
maxCommitAgeTimeStamp = dmp.parseMath(maxCommitAge).getTime();
}
if (IndexDeletionPolicyWrapper.getCommitTimestamp(commit) < maxCommitAgeTimeStamp) {
commit.delete();
continue;
}
}
} catch (Exception e) {
log.warn("Exception while checking commit point's age for deletion", e);
}
if (singleSegKept < maxOptimizedCommitsToKeep && commit.getSegmentCount() == 1) {
totalKept++;
singleSegKept++;
continue;
}
if (totalKept < maxCommitsToKeep) {
totalKept++;
continue;
}
commit.delete();
}
}
// end synchronized
}
use of org.apache.solr.util.DateMathParser in project lucene-solr by apache.
the class TestTrie method testTrieDateRangeSearch.
@Test
public void testTrieDateRangeSearch() throws Exception {
for (int i = 0; i < 10; i++) {
assertU(adoc("id", String.valueOf(i), "tdate", "1995-12-31T23:" + (i < 10 ? "0" + i : i) + ":59.999Z"));
}
assertU(commit());
SolrQueryRequest req = req("q", "*:*", "fq", "tdate:[1995-12-31T23:00:59.999Z TO 1995-12-31T23:04:59.999Z]");
assertQ("Range filter must match only 5 documents", req, "//*[@numFound='5']");
// Test open ended range searches
assertQ("Range filter tint:[1995-12-31T23:00:59.999Z to *] must match 10 documents", req("q", "*:*", "fq", "tdate:[1995-12-31T23:00:59.999Z TO *]"), "//*[@numFound='10']");
assertQ("Range filter tint:[* to 1995-12-31T23:09:59.999Z] must match 10 documents", req("q", "*:*", "fq", "tdate:[* TO 1995-12-31T23:09:59.999Z]"), "//*[@numFound='10']");
assertQ("Range filter tint:[* to *] must match 10 documents", req("q", "*:*", "fq", "tdate:[* TO *]"), "//*[@numFound='10']");
// Test date math syntax
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
assertU(delQ("*:*"));
DateMathParser dmp = new DateMathParser(DateMathParser.UTC);
String largestDate = "";
for (int i = 0; i < 10; i++) {
// 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), "tdate", d));
if (i == 9)
largestDate = d;
}
assertU(commit());
assertQ("Range filter must match only 10 documents", req("q", "*:*", "fq", "tdate:[* TO *]"), "//*[@numFound='10']");
req = req("q", "*:*", "fq", "tdate:[NOW/DAY TO NOW/DAY+5DAYS]");
assertQ("Range filter must match only 6 documents", req, "//*[@numFound='6']");
// Test Term Queries
assertU(adoc("id", "11", "tdate", "1995-12-31T23:59:59.999Z"));
assertU(commit());
assertQ("Term query must match only 1 document", req("q", "tdate:1995-12-31T23\\:59\\:59.999Z"), "//*[@numFound='1']");
assertQ("Term query must match only 1 document", req("q", "*:*", "fq", "tdate:1995-12-31T23\\:59\\:59.999Z"), "//*[@numFound='1']");
// Sorting
assertQ("Sort descending does not work correctly on tdate fields", req("q", "*:*", "sort", "tdate desc"), "//*[@numFound='11']", "//date[@name='tdate'][.='" + largestDate + "']");
assertQ("Sort ascending does not work correctly on tdate fields", req("q", "*:*", "sort", "tdate asc"), "//*[@numFound='11']", "//date[@name='tdate'][.='1995-12-31T23:59:59.999Z']");
// Function queries
assertQ("Function queries does not work correctly on tdate fields", req("q", "_val_:\"sum(tdate,1.0)\""), "//*[@numFound='11']", "//date[@name='tdate'][.='" + largestDate + "']");
}
Aggregations