public JSONArray MapDoc(JSONObject doc) {
JSONArray out = new JSONArray();
try {
Analyzer analyzer = getAnalyzer();
SingleDocumentIndex index = new SingleDocumentIndex();
// user specified field set
Map<String, List<Object>> mapped = CouchIndexUtils.MapJSONObject2Object(doc, null);
if (mapped == null) {
// Log("can't map json doc");
return out.put(new JSONArray());
}
for (String field : mapped.keySet()) {
try {
FieldOption fo = findField(field);
if (fo == null) continue;
List<Object> l = mapped.get(field);
Iterator<Object> iter = l.iterator();
while (iter.hasNext()) {
Object o = iter.next();
if (o == null) continue;
final IndexType type = fo.getType();
final float boost = (float)fo.getBoost();
String luceneName = fo.getFieldName();
if (luceneName == null) luceneName = field;
switch (type) {
case STRING: index.addField(luceneName, IndexUtilities.ObjectToString(o), analyzer, boost);break;
case KEYWORD: index.addField(luceneName, IndexUtilities.ObjectToString(o), boost);break;
case BOOLEAN: index.addField(luceneName, o, boost);break;
// JSON doesn't distinguish between int and long, always returns long
case INT: index.addField(luceneName, (Long)o, boost);break;
case LONG: index.addField(luceneName, (Long)o, boost);break;
case FLOAT: index.addField(luceneName, (Float)o, boost);break;
case DOUBLE: index.addField(luceneName, (Double)o, boost);break;
case DATE: {
Date d = null;
d = parseDate((String)o);
if (d != null) {
index.addField(luceneName, d.getTime(), boost);break;
} else {
index.addField(luceneName, IndexUtilities.ObjectToString(o), boost);break;
}
}
case JSONOBJECT: index.addField(luceneName, o, boost);break;
case NULL: index.addField(luceneName, (Object)JSONObject.NULL, boost);break;
}
}
} catch (Exception e) {
/* probably a casting exception */
}
}
out = index.jsonMap();
} catch (Exception e) {
out.put(new JSONArray());
if (e != null ) {
Log("Map Exception: " + e.toString());
}