comparison lucene/src/luan/modules/lucene/LuceneIndex.java @ 232:9ce18106f95a

more lucene work git-svn-id: https://luan-java.googlecode.com/svn/trunk@233 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 01 Oct 2014 06:55:14 +0000
parents 4438cb2e04d0
children ef39bc4d3f70
comparison
equal deleted inserted replaced
231:a35417bf493a 232:9ce18106f95a
86 86
87 private long id = 0; 87 private long id = 0;
88 private long idLim = 0; 88 private long idLim = 0;
89 private final int idBatch = 10; 89 private final int idBatch = 10;
90 90
91 private void initId() { 91 private void initId() throws IOException {
92 TopDocs td = searcher.search(new TermQuery(new Term(FLD_TYPE,"next_id")),1); 92 TopDocs td = searcher.search(new TermQuery(new Term(FLD_TYPE,"next_id")),1);
93 switch(td.totalHits) { 93 switch(td.totalHits) {
94 case 0: 94 case 0:
95 break; // do nothing 95 break; // do nothing
96 case 1: 96 case 1:
114 writer.updateDocument(new Term(FLD_TYPE,"next_id"),LuceneDocument.toLucene(doc)); 114 writer.updateDocument(new Term(FLD_TYPE,"next_id"),LuceneDocument.toLucene(doc));
115 } 115 }
116 return rtn; 116 return rtn;
117 } catch(IOException e) { 117 } catch(IOException e) {
118 throw new RuntimeException(e); 118 throw new RuntimeException(e);
119 }
120 }
121
122 public LuanTable getDocument(String id) {
123 return getDocument(new Term(LuceneWriter.FLD_ID,id));
124 }
125
126 public LuanTable getDocument(Term term) {
127 LuceneSearcher searcher = openSearcher();
128 try {
129 TopDocs td = searcher.search(new TermQuery(term),1);
130 switch(td.totalHits) {
131 case 0:
132 return null;
133 case 1:
134 return searcher.doc(td.scoreDocs[0].doc);
135 default:
136 throw new RuntimeException();
137 }
138 } finally {
139 searcher.close();
140 } 119 }
141 } 120 }
142 121
143 122
144 public void backup(String zipFile) { 123 public void backup(String zipFile) {
178 } finally { 157 } finally {
179 writer.close(); 158 writer.close();
180 } 159 }
181 } 160 }
182 161
162 public void Searcher(LuanState luan,LuanFunction fn) throws LuanException, IOException {
163 LuceneSearcher searcher = openSearcher();
164 try {
165 luan.call( fn, new Object[]{searcher.table()} );
166 } finally {
167 searcher.close();
168 }
169 }
170
183 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 171 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
184 t.put( method, new LuanJavaFunction(LuceneIndex.class.getMethod(method,parameterTypes),this) ); 172 t.put( method, new LuanJavaFunction(LuceneIndex.class.getMethod(method,parameterTypes),this) );
185 } 173 }
186 174
187 public LuanTable table() { 175 public LuanTable table() {
188 LuanTable tbl = Luan.newTable(); 176 LuanTable tbl = Luan.newTable();
189 try { 177 try {
190 add( tbl, "to_string" ); 178 add( tbl, "to_string" );
191 add( tbl, "backup", String.class ); 179 add( tbl, "backup", String.class );
180 add( tbl, "Writer", LuanState.class, LuanFunction.class );
181 add( tbl, "Searcher", LuanState.class, LuanFunction.class );
192 } catch(NoSuchMethodException e) { 182 } catch(NoSuchMethodException e) {
193 throw new RuntimeException(e); 183 throw new RuntimeException(e);
194 } 184 }
195 return tbl; 185 return tbl;
196 } 186 }