changeset 621:fd15da41afca

allow list of values to be stored in lucene
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 Jan 2016 21:41:33 -0700
parents 89eb02f9827f
children 1a53333eb4d5
files lucene/src/luan/modules/lucene/LuceneIndex.java
diffstat 1 files changed, 76 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java	Mon Jan 04 17:27:36 2016 -0700
+++ b/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed Jan 06 21:41:33 2016 -0700
@@ -444,6 +444,48 @@
 
 
 
+	private IndexableField newField(LuanState luan,String name,Object value,Field.Store store,Set<String> indexed)
+		throws LuanException
+	{
+		if( value instanceof String ) {
+			String s = (String)value;
+			FieldParser fp = mfp.fields.get(name);
+			if( fp != null ) {
+				if( fp instanceof StringFieldParser && fp != STRING_FIELD_PARSER ) {
+					return new TextField(name, s, store);
+				} else {
+					return new StringField(name, s, store);
+				}
+			} else {
+				return new StoredField(name, s);
+			}
+		} else if( value instanceof Integer ) {
+			int i = (Integer)value;
+			if( indexed.contains(name) ) {
+				return new IntField(name, i, store);
+			} else {
+				return new StoredField(name, i);
+			}
+		} else if( value instanceof Long ) {
+			long i = (Long)value;
+			if( indexed.contains(name) ) {
+				return new LongField(name, i, store);
+			} else {
+				return new StoredField(name, i);
+			}
+		} else if( value instanceof Double ) {
+			double i = (Double)value;
+			if( indexed.contains(name) ) {
+				return new DoubleField(name, i, store);
+			} else {
+				return new StoredField(name, i);
+			}
+		} else if( value instanceof byte[] ) {
+			byte[] b = (byte[])value;
+			return new StoredField(name, b);
+		} else
+			throw new LuanException(luan,"invalid value type "+value.getClass()+"' for '"+name+"'");
+	}
 
 	private Document toLucene(LuanState luan,LuanTable table,Set indexOnly) throws LuanException {
 		Set<String> indexed = mfp.fields.keySet();
@@ -455,70 +497,52 @@
 			String name = (String)key;
 			Object value = entry.getValue();
 			Field.Store store = indexOnly.contains(name) ? Field.Store.NO : Field.Store.YES;
-			if( value instanceof String ) {
-				String s = (String)value;
-				FieldParser fp = mfp.fields.get(name);
-				if( fp != null ) {
-					if( fp instanceof StringFieldParser && fp != STRING_FIELD_PARSER ) {
-						doc.add(new TextField(name, s, store));
-					} else {
-						doc.add(new StringField(name, s, store));
-					}
-				} else {
-					doc.add(new StoredField(name, s));
-				}
-			} else if( value instanceof Integer ) {
-				int i = (Integer)value;
-				if( indexed.contains(name) ) {
-					doc.add(new IntField(name, i, store));
-				} else {
-					doc.add(new StoredField(name, i));
+			if( !(value instanceof LuanTable) ) {
+				doc.add(newField(luan, name, value, store, indexed));
+			} else { // list
+				LuanTable list = (LuanTable)value;
+				for( Object el : list.asList() ) {
+					doc.add(newField(luan, name, el, store, indexed));
 				}
-			} else if( value instanceof Long ) {
-				long i = (Long)value;
-				if( indexed.contains(name) ) {
-					doc.add(new LongField(name, i, store));
-				} else {
-					doc.add(new StoredField(name, i));
-				}
-			} else if( value instanceof Double ) {
-				double i = (Double)value;
-				if( indexed.contains(name) ) {
-					doc.add(new DoubleField(name, i, store));
-				} else {
-					doc.add(new StoredField(name, i));
-				}
-			} else if( value instanceof byte[] ) {
-				byte[] b = (byte[])value;
-				doc.add(new StoredField(name, b));
-			} else
-				throw new LuanException(luan,"invalid value type "+value.getClass()+"' for '"+name+"'");
+			}
 		}
 		return doc;
 	}
 
+	private static Object getValue(LuanState luan,IndexableField ifld) throws LuanException {
+		BytesRef br = ifld.binaryValue();
+		if( br != null )
+			return br.bytes;
+		Number n = ifld.numericValue();
+		if( n != null )
+			return n;
+		String s = ifld.stringValue();
+		if( s != null )
+			return s;
+		throw new LuanException(luan,"invalid field type for "+ifld);
+	}
+
 	private static LuanTable toTable(LuanState luan,Document doc) throws LuanException {
 		if( doc==null )
 			return null;
 		LuanTable table = new LuanTable();
 		for( IndexableField ifld : doc ) {
 			String name = ifld.name();
-			BytesRef br = ifld.binaryValue();
-			if( br != null ) {
-				table.rawPut(name,br.bytes);
-				continue;
+			Object value = getValue(luan,ifld);
+			Object old = table.rawGet(name);
+			if( old == null ) {
+				table.rawPut(name,value);
+			} else {
+				LuanTable list;
+				if( old instanceof LuanTable ) {
+					list = (LuanTable)old;
+				} else {
+					list = new LuanTable();
+					list.rawPut(1,old);
+					table.rawPut(name,list);
+				}
+				list.rawPut(list.rawLength()+1,value);
 			}
-			Number n = ifld.numericValue();
-			if( n != null ) {
-				table.rawPut(name,n);
-				continue;
-			}
-			String s = ifld.stringValue();
-			if( s != null ) {
-				table.rawPut(name,s);
-				continue;
-			}
-			throw new LuanException(luan,"invalid field type for "+ifld);
 		}
 		return table;
 	}