Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
543:9767da72545b | 544:c5a93767cc5c |
---|---|
19 import luan.LuanTable; | 19 import luan.LuanTable; |
20 import luan.LuanException; | 20 import luan.LuanException; |
21 | 21 |
22 | 22 |
23 public class LuceneDocument { | 23 public class LuceneDocument { |
24 // I assume there will be more flags later | |
25 public static final String INDEX = "index"; | |
26 | |
27 private LuceneDocument(String a) {} // never | 24 private LuceneDocument(String a) {} // never |
28 | 25 |
29 static Document toLucene(LuanState luan,LuanTable table,Map<String,String> nameMap) throws LuanException { | 26 static Document toLucene(LuanState luan,LuanTable table,Set<String> indexed) throws LuanException { |
30 Document doc = new Document(); | 27 Document doc = new Document(); |
31 for( Map.Entry<Object,Object> entry : table.iterable(luan) ) { | 28 for( Map.Entry<Object,Object> entry : table.iterable(luan) ) { |
32 Object key = entry.getKey(); | 29 Object key = entry.getKey(); |
33 if( !(key instanceof String) ) | 30 if( !(key instanceof String) ) |
34 throw luan.exception("key must be string"); | 31 throw luan.exception("key must be string"); |
35 String name = (String)key; | 32 String name = (String)key; |
36 Object value = entry.getValue(); | 33 Object value = entry.getValue(); |
37 String newName = nameMap.get(name); | |
38 if( newName != null ) | |
39 name = newName; | |
40 Set<String> flags = new HashSet<String>(); | |
41 String[] a = name.split(" +"); | |
42 for( int i=1; i<a.length; i++ ) { | |
43 flags.add(a[i]); | |
44 } | |
45 if( value instanceof String ) { | 34 if( value instanceof String ) { |
46 String s = (String)value; | 35 String s = (String)value; |
47 if( flags.remove(INDEX) ) { | 36 if( indexed.contains(name) ) { |
48 doc.add(new StringField(name, s, Field.Store.YES)); | 37 doc.add(new StringField(name, s, Field.Store.YES)); |
49 } else { | 38 } else { |
50 doc.add(new StoredField(name, s)); | 39 doc.add(new StoredField(name, s)); |
51 } | 40 } |
52 } else if( value instanceof Integer ) { | 41 } else if( value instanceof Integer ) { |
53 int i = (Integer)value; | 42 int i = (Integer)value; |
54 if( flags.remove(INDEX) ) { | 43 if( indexed.contains(name) ) { |
55 doc.add(new IntField(name, i, Field.Store.YES)); | 44 doc.add(new IntField(name, i, Field.Store.YES)); |
56 } else { | 45 } else { |
57 doc.add(new StoredField(name, i)); | 46 doc.add(new StoredField(name, i)); |
58 } | 47 } |
59 } else if( value instanceof Long ) { | 48 } else if( value instanceof Long ) { |
60 long i = (Long)value; | 49 long i = (Long)value; |
61 if( flags.remove(INDEX) ) { | 50 if( indexed.contains(name) ) { |
62 doc.add(new LongField(name, i, Field.Store.YES)); | 51 doc.add(new LongField(name, i, Field.Store.YES)); |
63 } else { | 52 } else { |
64 doc.add(new StoredField(name, i)); | 53 doc.add(new StoredField(name, i)); |
65 } | 54 } |
66 } else if( value instanceof Double ) { | 55 } else if( value instanceof Double ) { |
67 double i = (Double)value; | 56 double i = (Double)value; |
68 if( flags.remove(INDEX) ) { | 57 if( indexed.contains(name) ) { |
69 doc.add(new DoubleField(name, i, Field.Store.YES)); | 58 doc.add(new DoubleField(name, i, Field.Store.YES)); |
70 } else { | 59 } else { |
71 doc.add(new StoredField(name, i)); | 60 doc.add(new StoredField(name, i)); |
72 } | 61 } |
73 } else if( value instanceof byte[] ) { | 62 } else if( value instanceof byte[] ) { |
74 byte[] b = (byte[])value; | 63 byte[] b = (byte[])value; |
75 doc.add(new StoredField(name, b)); | 64 doc.add(new StoredField(name, b)); |
76 } else | 65 } else |
77 throw luan.exception("invalid value type "+value.getClass()+"' for '"+name+"'"); | 66 throw luan.exception("invalid value type "+value.getClass()+"' for '"+name+"'"); |
78 if( !flags.isEmpty() ) | |
79 throw luan.exception("invalid flags "+flags+" in '"+name+"'"); | |
80 } | 67 } |
81 return doc; | 68 return doc; |
82 } | 69 } |
83 | 70 |
84 static LuanTable toTable(LuanState luan,Document doc,Map<String,String> nameMap) throws LuanException { | 71 static LuanTable toTable(LuanState luan,Document doc) throws LuanException { |
85 if( doc==null ) | 72 if( doc==null ) |
86 return null; | 73 return null; |
87 LuanTable table = new LuanTable(); | 74 LuanTable table = new LuanTable(); |
88 for( IndexableField ifld : doc ) { | 75 for( IndexableField ifld : doc ) { |
89 String name = ifld.name(); | 76 String name = ifld.name(); |
90 String newName = nameMap.get(name); | |
91 if( newName != null ) | |
92 name = newName; | |
93 BytesRef br = ifld.binaryValue(); | 77 BytesRef br = ifld.binaryValue(); |
94 if( br != null ) { | 78 if( br != null ) { |
95 table.rawPut(name,br.bytes); | 79 table.rawPut(name,br.bytes); |
96 continue; | 80 continue; |
97 } | 81 } |