comparison 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
comparison
equal deleted inserted replaced
229:2a54cb7d1cf4 230:4438cb2e04d0
1 package luan.modules.lucene;
2
3 import java.util.Map;
4 import java.util.HashMap;
5 import java.util.Set;
6 import java.util.HashSet;
7 import java.util.Arrays;
8 import org.apache.lucene.document.Document;
9 import org.apache.lucene.index.IndexableField;
10 import org.apache.lucene.document.Field;
11 import org.apache.lucene.document.StoredField;
12 import org.apache.lucene.document.StringField;
13 import org.apache.lucene.document.IntField;
14 import org.apache.lucene.document.LongField;
15 import org.apache.lucene.util.BytesRef;
16 import luan.Luan;
17 import luan.LuanTable;
18
19
20 public class LuceneDocument {
21 // I assume there will be more flags later
22 public static final String INDEX = "index";
23
24 private LuceneDocument(String a) {} // never
25
26 static Document toLucene(LuanTable table) {
27 Document doc = new Document();
28 for( Map.Entry<Object,Object> entry : table ) {
29 Object key = entry.getKey();
30 if( !(key instanceof String) )
31 throw new IllegalArgumentException("key must be string");
32 String name = (String)key;
33 Object value = entry.getValue();
34 if( value == null )
35 continue;
36 Set<String> flags = new HashSet<String>();
37 String[] a = name.split(" +");
38 for( int i=1; i<a.length; i++ ) {
39 flags.add(a[i]);
40 }
41 if( value instanceof String ) {
42 String s = (String)value;
43 if( flags.remove(INDEX) ) {
44 doc.add(new StringField(name, s, Field.Store.YES));
45 } else {
46 doc.add(new StoredField(name, s));
47 }
48 } else if( value instanceof Integer ) {
49 int i = (Integer)value;
50 if( flags.remove(INDEX) ) {
51 doc.add(new IntField(name, i, Field.Store.YES));
52 } else {
53 doc.add(new StoredField(name, i));
54 }
55 } else if( value instanceof Long ) {
56 long i = (Long)value;
57 if( flags.remove(INDEX) ) {
58 doc.add(new LongField(name, i, Field.Store.YES));
59 } else {
60 doc.add(new StoredField(name, i));
61 }
62 } else if( value instanceof byte[] ) {
63 byte[] b = (byte[])value;
64 doc.add(new StoredField(name, b));
65 } else
66 throw new IllegalArgumentException("invalid value type "+value.getClass()+"' for '"+name+"'");
67 if( !flags.isEmpty() )
68 throw new IllegalArgumentException("invalid flags "+flags+" in '"+name+"'");
69 }
70 return doc;
71 }
72
73 static LuanTable toTable(Document doc) {
74 if( doc==null )
75 return null;
76 LuanTable table = Luan.newTable();
77 for( IndexableField ifld : doc ) {
78 String name = ifld.name();
79 BytesRef br = ifld.binaryValue();
80 if( br != null ) {
81 table.put(name,br.bytes);
82 continue;
83 }
84 Number n = ifld.numericValue();
85 if( n != null ) {
86 table.put(name,n);
87 continue;
88 }
89 String s = ifld.stringValue();
90 if( s != null ) {
91 table.put(name,s);
92 continue;
93 }
94 throw new RuntimeException("invalid field type for "+ifld);
95 }
96 return table;
97 }
98 }