Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/LuceneDocument.java @ 233:ef39bc4d3f70
basic lucene works
git-svn-id: https://luan-java.googlecode.com/svn/trunk@234 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 02 Oct 2014 02:58:55 +0000 |
parents | 4438cb2e04d0 |
children | 23b99a5039b5 |
comparison
equal
deleted
inserted
replaced
232:9ce18106f95a | 233:ef39bc4d3f70 |
---|---|
10 import org.apache.lucene.document.Field; | 10 import org.apache.lucene.document.Field; |
11 import org.apache.lucene.document.StoredField; | 11 import org.apache.lucene.document.StoredField; |
12 import org.apache.lucene.document.StringField; | 12 import org.apache.lucene.document.StringField; |
13 import org.apache.lucene.document.IntField; | 13 import org.apache.lucene.document.IntField; |
14 import org.apache.lucene.document.LongField; | 14 import org.apache.lucene.document.LongField; |
15 import org.apache.lucene.document.DoubleField; | |
15 import org.apache.lucene.util.BytesRef; | 16 import org.apache.lucene.util.BytesRef; |
16 import luan.Luan; | 17 import luan.Luan; |
18 import luan.LuanState; | |
17 import luan.LuanTable; | 19 import luan.LuanTable; |
20 import luan.LuanException; | |
18 | 21 |
19 | 22 |
20 public class LuceneDocument { | 23 public class LuceneDocument { |
21 // I assume there will be more flags later | 24 // I assume there will be more flags later |
22 public static final String INDEX = "index"; | 25 public static final String INDEX = "index"; |
23 | 26 |
24 private LuceneDocument(String a) {} // never | 27 private LuceneDocument(String a) {} // never |
25 | 28 |
26 static Document toLucene(LuanTable table) { | 29 static Document toLucene(LuanState luan,LuanTable table,Map<String,String> nameMap) throws LuanException { |
27 Document doc = new Document(); | 30 Document doc = new Document(); |
28 for( Map.Entry<Object,Object> entry : table ) { | 31 for( Map.Entry<Object,Object> entry : table ) { |
29 Object key = entry.getKey(); | 32 Object key = entry.getKey(); |
30 if( !(key instanceof String) ) | 33 if( !(key instanceof String) ) |
31 throw new IllegalArgumentException("key must be string"); | 34 throw luan.exception("key must be string"); |
32 String name = (String)key; | 35 String name = (String)key; |
33 Object value = entry.getValue(); | 36 Object value = entry.getValue(); |
34 if( value == null ) | 37 String newName = nameMap.get(name); |
35 continue; | 38 if( newName != null ) |
39 name = newName; | |
36 Set<String> flags = new HashSet<String>(); | 40 Set<String> flags = new HashSet<String>(); |
37 String[] a = name.split(" +"); | 41 String[] a = name.split(" +"); |
38 for( int i=1; i<a.length; i++ ) { | 42 for( int i=1; i<a.length; i++ ) { |
39 flags.add(a[i]); | 43 flags.add(a[i]); |
40 } | 44 } |
57 if( flags.remove(INDEX) ) { | 61 if( flags.remove(INDEX) ) { |
58 doc.add(new LongField(name, i, Field.Store.YES)); | 62 doc.add(new LongField(name, i, Field.Store.YES)); |
59 } else { | 63 } else { |
60 doc.add(new StoredField(name, i)); | 64 doc.add(new StoredField(name, i)); |
61 } | 65 } |
66 } else if( value instanceof Double ) { | |
67 double i = (Double)value; | |
68 if( flags.remove(INDEX) ) { | |
69 doc.add(new DoubleField(name, i, Field.Store.YES)); | |
70 } else { | |
71 doc.add(new StoredField(name, i)); | |
72 } | |
62 } else if( value instanceof byte[] ) { | 73 } else if( value instanceof byte[] ) { |
63 byte[] b = (byte[])value; | 74 byte[] b = (byte[])value; |
64 doc.add(new StoredField(name, b)); | 75 doc.add(new StoredField(name, b)); |
65 } else | 76 } else |
66 throw new IllegalArgumentException("invalid value type "+value.getClass()+"' for '"+name+"'"); | 77 throw luan.exception("invalid value type "+value.getClass()+"' for '"+name+"'"); |
67 if( !flags.isEmpty() ) | 78 if( !flags.isEmpty() ) |
68 throw new IllegalArgumentException("invalid flags "+flags+" in '"+name+"'"); | 79 throw luan.exception("invalid flags "+flags+" in '"+name+"'"); |
69 } | 80 } |
70 return doc; | 81 return doc; |
71 } | 82 } |
72 | 83 |
73 static LuanTable toTable(Document doc) { | 84 static LuanTable toTable(LuanState luan,Document doc,Map<String,String> nameMap) throws LuanException { |
74 if( doc==null ) | 85 if( doc==null ) |
75 return null; | 86 return null; |
76 LuanTable table = Luan.newTable(); | 87 LuanTable table = Luan.newTable(); |
77 for( IndexableField ifld : doc ) { | 88 for( IndexableField ifld : doc ) { |
78 String name = ifld.name(); | 89 String name = ifld.name(); |
90 String newName = nameMap.get(name); | |
91 if( newName != null ) | |
92 name = newName; | |
79 BytesRef br = ifld.binaryValue(); | 93 BytesRef br = ifld.binaryValue(); |
80 if( br != null ) { | 94 if( br != null ) { |
81 table.put(name,br.bytes); | 95 table.put(name,br.bytes); |
82 continue; | 96 continue; |
83 } | 97 } |
89 String s = ifld.stringValue(); | 103 String s = ifld.stringValue(); |
90 if( s != null ) { | 104 if( s != null ) { |
91 table.put(name,s); | 105 table.put(name,s); |
92 continue; | 106 continue; |
93 } | 107 } |
94 throw new RuntimeException("invalid field type for "+ifld); | 108 throw luan.exception("invalid field type for "+ifld); |
95 } | 109 } |
96 return table; | 110 return table; |
97 } | 111 } |
98 } | 112 } |