Mercurial Hosting > luan
diff lucene/src/luan/modules/lucene/LuceneDocument.java @ 230:4438cb2e04d0
start lucene
git-svn-id: https://luan-java.googlecode.com/svn/trunk@231 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 30 Sep 2014 20:03:56 +0000 |
parents | |
children | ef39bc4d3f70 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lucene/src/luan/modules/lucene/LuceneDocument.java Tue Sep 30 20:03:56 2014 +0000 @@ -0,0 +1,98 @@ +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.util.BytesRef; +import luan.Luan; +import luan.LuanTable; + + +public class LuceneDocument { + // I assume there will be more flags later + public static final String INDEX = "index"; + + private LuceneDocument(String a) {} // never + + static Document toLucene(LuanTable table) { + Document doc = new Document(); + for( Map.Entry<Object,Object> entry : table ) { + Object key = entry.getKey(); + if( !(key instanceof String) ) + throw new IllegalArgumentException("key must be string"); + String name = (String)key; + Object value = entry.getValue(); + if( value == null ) + continue; + Set<String> flags = new HashSet<String>(); + String[] a = name.split(" +"); + for( int i=1; i<a.length; i++ ) { + flags.add(a[i]); + } + if( value instanceof String ) { + String s = (String)value; + if( flags.remove(INDEX) ) { + 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( flags.remove(INDEX) ) { + 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( flags.remove(INDEX) ) { + doc.add(new LongField(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 new IllegalArgumentException("invalid value type "+value.getClass()+"' for '"+name+"'"); + if( !flags.isEmpty() ) + throw new IllegalArgumentException("invalid flags "+flags+" in '"+name+"'"); + } + return doc; + } + + static LuanTable toTable(Document doc) { + if( doc==null ) + return null; + LuanTable table = Luan.newTable(); + for( IndexableField ifld : doc ) { + String name = ifld.name(); + BytesRef br = ifld.binaryValue(); + if( br != null ) { + table.put(name,br.bytes); + continue; + } + Number n = ifld.numericValue(); + if( n != null ) { + table.put(name,n); + continue; + } + String s = ifld.stringValue(); + if( s != null ) { + table.put(name,s); + continue; + } + throw new RuntimeException("invalid field type for "+ifld); + } + return table; + } +}