Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/LuceneWriter.java @ 544:c5a93767cc5c
lucene overhaul, untested
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 12 Jun 2015 19:11:44 -0600 |
parents | d9df6d6cb927 |
children | ddcd4296107a |
comparison
equal
deleted
inserted
replaced
543:9767da72545b | 544:c5a93767cc5c |
---|---|
5 import java.util.Set; | 5 import java.util.Set; |
6 import java.util.List; | 6 import java.util.List; |
7 import java.util.ArrayList; | 7 import java.util.ArrayList; |
8 import org.apache.lucene.index.IndexableField; | 8 import org.apache.lucene.index.IndexableField; |
9 import org.apache.lucene.index.Term; | 9 import org.apache.lucene.index.Term; |
10 import org.apache.lucene.util.BytesRef; | |
11 import org.apache.lucene.util.NumericUtils; | |
10 import luan.Luan; | 12 import luan.Luan; |
11 import luan.LuanState; | 13 import luan.LuanState; |
12 import luan.LuanTable; | 14 import luan.LuanTable; |
13 import luan.LuanJavaFunction; | 15 import luan.LuanJavaFunction; |
14 import luan.LuanException; | 16 import luan.LuanException; |
29 | 31 |
30 void commit() throws IOException { | 32 void commit() throws IOException { |
31 index.writer.commit(); | 33 index.writer.commit(); |
32 } | 34 } |
33 | 35 |
34 void addDocument(LuanState luan,LuanTable doc) throws LuanException, IOException { | 36 private Term term(String key,int value) { |
35 index.writer.addDocument(index.toLucene(luan,doc)); | 37 BytesRef br = new BytesRef(); |
38 NumericUtils.intToPrefixCoded(value,0,br); | |
39 return new Term(key,br); | |
36 } | 40 } |
37 | 41 |
38 void updateDocument(LuanState luan,Term term,LuanTable doc) throws LuanException, IOException { | 42 private Term term(String key,long value) { |
39 index.writer.updateDocument(term,index.toLucene(luan,doc)); | 43 BytesRef br = new BytesRef(); |
44 NumericUtils.longToPrefixCoded(value,0,br); | |
45 return new Term(key,br); | |
46 } | |
47 | |
48 private Term term(LuanState luan,String key,Object value) throws LuanException { | |
49 if( value instanceof String ) | |
50 return new Term( key, (String)value ); | |
51 if( value instanceof Integer ) | |
52 return term( key, (Integer)value ); | |
53 if( value instanceof Long ) | |
54 return term( key, (Long)value ); | |
55 if( value instanceof Float ) | |
56 return term( key, NumericUtils.floatToSortableInt((Float)value) ); | |
57 if( value instanceof Double ) | |
58 return term( key, NumericUtils.doubleToSortableLong((Double)value) ); | |
59 throw luan.exception("invalid value type '"+value.getClass().getSimpleName()+"' for key '"+key+"'"); | |
40 } | 60 } |
41 | 61 |
42 public void delete_documents(LuanState luan,LuanTable tblTerms) throws LuanException, IOException { | 62 public void delete_documents(LuanState luan,LuanTable tblTerms) throws LuanException, IOException { |
43 List<Term> list = new ArrayList<Term>(); | 63 List<Term> list = new ArrayList<Term>(); |
44 for( Map.Entry<Object,Object> entry : tblTerms.iterable(luan) ) { | 64 for( Map.Entry<Object,Object> entry : tblTerms.iterable(luan) ) { |
45 Object key = entry.getKey(); | 65 Object key = entry.getKey(); |
46 Object value = entry.getValue(); | 66 Object value = entry.getValue(); |
47 if( !(key instanceof String) ) | 67 if( !(key instanceof String) ) |
48 throw luan.exception("key must be a string but got "+key.getClass().getSimpleName()); | 68 throw luan.exception("key must be a string but got "+key.getClass().getSimpleName()); |
49 if( !(value instanceof String) ) | 69 list.add( term( luan, (String)key, value ) ); |
50 throw luan.exception("value must be a string but got "+value.getClass().getSimpleName()); | |
51 list.add( index.newTerm( (String)key, (String)value ) ); | |
52 } | 70 } |
53 index.writer.deleteDocuments(list.toArray(new Term[list.size()])); | 71 index.writer.deleteDocuments(list.toArray(new Term[list.size()])); |
54 } | |
55 | |
56 String nextId(LuanState luan) throws LuanException, IOException { | |
57 return index.nextId(luan); | |
58 } | 72 } |
59 | 73 |
60 public void save_document(LuanState luan,LuanTable doc) throws LuanException, IOException { | 74 public void save_document(LuanState luan,LuanTable doc) throws LuanException, IOException { |
61 if( doc.get(luan,"type")==null ) | 75 if( doc.get(luan,"type")==null ) |
62 throw luan.exception("missing 'type' field"); | 76 throw luan.exception("missing 'type' field"); |
63 String id = (String)doc.get(luan,"id"); | 77 Long id = (Long)doc.get(luan,"id"); |
64 if( id == null ) { | 78 if( id == null ) { |
65 id = nextId(luan); | 79 id = index.nextId(luan); |
66 doc.put(luan,"id",id); | 80 doc.put(luan,"id",id); |
67 addDocument(luan,doc); | 81 index.writer.addDocument(index.toLucene(luan,doc)); |
68 } else { | 82 } else { |
69 updateDocument(luan,index.newTerm("id",id),doc); | 83 index.writer.updateDocument( term("id",id), index.toLucene(luan,doc) ); |
70 } | 84 } |
71 } | 85 } |
72 | 86 |
73 // luan | 87 // luan |
74 | 88 |