/** * Returns a formatter that can be use by the current thread if needed to * convert Date objects to the Internal representation. * * @deprecated use {@link org.apache.solr.common.util.DateUtil#getThreadLocalDateFormat()} */ public static DateFormat getThreadLocalDateFormat() { return DateUtil.getThreadLocalDateFormat(); }
public static Date parseDate(String d, Collection<String> fmts) throws ParseException { // 2007-04-26T08:05:04Z if (d.endsWith("Z") && d.length() > 20) { return getThreadLocalDateFormat().parse(d); } return parseDate(d, fmts, null); }
/** * Add a numeric range facet. * * @param field The field * @param start The start of range * @param end The end of the range * @param gap The gap between each count * @return this */ public SolrQuery addDateRangeFacet(String field, Date start, Date end, String gap) { add(FacetParams.FACET_RANGE, field); add(String.format(Locale.ROOT, "f.%s.%s", field, FacetParams.FACET_RANGE_START), DateUtil.getThreadLocalDateFormat().format(start)); add(String.format(Locale.ROOT, "f.%s.%s", field, FacetParams.FACET_RANGE_END), DateUtil.getThreadLocalDateFormat().format(end)); add(String.format(Locale.ROOT, "f.%s.%s", field, FacetParams.FACET_RANGE_GAP), gap); this.set(FacetParams.FACET, true); return this; }
/** * Can be used to transform input values based on their {@link org.apache.solr.schema.SchemaField} * <p/> * This implementation only formats dates using the {@link org.apache.solr.common.util.DateUtil}. * * @param val The value to transform * @param schFld The {@link org.apache.solr.schema.SchemaField} * @return The potentially new value. */ protected String transformValue(String val, SchemaField schFld) { String result = val; if (schFld != null && schFld.getType() instanceof DateField) { //try to transform the date try { Date date = DateUtil.parseDate(val, dateFormats); DateFormat df = DateUtil.getThreadLocalDateFormat(); result = df.format(date); } catch (Exception e) { // Let the specific fieldType handle errors // throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid value: " + val + " for field: " + schFld, e); } } return result; }
public static void writeXML( SolrInputDocument doc, Writer writer ) throws IOException { writer.write("<doc boost=\""+doc.getDocumentBoost()+"\">"); for( SolrInputField field : doc ) { float boost = field.getBoost(); String name = field.getName(); for( Object v : field ) { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); }else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0,bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost ); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name ); } // only write the boost for the first multi-valued field // otherwise, the used boost is the product of all the boost values boost = 1.0f; } } writer.write("</doc>"); }
private static void writeVal(Writer writer, float boost, String name, Object v, String update) throws IOException { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); } else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0, bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } if (update == null) { if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name ); } } else { if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost, "update", update); } else { if (v == null) { XML.writeXML(writer, "field", null, "name", name, "update", update, "null", true); } else { XML.writeXML(writer, "field", v.toString(), "name", name, "update", update); } } } }
private static DcsEvent getEvent(SolrDocument doc) { DcsEvent event = new DcsEvent(); event.setId(getFirst(doc, EntityField.ID)); Date date = (Date) doc.getFirstValue(EventField.DATE.solrName()); if (date != null) { event.setDate(DateUtil.getThreadLocalDateFormat().format(date)); } event.setDetail(getFirst(doc, EventField.DETAIL)); event.setOutcome(getFirst(doc, EventField.OUTCOME)); event.setEventType(getFirst(doc, EventField.TYPE)); event.setTargets(getEntityRefSet(doc, EventField.TARGET)); event.setAlternateIds(getResourceIdentifierSet(doc)); return event; }