Skip to content

Commit

Permalink
Extended symbolic date math logic
Browse files Browse the repository at this point in the history
  • Loading branch information
romanchyla committed Aug 25, 2019
1 parent 9347ad7 commit a4531fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.solr.common.SolrException;
import org.apache.solr.util.DateMathParser;

public final class DateNormalizerTokenFilter extends TokenFilter {
Expand Down Expand Up @@ -54,6 +55,20 @@ private CharSequence normalize(String string) {
normalDate = false;
}

// we allow symbolic date math logic; if parsed
// then the date will always be moved into the first
// 30 mins of the day
try {
Date date = dmp.parseMath(null, string);
dmp.setNow(date);
date = dmp.parseMath("/DAY" + this.offset);
return sdf.format(date);
} catch (SolrException e) {
// pass
} catch (ParseException e) {
// pass
}

for (SimpleDateFormat f: this.format) {
try {
Date date = f.parse(string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void test() throws Exception {
// test the query parser does the right thing
//setDebug(true);


// added symbolic date math parsing
assertQueryEquals(req("defType", "aqp", "q", "date:[\"1976-12-30T00:30:00Z\" TO \"1977-12-30T00:30:00Z\"]"),
"date:[220753800000 TO 252289800000]",
Expand Down Expand Up @@ -297,6 +298,14 @@ public void test() throws Exception {
"//*[@numFound='125']"
);

/*
* symbolic date math
*/

assertQ(req("q", "pubdate:[NOW-60YEARS TO *] foo", "qf", "title keyword"),
"//*[@numFound='1']"
);

}

}

0 comments on commit a4531fc

Please sign in to comment.