Examples of DateMathParser


Examples of org.apache.solr.util.DateMathParser

  protected DateField f = null;
  protected DateMathParser p = null;
 
  public void setUp() throws Exception {
    super.setUp();
    p = new DateMathParser(UTC, Locale.US);
    f = new DateField();
    // so test can be run against Solr 1.2...
    try {
      Class clazz = Class.forName("org.apache.solr.schema.LegacyDateField");
      f = (DateField) clazz.newInstance();
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

          (SolrException.ErrorCode.BAD_REQUEST,
           "date facet 'end' comes before 'start': "+endS+" < "+startS);
      }

      final String gap = required.getFieldParam(f,FacetParams.FACET_DATE_GAP);
      final DateMathParser dmp = new DateMathParser(ft.UTC, Locale.US);
      dmp.setNow(NOW);
     
      try {
       
        Date low = start;
        while (low.before(end)) {
          dmp.setNow(low);
          final String lowI = ft.toInternal(low);
          final String label = ft.indexedToReadable(lowI);
          Date high = dmp.parseMath(gap);
          if (end.before(high)) {
            if (params.getFieldBool(f,FacetParams.FACET_DATE_HARD_END,false)) {
              high = end;
            } else {
              end = high;
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   
  }
 
  public void testParseStatelessness() throws Exception {

    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));

    String e = fmt.format(p.parseMath(""));
   
    Date trash = p.parseMath("+7YEARS");
    trash = p.parseMath("/MONTH");
    trash = p.parseMath("-5DAYS+20MINUTES");
    Thread.currentThread().sleep(5);
   
    String a = fmt.format(p.parseMath(""));
    assertEquals("State of DateMathParser changed", e, a);
  }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

    assertEquals("State of DateMathParser changed", e, a);
  }
   
  public void testParseMath() throws Exception {

    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));

    // No-Op
    assertMath("2001-07-04T12:08:56.235", p, "");
   
    // simple round
    assertMath("2001-07-04T12:08:56.000", p, "/SECOND");
    assertMath("2001-07-04T12:08:00.000", p, "/MINUTE");
    assertMath("2001-07-04T12:00:00.000", p, "/HOUR");
    assertMath("2001-07-04T00:00:00.000", p, "/DAY");
    assertMath("2001-07-01T00:00:00.000", p, "/MONTH");
    assertMath("2001-01-01T00:00:00.000", p, "/YEAR");

    // simple addition
    assertMath("2001-07-04T12:08:56.236", p, "+1MILLISECOND");
    assertMath("2001-07-04T12:08:57.235", p, "+1SECOND");
    assertMath("2001-07-04T12:09:56.235", p, "+1MINUTE");
    assertMath("2001-07-04T13:08:56.235", p, "+1HOUR");
    assertMath("2001-07-05T12:08:56.235", p, "+1DAY");
    assertMath("2001-08-04T12:08:56.235", p, "+1MONTH");
    assertMath("2002-07-04T12:08:56.235", p, "+1YEAR");

    // simple subtraction
    assertMath("2001-07-04T12:08:56.234", p, "-1MILLISECOND");
    assertMath("2001-07-04T12:08:55.235", p, "-1SECOND");
    assertMath("2001-07-04T12:07:56.235", p, "-1MINUTE");
    assertMath("2001-07-04T11:08:56.235", p, "-1HOUR");
    assertMath("2001-07-03T12:08:56.235", p, "-1DAY");
    assertMath("2001-06-04T12:08:56.235", p, "-1MONTH");
    assertMath("2000-07-04T12:08:56.235", p, "-1YEAR");

    // simple '+/-'
    assertMath("2001-07-04T12:08:56.235", p, "+1MILLISECOND-1MILLISECOND");
    assertMath("2001-07-04T12:08:56.235", p, "+1SECOND-1SECOND");
    assertMath("2001-07-04T12:08:56.235", p, "+1MINUTE-1MINUTE");
    assertMath("2001-07-04T12:08:56.235", p, "+1HOUR-1HOUR");
    assertMath("2001-07-04T12:08:56.235", p, "+1DAY-1DAY");
    assertMath("2001-07-04T12:08:56.235", p, "+1MONTH-1MONTH");
    assertMath("2001-07-04T12:08:56.235", p, "+1YEAR-1YEAR");

    // simple '-/+'
    assertMath("2001-07-04T12:08:56.235", p, "-1MILLISECOND+1MILLISECOND");
    assertMath("2001-07-04T12:08:56.235", p, "-1SECOND+1SECOND");
    assertMath("2001-07-04T12:08:56.235", p, "-1MINUTE+1MINUTE");
    assertMath("2001-07-04T12:08:56.235", p, "-1HOUR+1HOUR");
    assertMath("2001-07-04T12:08:56.235", p, "-1DAY+1DAY");
    assertMath("2001-07-04T12:08:56.235", p, "-1MONTH+1MONTH");
    assertMath("2001-07-04T12:08:56.235", p, "-1YEAR+1YEAR");

    // more complex stuff
    assertMath("2000-07-04T12:08:56.236", p, "+1MILLISECOND-1YEAR");
    assertMath("2000-07-04T12:08:57.235", p, "+1SECOND-1YEAR");
    assertMath("2000-07-04T12:09:56.235", p, "+1MINUTE-1YEAR");
    assertMath("2000-07-04T13:08:56.235", p, "+1HOUR-1YEAR");
    assertMath("2000-07-05T12:08:56.235", p, "+1DAY-1YEAR");
    assertMath("2000-08-04T12:08:56.235", p, "+1MONTH-1YEAR");
    assertMath("2000-07-04T12:08:56.236", p, "-1YEAR+1MILLISECOND");
    assertMath("2000-07-04T12:08:57.235", p, "-1YEAR+1SECOND");
    assertMath("2000-07-04T12:09:56.235", p, "-1YEAR+1MINUTE");
    assertMath("2000-07-04T13:08:56.235", p, "-1YEAR+1HOUR");
    assertMath("2000-07-05T12:08:56.235", p, "-1YEAR+1DAY");
    assertMath("2000-08-04T12:08:56.235", p, "-1YEAR+1MONTH");
    assertMath("2000-07-01T00:00:00.000", p, "-1YEAR+1MILLISECOND/MONTH");
    assertMath("2000-07-04T00:00:00.000", p, "-1YEAR+1SECOND/DAY");
    assertMath("2000-07-04T00:00:00.000", p, "-1YEAR+1MINUTE/DAY");
    assertMath("2000-07-04T13:00:00.000", p, "-1YEAR+1HOUR/HOUR");
    assertMath("2000-07-05T12:08:56.000", p, "-1YEAR+1DAY/SECOND");
    assertMath("2000-08-04T12:08:56.000", p, "-1YEAR+1MONTH/SECOND");

    // "tricky" cases
    p.setNow(parser.parse("2006-01-31T17:09:59.999"));
    assertMath("2006-02-28T17:09:59.999", p, "+1MONTH");
    assertMath("2008-02-29T17:09:59.999", p, "+25MONTH");
    assertMath("2006-02-01T00:00:00.000", p, "/MONTH+35DAYS/MONTH");
    assertMath("2006-01-31T17:10:00.000", p, "+3MILLIS/MINUTE");
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   
  }
 
  public void testParseMathExceptions() throws Exception {
   
    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));
   
    Map<String,Integer> badCommands = new HashMap<String,Integer>();
    badCommands.put("/", 1);
    badCommands.put("+", 1);
    badCommands.put("-", 1);
    badCommands.put("/BOB", 1);
    badCommands.put("+SECOND", 1);
    badCommands.put("-2MILLI/", 4);
    badCommands.put(" +BOB", 0);
    badCommands.put("+2SECONDS ", 3);
    badCommands.put("/4", 1);
    badCommands.put("?SECONDS", 0);

    for (String command : badCommands.keySet()) {
      try {
        Date out = p.parseMath(command);
        fail("Didn't generate ParseException for: " + command);
      } catch (ParseException e) {
        assertEquals("Wrong pos for: " + command + " => " + e.getMessage(),
                     badCommands.get(command).intValue(), e.getErrorOffset());
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

    int len=val.length();
    if (val.charAt(len-1)=='Z') {
      return val.substring(0,len-1);
    } else if (val.startsWith("NOW")) {
      /* :TODO: let Locale/TimeZone come from init args for rounding only */
      DateMathParser p = new DateMathParser(UTC, Locale.US);
      try {
        return toInternal(p.parseMath(val.substring(3)));
      } catch (ParseException e) {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Invalid Date Math String:'" +val+'\'',e);
      }
    }
    throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Invalid Date String:'" +val+'\'');
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   
  }
 
  public void testParseStatelessness() throws Exception {

    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));

    String e = fmt.format(p.parseMath(""));
   
    Date trash = p.parseMath("+7YEARS");
    trash = p.parseMath("/MONTH");
    trash = p.parseMath("-5DAYS+20MINUTES");
    Thread.currentThread().sleep(5);
   
    String a = fmt.format(p.parseMath(""));
    assertEquals("State of DateMathParser changed", e, a);
  }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

    assertEquals("State of DateMathParser changed", e, a);
  }
   
  public void testParseMath() throws Exception {

    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));

    // No-Op
    assertMath("2001-07-04T12:08:56.235", p, "");
   
    // simple round
    assertMath("2001-07-04T12:08:56.000", p, "/SECOND");
    assertMath("2001-07-04T12:08:00.000", p, "/MINUTE");
    assertMath("2001-07-04T12:00:00.000", p, "/HOUR");
    assertMath("2001-07-04T00:00:00.000", p, "/DAY");
    assertMath("2001-07-01T00:00:00.000", p, "/MONTH");
    assertMath("2001-01-01T00:00:00.000", p, "/YEAR");

    // simple addition
    assertMath("2001-07-04T12:08:56.236", p, "+1MILLISECOND");
    assertMath("2001-07-04T12:08:57.235", p, "+1SECOND");
    assertMath("2001-07-04T12:09:56.235", p, "+1MINUTE");
    assertMath("2001-07-04T13:08:56.235", p, "+1HOUR");
    assertMath("2001-07-05T12:08:56.235", p, "+1DAY");
    assertMath("2001-08-04T12:08:56.235", p, "+1MONTH");
    assertMath("2002-07-04T12:08:56.235", p, "+1YEAR");

    // simple subtraction
    assertMath("2001-07-04T12:08:56.234", p, "-1MILLISECOND");
    assertMath("2001-07-04T12:08:55.235", p, "-1SECOND");
    assertMath("2001-07-04T12:07:56.235", p, "-1MINUTE");
    assertMath("2001-07-04T11:08:56.235", p, "-1HOUR");
    assertMath("2001-07-03T12:08:56.235", p, "-1DAY");
    assertMath("2001-06-04T12:08:56.235", p, "-1MONTH");
    assertMath("2000-07-04T12:08:56.235", p, "-1YEAR");

    // simple '+/-'
    assertMath("2001-07-04T12:08:56.235", p, "+1MILLISECOND-1MILLISECOND");
    assertMath("2001-07-04T12:08:56.235", p, "+1SECOND-1SECOND");
    assertMath("2001-07-04T12:08:56.235", p, "+1MINUTE-1MINUTE");
    assertMath("2001-07-04T12:08:56.235", p, "+1HOUR-1HOUR");
    assertMath("2001-07-04T12:08:56.235", p, "+1DAY-1DAY");
    assertMath("2001-07-04T12:08:56.235", p, "+1MONTH-1MONTH");
    assertMath("2001-07-04T12:08:56.235", p, "+1YEAR-1YEAR");

    // simple '-/+'
    assertMath("2001-07-04T12:08:56.235", p, "-1MILLISECOND+1MILLISECOND");
    assertMath("2001-07-04T12:08:56.235", p, "-1SECOND+1SECOND");
    assertMath("2001-07-04T12:08:56.235", p, "-1MINUTE+1MINUTE");
    assertMath("2001-07-04T12:08:56.235", p, "-1HOUR+1HOUR");
    assertMath("2001-07-04T12:08:56.235", p, "-1DAY+1DAY");
    assertMath("2001-07-04T12:08:56.235", p, "-1MONTH+1MONTH");
    assertMath("2001-07-04T12:08:56.235", p, "-1YEAR+1YEAR");

    // more complex stuff
    assertMath("2000-07-04T12:08:56.236", p, "+1MILLISECOND-1YEAR");
    assertMath("2000-07-04T12:08:57.235", p, "+1SECOND-1YEAR");
    assertMath("2000-07-04T12:09:56.235", p, "+1MINUTE-1YEAR");
    assertMath("2000-07-04T13:08:56.235", p, "+1HOUR-1YEAR");
    assertMath("2000-07-05T12:08:56.235", p, "+1DAY-1YEAR");
    assertMath("2000-08-04T12:08:56.235", p, "+1MONTH-1YEAR");
    assertMath("2000-07-04T12:08:56.236", p, "-1YEAR+1MILLISECOND");
    assertMath("2000-07-04T12:08:57.235", p, "-1YEAR+1SECOND");
    assertMath("2000-07-04T12:09:56.235", p, "-1YEAR+1MINUTE");
    assertMath("2000-07-04T13:08:56.235", p, "-1YEAR+1HOUR");
    assertMath("2000-07-05T12:08:56.235", p, "-1YEAR+1DAY");
    assertMath("2000-08-04T12:08:56.235", p, "-1YEAR+1MONTH");
    assertMath("2000-07-01T00:00:00.000", p, "-1YEAR+1MILLISECOND/MONTH");
    assertMath("2000-07-04T00:00:00.000", p, "-1YEAR+1SECOND/DAY");
    assertMath("2000-07-04T00:00:00.000", p, "-1YEAR+1MINUTE/DAY");
    assertMath("2000-07-04T13:00:00.000", p, "-1YEAR+1HOUR/HOUR");
    assertMath("2000-07-05T12:08:56.000", p, "-1YEAR+1DAY/SECOND");
    assertMath("2000-08-04T12:08:56.000", p, "-1YEAR+1MONTH/SECOND");

    // "tricky" cases
    p.setNow(parser.parse("2006-01-31T17:09:59.999"));
    assertMath("2006-02-28T17:09:59.999", p, "+1MONTH");
    assertMath("2008-02-29T17:09:59.999", p, "+25MONTH");
    assertMath("2006-02-01T00:00:00.000", p, "/MONTH+35DAYS/MONTH");
    assertMath("2006-01-31T17:10:00.000", p, "+3MILLIS/MINUTE");
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   
  }
 
  public void testParseMathExceptions() throws Exception {
   
    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));
   
    Map<String,Integer> badCommands = new HashMap<String,Integer>();
    badCommands.put("/", 1);
    badCommands.put("+", 1);
    badCommands.put("-", 1);
    badCommands.put("/BOB", 1);
    badCommands.put("+SECOND", 1);
    badCommands.put("-2MILLI/", 4);
    badCommands.put(" +BOB", 0);
    badCommands.put("+2SECONDS ", 3);
    badCommands.put("/4", 1);
    badCommands.put("?SECONDS", 0);

    for (String command : badCommands.keySet()) {
      try {
        Date out = p.parseMath(command);
        fail("Didn't generate ParseException for: " + command);
      } catch (ParseException e) {
        assertEquals("Wrong pos for: " + command + " => " + e.getMessage(),
                     badCommands.get(command).intValue(), e.getErrorOffset());
View Full Code Here

Examples of org.elasticsearch.common.joda.DateMathParser

        Object from = null;
        Object to = null;
        boolean includeLower = true;
        boolean includeUpper = true;
        DateTimeZone timeZone = null;
        DateMathParser forcedDateParser = null;
        float boost = 1.0f;
        String queryName = null;

        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else {
                if ("from".equals(currentFieldName)) {
                    from = parser.objectBytes();
                } else if ("to".equals(currentFieldName)) {
                    to = parser.objectBytes();
                } else if ("include_lower".equals(currentFieldName) || "includeLower".equals(currentFieldName)) {
                    includeLower = parser.booleanValue();
                } else if ("include_upper".equals(currentFieldName) || "includeUpper".equals(currentFieldName)) {
                    includeUpper = parser.booleanValue();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if ("gt".equals(currentFieldName)) {
                    from = parser.objectBytes();
                    includeLower = false;
                } else if ("gte".equals(currentFieldName) || "ge".equals(currentFieldName)) {
                    from = parser.objectBytes();
                    includeLower = true;
                } else if ("lt".equals(currentFieldName)) {
                    to = parser.objectBytes();
                    includeUpper = false;
                } else if ("lte".equals(currentFieldName) || "le".equals(currentFieldName)) {
                    to = parser.objectBytes();
                    includeUpper = true;
                } else if ("time_zone".equals(currentFieldName) || "timeZone".equals(currentFieldName)) {
                    timeZone = DateMathParser.parseZone(parser.text());
                } else if ("_name".equals(currentFieldName)) {
                    queryName = parser.text();
                } else if ("format".equals(currentFieldName)) {
                    forcedDateParser = new DateMathParser(Joda.forPattern(parser.text()), DateFieldMapper.Defaults.TIME_UNIT);
                } else {
                    throw new QueryParsingException(parseContext.index(), "[range] query does not support [" + currentFieldName + "]");
                }
            }
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.