comparison lucene/src/luan/modules/lucene/LuceneSearcher.java @ 274:8afe9f2fdfec

AB testing, not fully tested git-svn-id: https://luan-java.googlecode.com/svn/trunk@275 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 10 Nov 2014 03:28:32 +0000
parents c5c60eca33dd
children 582e8db4cdb6
comparison
equal deleted inserted replaced
273:073044e3ac03 274:8afe9f2fdfec
14 import org.apache.lucene.search.SortField; 14 import org.apache.lucene.search.SortField;
15 import org.apache.lucene.search.ScoreDoc; 15 import org.apache.lucene.search.ScoreDoc;
16 import org.apache.lucene.search.TermQuery; 16 import org.apache.lucene.search.TermQuery;
17 import org.apache.lucene.search.BooleanQuery; 17 import org.apache.lucene.search.BooleanQuery;
18 import org.apache.lucene.search.BooleanClause; 18 import org.apache.lucene.search.BooleanClause;
19 import org.apache.lucene.search.Collector;
19 import org.apache.lucene.search.TotalHitCountCollector; 20 import org.apache.lucene.search.TotalHitCountCollector;
21 import org.apache.lucene.search.Scorer;
22 import org.apache.lucene.index.AtomicReaderContext;
20 import luan.Luan; 23 import luan.Luan;
21 import luan.LuanState; 24 import luan.LuanState;
22 import luan.LuanTable; 25 import luan.LuanTable;
23 import luan.LuanFunction; 26 import luan.LuanFunction;
24 import luan.LuanJavaFunction; 27 import luan.LuanJavaFunction;
25 import luan.LuanException; 28 import luan.LuanException;
29 import luan.LuanRuntimeException;
26 30
27 31
28 public final class LuceneSearcher { 32 public final class LuceneSearcher {
29 private final LuceneIndex index; 33 private final LuceneIndex index;
30 private final IndexSearcher searcher; 34 private final IndexSearcher searcher;
164 @Override public Object call(LuanState luan,Object[] args) { 168 @Override public Object call(LuanState luan,Object[] args) {
165 return LuanFunction.NOTHING; 169 return LuanFunction.NOTHING;
166 } 170 }
167 }; 171 };
168 172
169 public Object[] search( LuanState luan, LuanTable queryTbl, int n, LuanTable sortTbl ) throws LuanException, IOException { 173 private static abstract class MyCollector extends Collector {
174 @Override public void setScorer(Scorer scorer) {}
175 @Override public void setNextReader(AtomicReaderContext context) {}
176 @Override public boolean acceptsDocsOutOfOrder() {
177 return true;
178 }
179 }
180
181 public Object[] search( final LuanState luan, LuanTable queryTbl, Object nObj, LuanTable sortTbl ) throws LuanException, IOException {
170 Query query = query(queryTbl); 182 Query query = query(queryTbl);
171 if( query == null ) 183 if( query == null )
172 throw luan.exception("invalid query"); 184 throw luan.exception("invalid query");
185 if( nObj instanceof LuanFunction ) {
186 final LuanFunction fn = (LuanFunction)nObj;
187 Collector col = new MyCollector() {
188 @Override public void collect(int doc) {
189 try {
190 LuanTable docTbl = doc(luan,doc);
191 luan.call(fn,new Object[]{docTbl});
192 } catch(LuanException e) {
193 throw new LuanRuntimeException(e);
194 } catch(IOException e) {
195 throw new LuanRuntimeException(luan.exception(e));
196 }
197 }
198 };
199 try {
200 searcher.search(query,col);
201 } catch(LuanRuntimeException e) {
202 throw (LuanException)e.getCause();
203 }
204 return LuanFunction.NOTHING;
205 }
206 Integer nI = Luan.asInteger(nObj);
207 if( nI == null )
208 throw luan.exception("bad argument #2 (integer or function expected, got "+Luan.type(nObj)+")");
209 int n = nI;
173 if( n==0 ) { 210 if( n==0 ) {
174 TotalHitCountCollector thcc = new TotalHitCountCollector(); 211 TotalHitCountCollector thcc = new TotalHitCountCollector();
175 searcher.search(query,thcc); 212 searcher.search(query,thcc);
176 return new Object[]{ nothingFn, 0, thcc.getTotalHits() }; 213 return new Object[]{ nothingFn, 0, thcc.getTotalHits() };
177 } 214 }
199 } 236 }
200 237
201 LuanTable table() { 238 LuanTable table() {
202 LuanTable tbl = Luan.newTable(); 239 LuanTable tbl = Luan.newTable();
203 try { 240 try {
204 add( tbl, "search", LuanState.class, LuanTable.class, Integer.TYPE, LuanTable.class ); 241 add( tbl, "search", LuanState.class, LuanTable.class, Object.class, LuanTable.class );
205 } catch(NoSuchMethodException e) { 242 } catch(NoSuchMethodException e) {
206 throw new RuntimeException(e); 243 throw new RuntimeException(e);
207 } 244 }
208 return tbl; 245 return tbl;
209 } 246 }