view lucene/src/luan/modules/lucene/LuceneDocument.java @ 544:c5a93767cc5c

lucene overhaul, untested
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Jun 2015 19:11:44 -0600
parents d9df6d6cb927
children
line wrap: on
line source

package luan.modules.lucene;

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.DoubleField;
import org.apache.lucene.util.BytesRef;
import luan.Luan;
import luan.LuanState;
import luan.LuanTable;
import luan.LuanException;


public class LuceneDocument {
	private LuceneDocument(String a) {}  // never

	static Document toLucene(LuanState luan,LuanTable table,Set<String> indexed) throws LuanException {
		Document doc = new Document();
		for( Map.Entry<Object,Object> entry : table.iterable(luan) ) {
			Object key = entry.getKey();
			if( !(key instanceof String) )
				throw luan.exception("key must be string");
			String name = (String)key;
			Object value = entry.getValue();
			if( value instanceof String ) {
				String s = (String)value;
				if( indexed.contains(name) ) {
					doc.add(new StringField(name, s, Field.Store.YES));
				} 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, Field.Store.YES));
				} 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));
				} 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));
				} else {
					doc.add(new StoredField(name, i));
				}
			} else if( value instanceof byte[] ) {
				byte[] b = (byte[])value;
				doc.add(new StoredField(name, b));
			} else
				throw luan.exception("invalid value type "+value.getClass()+"' for '"+name+"'");
		}
		return doc;
	}

	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;
			}
			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 luan.exception("invalid field type for "+ifld);
		}
		return table;
	}
}