diff lucene/src/luan/modules/lucene/LuceneWriter.java @ 544:c5a93767cc5c

lucene overhaul, untested
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Jun 2015 19:11:44 -0600
parents d9df6d6cb927
children ddcd4296107a
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/LuceneWriter.java	Mon Jun 08 01:11:08 2015 -0400
+++ b/lucene/src/luan/modules/lucene/LuceneWriter.java	Fri Jun 12 19:11:44 2015 -0600
@@ -7,6 +7,8 @@
 import java.util.ArrayList;
 import org.apache.lucene.index.IndexableField;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.NumericUtils;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -31,12 +33,30 @@
 		index.writer.commit();
 	}
 
-	void addDocument(LuanState luan,LuanTable doc) throws LuanException, IOException {
-		index.writer.addDocument(index.toLucene(luan,doc));
+	private Term term(String key,int value) {
+		BytesRef br = new BytesRef();
+		NumericUtils.intToPrefixCoded(value,0,br);
+		return new Term(key,br);
+	}
+
+	private Term term(String key,long value) {
+		BytesRef br = new BytesRef();
+		NumericUtils.longToPrefixCoded(value,0,br);
+		return new Term(key,br);
 	}
 
-	void updateDocument(LuanState luan,Term term,LuanTable doc) throws LuanException, IOException {
-		index.writer.updateDocument(term,index.toLucene(luan,doc));
+	private Term term(LuanState luan,String key,Object value) throws LuanException {
+		if( value instanceof String )
+			return new Term( key, (String)value );
+		if( value instanceof Integer )
+			return term( key, (Integer)value );
+		if( value instanceof Long )
+			return term( key, (Long)value );
+		if( value instanceof Float )
+			return term( key, NumericUtils.floatToSortableInt((Float)value) );
+		if( value instanceof Double )
+			return term( key, NumericUtils.doubleToSortableLong((Double)value) );
+		throw luan.exception("invalid value type '"+value.getClass().getSimpleName()+"' for key '"+key+"'");
 	}
 
 	public void delete_documents(LuanState luan,LuanTable tblTerms) throws LuanException, IOException {
@@ -46,27 +66,21 @@
 			Object value = entry.getValue();
 			if( !(key instanceof String) )
 				throw luan.exception("key must be a string but got "+key.getClass().getSimpleName());
-			if( !(value instanceof String) )
-				throw luan.exception("value must be a string but got "+value.getClass().getSimpleName());
-			list.add( index.newTerm( (String)key, (String)value ) );
+			list.add( term( luan, (String)key, value ) );
 		}
 		index.writer.deleteDocuments(list.toArray(new Term[list.size()]));
 	}
 
-	String nextId(LuanState luan) throws LuanException, IOException {
-		return index.nextId(luan);
-	}
-
 	public void save_document(LuanState luan,LuanTable doc) throws LuanException, IOException {
 		if( doc.get(luan,"type")==null )
 			throw luan.exception("missing 'type' field");
-		String id = (String)doc.get(luan,"id");
+		Long id = (Long)doc.get(luan,"id");
 		if( id == null ) {
-			id = nextId(luan);
+			id = index.nextId(luan);
 			doc.put(luan,"id",id);
-			addDocument(luan,doc);
+			index.writer.addDocument(index.toLucene(luan,doc));
 		} else {
-			updateDocument(luan,index.newTerm("id",id),doc);
+			index.writer.updateDocument( term("id",id), index.toLucene(luan,doc) );
 		}
 	}