changeset 618:5e495e4e560b

add lucene indexed_only_fields
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 Jan 2016 01:24:10 -0700
parents e54c1646eed0
children cc3a68033179
files lucene/ext/sane-lucene-queryparser.jar lucene/src/luan/modules/lucene/Lucene.luan lucene/src/luan/modules/lucene/LuceneIndex.java
diffstat 3 files changed, 44 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
Binary file lucene/ext/sane-lucene-queryparser.jar has changed
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Wed Dec 30 02:15:01 2015 -0700
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Fri Jan 01 01:24:10 2016 -0700
@@ -27,6 +27,10 @@
 	index.dir = index_dir
 	local java_index = LuceneIndex.new(index_dir,default_type,default_fields)
 	index.indexed_fields = java_index.indexedFieldsMeta.newTable()
+
+	-- index.indexed_only_fields[type][field] = fn(doc)
+	index.indexed_only_fields = java_index.indexed_only_fields
+
 	index.to_string = java_index.to_string
 	index.backup = java_index.backup
 	index.advanced_search = java_index.advanced_search
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed Dec 30 02:15:01 2015 -0700
+++ b/lucene/src/luan/modules/lucene/LuceneIndex.java	Fri Jan 01 01:24:10 2016 -0700
@@ -10,6 +10,8 @@
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Set;
+import java.util.HashSet;
+import java.util.Collections;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 import java.util.zip.ZipOutputStream;
@@ -81,6 +83,7 @@
 	private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>();
 	private boolean isClosed = false;
 	private final MultiFieldParser mfp;
+	public final LuanTable indexed_only_fields = new LuanTable();
 
 	public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException {
 		mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields);
@@ -140,8 +143,32 @@
 	}
 
 	public void save(LuanState luan,LuanTable doc) throws LuanException, IOException {
-		if( doc.get(luan,"type")==null )
+		Set indexedOnlySet = new HashSet();
+		Object typeObj = doc.get(luan,"type");
+		if( typeObj==null )
 			throw new LuanException(luan,"missing 'type' field");
+		if( !(typeObj instanceof String) )
+			throw new LuanException(luan,"type must be string");
+		String type = (String)typeObj;
+		Object indexedOnlyObj = indexed_only_fields.get(luan,type);
+		if( indexedOnlyObj != null ) {
+			if( !(indexedOnlyObj instanceof LuanTable) )
+				throw new LuanException(luan,"indexed_only_fields elements must be tables");
+			LuanTable indexedOnly = (LuanTable)indexedOnlyObj;
+			for( Map.Entry<Object,Object> entry : indexedOnly.iterable(luan) ) {
+				Object key = entry.getKey();
+				if( !(key instanceof String) )
+					throw new LuanException(luan,"indexed_only_fields."+type+" entries must be strings");
+				String name = (String)key;
+				Object value = entry.getValue();
+				if( !(value instanceof LuanFunction) )
+					throw new LuanException(luan,"indexed_only_fields."+type+" values must be functions");
+				LuanFunction fn = (LuanFunction)value;
+				value = Luan.first(fn.call(luan,new Object[]{doc}));
+				doc.put(luan, name, value );
+				indexedOnlySet.add(name);
+			}
+		}
 		Object obj = doc.get(luan,"id");
 		Long id;
 		try {
@@ -156,9 +183,9 @@
 			if( id == null ) {
 				id = nextId(luan);
 				doc.put(luan,"id",id);
-				writer.addDocument(toLucene(luan,doc));
+				writer.addDocument(toLucene(luan,doc,indexedOnlySet));
 			} else {
-				writer.updateDocument( term("id",id), toLucene(luan,doc) );
+				writer.updateDocument( term("id",id), toLucene(luan,doc,indexedOnlySet) );
 			}
 			if(commit) writer.commit();
 		} finally {
@@ -184,7 +211,7 @@
 
 	private void initId(LuanState luan) throws LuanException, IOException {
 		TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1);
-
+/*
 		// tmp hack
 		if( td.totalHits == 0 ) {
 			td = searcher.search(new TermQuery(new Term("type index","next_id")),1);
@@ -197,7 +224,7 @@
 				writer.commit();
 			}
 		}
-
+*/
 		switch(td.totalHits) {
 		case 0:
 			break;  // do nothing
@@ -216,7 +243,7 @@
 			LuanTable doc = new LuanTable();
 			doc.rawPut( "type", "next_id" );
 			doc.rawPut( FLD_NEXT_ID, idLim );
-			writer.updateDocument(new Term("type","next_id"),toLucene(luan,doc));
+			writer.updateDocument(new Term("type","next_id"),toLucene(luan,doc,Collections.EMPTY_SET));
 		}
 		return id;
 	}
@@ -416,7 +443,7 @@
 
 
 
-	private Document toLucene(LuanState luan,LuanTable table) throws LuanException {
+	private Document toLucene(LuanState luan,LuanTable table,Set indexOnly) throws LuanException {
 		Set<String> indexed = mfp.fields.keySet();
 		Document doc = new Document();
 		for( Map.Entry<Object,Object> entry : table.iterable(luan) ) {
@@ -425,14 +452,15 @@
 				throw new LuanException(luan,"key must be string");
 			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, Field.Store.YES));
+						doc.add(new TextField(name, s, store));
 					} else {
-						doc.add(new StringField(name, s, Field.Store.YES));
+						doc.add(new StringField(name, s, store));
 					}
 				} else {
 					doc.add(new StoredField(name, s));
@@ -440,21 +468,21 @@
 			} else if( value instanceof Integer ) {
 				int i = (Integer)value;
 				if( indexed.contains(name) ) {
-					doc.add(new IntField(name, i, Field.Store.YES));
+					doc.add(new IntField(name, i, store));
 				} else {
 					doc.add(new StoredField(name, i));
 				}
 			} else if( value instanceof Long ) {
 				long i = (Long)value;
 				if( indexed.contains(name) ) {
-					doc.add(new LongField(name, i, Field.Store.YES));
+					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, Field.Store.YES));
+					doc.add(new DoubleField(name, i, store));
 				} else {
 					doc.add(new StoredField(name, i));
 				}