Package org.apache.solr.util

Examples of org.apache.solr.util.DateMathParser


   * @param now an optional fixed date to use as "NOW" in the DateMathParser
   * @param val the string to parse
   */
  public Date parseMath(Date now, String val) {
    String math = null;
    final DateMathParser p = new DateMathParser(MATH_TZ, MATH_LOCALE);
   
    if (null != now) p.setNow(now);
   
    if (val.startsWith(NOW)) {
      math = val.substring(NOW.length());
    } else {
      final int zz = val.indexOf(Z);
      if (0 < zz) {
        math = val.substring(zz+1);
        try {
          // p.setNow(toObject(val.substring(0,zz)));
          p.setNow(parseDate(val.substring(0,zz+1)));
        } catch (ParseException e) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                   "Invalid Date in Date Math String:'"
                                   +val+'\'',e);
        }
      } else {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                 "Invalid Date String:'" +val+'\'');
      }
    }

    if (null == math || math.equals("")) {
      return p.getNow();
    }
   
    try {
      return p.parseMath(math);
    } catch (ParseException e) {
      throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                               "Invalid Date Math String:'" +val+'\'',e);
    }
  }
View Full Code Here


   * @param now an optional fixed date to use as "NOW" in the DateMathParser
   * @param val the string to parse
   */
  public Date parseMathLenient(Date now, String val, SolrQueryRequest req) {
    String math = null;
    final DateMathParser p = new DateMathParser(MATH_TZ, MATH_LOCALE);

    if (null != now) p.setNow(now);

    if (val.startsWith(NOW)) {
      math = val.substring(NOW.length());
    } else {
      final int zz = val.indexOf(Z);
      if (0 < zz) {
        math = val.substring(zz+1);
        try {
          // p.setNow(toObject(val.substring(0,zz)));
          p.setNow(parseDateLenient(val.substring(0,zz+1), req));
        } catch (ParseException e) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                   "Invalid Date in Date Math String:'"
                                   +val+'\'',e);
        }
      } else {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                 "Invalid Date String:'" +val+'\'');
      }
    }

    if (null == math || math.equals("")) {
      return p.getNow();
    }

    try {
      return p.parseMath(math);
    } catch (ParseException e) {
      throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                               "Invalid Date Math String:'" +val+'\'',e);
    }
  }
View Full Code Here

        // delete anything too old, regardless of other policies
        try {
          if (maxCommitAge != null) {
            if (maxCommitAgeTimeStamp==-1) {
              DateMathParser dmp = new DateMathParser(DateField.UTC, Locale.US);
              maxCommitAgeTimeStamp = dmp.parseMath(maxCommitAge).getTime();
            }
            if (commit.getTimestamp() < maxCommitAgeTimeStamp) {
              commit.delete();
              continue;
            }
View Full Code Here

  protected DateMathParser p = null;
 
  @Override
  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

          (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);

    final int minCount = params.getFieldInt(f,FacetParams.FACET_MINCOUNT, 0);

    String[] iStrs = params.getFieldParams(f,FacetParams.FACET_DATE_INCLUDE);
    // Legacy support for default of [lower,upper,edge] for date faceting
    // this is not handled by FacetRangeInclude.parseParam because
    // range faceting has differnet defaults
    final EnumSet<FacetRangeInclude> include =
      (null == iStrs || 0 == iStrs.length ) ?
      EnumSet.of(FacetRangeInclude.LOWER,
                 FacetRangeInclude.UPPER,
                 FacetRangeInclude.EDGE)
      : FacetRangeInclude.parseParam(iStrs);

    try {
      Date low = start;
      while (low.before(end)) {
        dmp.setNow(low);
        String label = ft.toExternal(low);

        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

    protected Object parseGap(final String rawval) {
      return rawval;
    }
    @Override
    public Date parseAndAddGap(Date value, String gap) throws java.text.ParseException {
      final DateMathParser dmp = new DateMathParser(DateField.UTC, Locale.US);
      dmp.setNow(value);
      return dmp.parseMath(gap);
    }
View Full Code Here

    // Test date math syntax
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));

    assertU(delQ("*:*"));
    DateMathParser dmp = new DateMathParser(DateField.UTC, Locale.US);
    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']");
 
View Full Code Here

    checkPrecisionSteps("tdate");

    // For tdate tests
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateMathParser dmp = new DateMathParser(DateField.UTC, Locale.US);

    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",
            "facet.date", "tdate",
            "facet.date.start", "NOW/DAY",
            "facet.date.end", "NOW/DAY+6DAYS",
            "facet.date.gap", "+1DAY");
    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");

    testFacetDate(req, "tdate", format.format(dmp.parseMath("/DAY")), "4");
    testFacetDate(req, "tdate", format.format(dmp.parseMath("/DAY+5DAYS")), "2");
  }
View Full Code Here

              .getFunctionsNamespace(Collections.EMPTY_LIST,null));

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
      format.setTimeZone(TimeZone.getTimeZone("UTC"));
      resetEvaluatorBagDateMathParser();
      DateMathParser dmp = new DateMathParser(TimeZone.getDefault(), Locale.getDefault());

      String s = vri.replaceTokens("${dataimporter.functions.formatDate('NOW/DAY','yyyy-MM-dd HH:mm')}");
      assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(dmp.parseMath("/DAY")), s);
    } finally {
      Context.CURRENT_CONTEXT.remove();
    }
  }
View Full Code Here

      l.add(m);

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
      format.setTimeZone(TimeZone.getTimeZone("UTC"));
      resetEvaluatorBagDateMathParser();
      DateMathParser dmp = new DateMathParser(TimeZone.getDefault(), Locale.getDefault());

      resolver.addNamespace("dataimporter.functions", EvaluatorBag
              .getFunctionsNamespace(l,null));
      String s = resolver
              .replaceTokens("${dataimporter.functions.formatDate('NOW/DAY','yyyy-MM-dd HH:mm')}");
      assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm")
              .format(dmp.parseMath("/DAY")), s);
      assertEquals("Hello World", resolver
              .replaceTokens("${dataimporter.functions.test('TEST')}"));
    } finally {
      Context.CURRENT_CONTEXT.remove();
    }
View Full Code Here

TOP

Related Classes of org.apache.solr.util.DateMathParser

Copyright © 2018 www.massapicom. 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.