List of usage examples for org.apache.solr.util DateMathParser parseMath
public Date parseMath(String math) throws ParseException
From source file:com.appleframework.solr.util.DateFormatUtil.java
License:Apache License
/** * Parses a String which may be a date (in the standard format) followed by * an optional math expression./*from ww w. ja v a 2s . c om*/ * * @param now * an optional fixed date to use as "NOW" in the DateMathParser * @param val * the string to parse */ public static Date parseMath(Date now, String val) { String math; final DateMathParser p = new DateMathParser(); 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); } }
From source file:com.appleframework.solr.util.DateFormatUtil.java
License:Apache License
/** * Parses a String which may be a date followed by an optional math * expression./*from w w w . j a v a 2 s .c o m*/ * * @param now * an optional fixed date to use as "NOW" in the DateMathParser * @param val * the string to parse */ public static Date parseMathLenient(Date now, String val, SolrQueryRequest req) { String math; final DateMathParser p = new DateMathParser(); if (null != now) p.setNow(now); if (val.startsWith(DateFormatUtil.NOW)) { math = val.substring(DateFormatUtil.NOW.length()); } else { final int zz = val.indexOf(DateFormatUtil.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); } }
From source file:fi.nationallibrary.ndl.solr.schema.NegativeSupportingDateField.java
License:Apache License
/** * Needs to be overridden because this method calls parseDate * which is a static method in DateField *//*ww w.j a v a 2 s . c o m*/ @Override public Date parseMath(Date now, String val) { // Ugly hax if (val.length() == 8) val = "AD" + val.substring(0, 4) + "-" + val.substring(4, 6) + "-" + val.substring(6, 8); if (val.length() == 12) val += "T00:00:00Z"; 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 { try { p.setNow(eraAwareParseDate(val)); } catch (ParseException e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid Date in Date Math String:'" + val + '\'', e); } } 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); } }
From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java
License:Open Source License
/** * @deprecated Use getFacetRangeCounts which is more generalized *//*from w w w. j a v a2 s.c o m*/ @Deprecated public void getFacetDateCounts(String dateFacet, NamedList<Object> resOuter) throws IOException, ParseException, RepositoryException, JahiaException { parseParams(FacetParams.FACET_DATE, dateFacet); String f = facetValue; final NamedList<Object> resInner = new SimpleOrderedMap<Object>(); String fieldName = StringUtils.substringBeforeLast(f, PROPNAME_INDEX_SEPARATOR); ExtendedPropertyDefinition epd = NodeTypeRegistry.getInstance() .getNodeType(params.get("f." + f + ".facet.nodetype")).getPropertyDefinition(fieldName); String fieldNameInIndex = getFieldNameInIndex(f, fieldName, epd, params.getFieldParam(f, "facet.locale")); String prefix = params.getFieldParam(f, FacetParams.FACET_PREFIX); DateField ft = StringUtils.isEmpty(prefix) ? JahiaQueryParser.DATE_TYPE : JahiaQueryParser.JR_DATE_TYPE; final SchemaField sf = new SchemaField(fieldNameInIndex, ft); // TODO: Should we use the key now ? // resOuter.add(key, resInner); resOuter.add(fieldName + PROPNAME_INDEX_SEPARATOR + fieldNameInIndex, resInner); if (!(epd.getRequiredType() == PropertyType.DATE)) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can not date facet on a field which is not a DateField: " + f); } Integer minCount = params.getFieldInt(f, FacetParams.FACET_MINCOUNT); if (minCount == null) { Boolean zeros = params.getFieldBool(f, FacetParams.FACET_ZEROS); // mincount = (zeros!=null && zeros) ? 0 : 1; minCount = (zeros != null && !zeros) ? 1 : 0; // current default is to include zeros. } final String startS = required.getFieldParam(f, FacetParams.FACET_DATE_START); final Date start; try { start = JahiaQueryParser.DATE_TYPE.parseMath(NOW, startS); } catch (SolrException e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "date facet 'start' is not a valid Date string: " + startS, e); } final String endS = required.getFieldParam(f, FacetParams.FACET_DATE_END); Date end; // not final, hardend may change this try { end = JahiaQueryParser.DATE_TYPE.parseMath(NOW, endS); } catch (SolrException e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "date facet 'end' is not a valid Date string: " + endS, e); } if (end.before(start)) { throw new SolrException(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(DateField.UTC, Locale.US); dmp.setNow(NOW); 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 = JahiaQueryParser.DATE_TYPE.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; } } if (high.before(low)) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "date facet infinite loop (is gap negative?)"); } final boolean includeLower = (include.contains(FacetRangeInclude.LOWER) || (include.contains(FacetRangeInclude.EDGE) && low.equals(start))); final boolean includeUpper = (include.contains(FacetRangeInclude.UPPER) || (include.contains(FacetRangeInclude.EDGE) && high.equals(end))); Query rangeQuery = getRangeQuery(ft, null, sf, prefix, low, high, includeLower, includeUpper); int count = rangeCount(rangeQuery); if (count >= minCount) { // TODO: Can we use just label here ? resInner.add(label + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(), count); } low = high; } } catch (java.text.ParseException e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "date facet 'gap' is not a valid Date Math string: " + gap, e); } // explicitly return the gap and end so all the counts are meaningful resInner.add("gap", gap); resInner.add("start", start); resInner.add("end", end); final String[] othersP = params.getFieldParams(f, FacetParams.FACET_DATE_OTHER); if (null != othersP && 0 < othersP.length) { final Set<FacetDateOther> others = EnumSet.noneOf(FacetDateOther.class); for (final String o : othersP) { others.add(FacetDateOther.get(o)); } // no matter what other values are listed, we don't do // anything if "none" is specified. if (!others.contains(FacetDateOther.NONE)) { boolean all = others.contains(FacetDateOther.ALL); if (all || others.contains(FacetDateOther.BEFORE)) { Query rangeQuery = getRangeQuery(ft, null, sf, prefix, null, start, false, (include.contains(FacetRangeInclude.OUTER) || (!(include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE))))); int count = rangeCount(rangeQuery); if (count >= minCount) { resInner.add( FacetDateOther.BEFORE.toString() + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(), count); } } if (all || others.contains(FacetDateOther.AFTER)) { Query rangeQuery = getRangeQuery(ft, null, sf, prefix, end, null, (include.contains(FacetRangeInclude.OUTER) || (!(include.contains(FacetRangeInclude.UPPER) || include.contains(FacetRangeInclude.EDGE)))), false); int count = rangeCount(rangeQuery); if (count >= minCount) { resInner.add( FacetDateOther.AFTER.toString() + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(), count); } } if (all || others.contains(FacetDateOther.BETWEEN)) { Query rangeQuery = getRangeQuery(ft, null, sf, prefix, start, end, (include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE)), (include.contains(FacetRangeInclude.UPPER) || include.contains(FacetRangeInclude.EDGE))); int count = rangeCount(rangeQuery); if (count >= minCount) { resInner.add(FacetDateOther.BETWEEN.toString() + PROPNAME_INDEX_SEPARATOR + rangeQuery.toString(), count); } } } } }
From source file:org.opencommercesearch.Utils.java
License:Apache License
private static Date parseDate(String value, DateMathParser dmp) throws ParseException { if (SolrDatePattern.matcher(value).find()) { return dmp.parseMath(StringUtils.remove(value, NOW)); } else {/* w ww. j a va 2 s. c om*/ return iso8601Formatter.parse(value); } }