Skip to content
Browse files

automatically detect Dates, remove special date() method.

  • Loading branch information...
1 parent 8bc0cb8 commit 411170320d158d9d906c6c0ba096f1c4953d0bdc Robert Newson committed Apr 26, 2009
Showing with 24 additions and 80 deletions.
  1. +2 −16 README.md
  2. +22 −64 src/main/java/com/github/rnewson/couchdb/lucene/RhinoDocument.java
View
18 README.md
@@ -88,12 +88,6 @@ The following indexing options can be defaulted;
<td>default</td>
</tr>
<tr>
- <th>type</th>
- <td>the type of data, which may affect analysis</td>
- <td>date, number, text</td>
- <td>text</td>
- </tr>
- <tr>
<th>store</th>
<td>whether the data is stored. The value will be returned in the search result.</td>
<td>yes, no</td>
@@ -129,6 +123,8 @@ var doc = new Document();
Data may be added to this document with the add method which takes an optional second object argument that can override any of the above default values.
+The data is usually interpreted as a String but couchdb-lucene provides special handling if a Javascript Date object is passed. Specifically, the date is indexed as a numeric value, which allows correct sorting, and stored (if requested) in ISO 8601 format (with a timezone marker).
+
<pre>
// Add with all the defaults.
doc.add("value");
@@ -144,16 +140,6 @@ doc.add("don't analyze me", {"index":"not_analyzed"});
// Extract text from the named attachment and index it (but not store it).
doc.attachment("attachment name", {"field":"attachments"});
-
-// Interpret "value" as a date using the default date formats.
-doc.add("2009-01-01T00:00:00Z", {"type":"date"});
-
-// intrepret "value" as a date using the supplied format string
-// (see Java's SimpleDateFormat class for the syntax).
-doc.add("2009-01-01", {"type":"date", "format":"YYYY-MM-dd"});
-
-// intrepret "value" as a number.
-doc.add("100", {"type":"number"});
</pre>
<h3>Example Transforms</h3>
View
86 src/main/java/com/github/rnewson/couchdb/lucene/RhinoDocument.java
@@ -18,14 +18,12 @@
import java.io.IOException;
import java.io.InputStream;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.mozilla.javascript.Context;
@@ -43,10 +41,6 @@
private static final Tika TIKA = new Tika();
- private static final DateFormat[] DATE_FORMATS = new DateFormat[] {
- new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'"),
- new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") };
-
private static final Map<String, Field.Index> Index = new HashMap<String, Field.Index>();
private static final Map<String, Field.Store> Store = new HashMap<String, Field.Store>();
@@ -114,7 +108,7 @@ public static void jsFunction_add(final Context cx, final Scriptable thisObj, fi
if (obj.has("field", null)) {
field = (String) obj.get("field", null);
}
-
+
// Change the stored flag.
if (obj.has("store", null)) {
store = Store.get(obj.get("store", null));
@@ -132,7 +126,26 @@ public static void jsFunction_add(final Context cx, final Scriptable thisObj, fi
}
- doc.add(new Field(field, args[0].toString(), store, index, tv));
+ if (args[0] instanceof String) {
+ doc.add(new Field(field, (String) args[0], store, index, tv));
+ } else {
+ // Is it a date?
+ try {
+ final Date date = (Date) Context.jsToJava(args[0], Date.class);
+
+ // Special indexed form.
+ doc.add(new Field(field, Long.toString(date.getTime()), Field.Store.NO,
+ Field.Index.NOT_ANALYZED_NO_NORMS));
+
+ // Store in ISO8601 format, if requested.
+ if (Field.Store.YES == store) {
+ final String asString = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(date);
+ doc.add(new Field(field, asString, Field.Store.YES, Field.Index.NO));
+ }
+ } catch (final EvaluatorException e) {
+ throw Context.reportRuntimeError(args[0].getClass() + " not supported.");
+ }
+ }
}
public static void jsFunction_attachment(final Context cx, final Scriptable thisObj, final Object[] args,
@@ -167,61 +180,6 @@ public static void jsFunction_attachment(final Context cx, final Scriptable this
}
}
- public static void jsFunction_date(final Context cx, final Scriptable thisObj, final Object[] args,
- final Function funObj) throws IOException {
- final RhinoDocument doc = checkInstance(thisObj);
- if (args.length < 2) {
- throw Context.reportRuntimeError("field name and value required.");
- }
-
- final String field = args[0].toString();
-
- final Field.Store str;
- if (args.length > 2) {
- final String strtype = args[2].toString().toUpperCase();
- str = Store.get(strtype) == null ? Field.Store.NO : (Field.Store) Store.get(strtype);
- } else {
- str = Field.Store.NO;
- }
-
- // Is it a native date?
- try {
- final Date date = (Date) Context.jsToJava(args[1], Date.class);
- doc.doc.add(new Field(field, Long.toString(date.getTime()), str, Field.Index.NOT_ANALYZED_NO_NORMS));
- return;
- } catch (final EvaluatorException e) {
- // Ignore.
- }
-
- // Try to parse it as a string.
- final String value = Context.toString(args[1]);
-
- final DateFormat[] formats;
- if (args.length > 3) {
- formats = new DateFormat[] { new SimpleDateFormat(args[3].toString()) };
- } else {
- formats = DATE_FORMATS;
- }
-
- final Date parsed = parse_date(formats, value);
- if (parsed == null) {
- throw Context.reportRuntimeError("failed to parse date value: " + value);
- }
-
- doc.doc.add(new Field(field, Long.toString(parsed.getTime()), str, Field.Index.NOT_ANALYZED_NO_NORMS));
- }
-
- private static Date parse_date(final DateFormat[] formats, final String value) {
- for (final DateFormat fmt : formats) {
- try {
- return fmt.parse(value);
- } catch (final ParseException e) {
- continue;
- }
- }
- return null;
- }
-
private static RhinoDocument checkInstance(Scriptable obj) {
if (obj == null || !(obj instanceof RhinoDocument)) {
throw Context.reportRuntimeError("called on incompatible object.");

0 comments on commit 4111703

Please sign in to comment.
Something went wrong with that request. Please try again.