07 January 2011

How to handle the date field in Apache Solr

Default Date format for Solr Date field is yyyy-MM-dd'T'HH:mm:sss'Z

Defining the date field in schema.xml of apache solr 
< field name="Date" datetimeformat="yyyy-MM-dd'T'HH:mm:sss'Z'" 
indexed="true" multivalued="false" stored='true' type="date"> </field>

Creating Date in Java application in required format,
the default Date format of java.util.Date is same as above so no need to format it.

we can directly add date like

SolrInputDocument doc = new SolarInputDocument();
doc.add("Date", new Date(), 0.3f);

or parsing date string using Date formater:

Date date = new Date();
String formatedDate = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sss'Z'")).format(date);

No comments:

Post a Comment