Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/LuceneSearcher.java @ 544:c5a93767cc5c
lucene overhaul, untested
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 12 Jun 2015 19:11:44 -0600 |
parents | 5d4a78c93383 |
children |
comparison
equal
deleted
inserted
replaced
543:9767da72545b | 544:c5a93767cc5c |
---|---|
19 import org.apache.lucene.search.Collector; | 19 import org.apache.lucene.search.Collector; |
20 import org.apache.lucene.search.TotalHitCountCollector; | 20 import org.apache.lucene.search.TotalHitCountCollector; |
21 import org.apache.lucene.search.Scorer; | 21 import org.apache.lucene.search.Scorer; |
22 import org.apache.lucene.search.MatchAllDocsQuery; | 22 import org.apache.lucene.search.MatchAllDocsQuery; |
23 import org.apache.lucene.index.AtomicReaderContext; | 23 import org.apache.lucene.index.AtomicReaderContext; |
24 import org.apache.lucene.queryparser.flexible.core.QueryNodeException; | 24 import sane.lucene.queryparser.ParseException; |
25 import luan.Luan; | 25 import luan.Luan; |
26 import luan.LuanState; | 26 import luan.LuanState; |
27 import luan.LuanTable; | 27 import luan.LuanTable; |
28 import luan.LuanFunction; | 28 import luan.LuanFunction; |
29 import luan.LuanJavaFunction; | 29 import luan.LuanJavaFunction; |
30 import luan.LuanException; | 30 import luan.LuanException; |
31 import luan.LuanRuntimeException; | 31 import luan.LuanRuntimeException; |
32 import luan.LuanMethod; | 32 import luan.LuanMethod; |
33 import luan.modules.Utils; | |
33 | 34 |
34 | 35 |
35 public final class LuceneSearcher { | 36 public final class LuceneSearcher { |
36 private final LuceneIndex index; | 37 private final LuceneIndex index; |
37 private final IndexSearcher searcher; | 38 final IndexSearcher searcher; |
38 | 39 |
39 LuceneSearcher(LuceneIndex index,IndexReader reader) { | 40 LuceneSearcher(LuceneIndex index,IndexReader reader) { |
40 this.index = index; | 41 this.index = index; |
41 this.searcher = new IndexSearcher(reader); | 42 this.searcher = new IndexSearcher(reader); |
42 } | 43 } |
44 // call in finally block | 45 // call in finally block |
45 void close() throws IOException { | 46 void close() throws IOException { |
46 searcher.getIndexReader().decRef(); | 47 searcher.getIndexReader().decRef(); |
47 } | 48 } |
48 | 49 |
49 LuanTable doc(LuanState luan,int docID) throws LuanException, IOException { | 50 private LuanTable doc(LuanState luan,int docID) throws LuanException, IOException { |
50 return index.toTable(luan,searcher.doc(docID)); | 51 return LuceneDocument.toTable(luan,searcher.doc(docID)); |
51 } | 52 } |
52 | 53 /* |
53 TopDocs search(Query query,int n) throws IOException { | 54 TopDocs search(Query query,int n) throws IOException { |
54 return searcher.search(query,n); | 55 return searcher.search(query,n); |
55 } | 56 } |
56 | 57 |
57 TopFieldDocs search(Query query,int n,Sort sort) throws IOException { | 58 TopFieldDocs search(Query query,int n,Sort sort) throws IOException { |
58 return searcher.search(query,n,sort); | 59 return searcher.search(query,n,sort); |
59 } | 60 } |
60 | 61 */ |
61 // luan | 62 // luan |
62 | 63 |
63 private static final LuanFunction nothingFn = new LuanFunction() { | 64 private static final LuanFunction nothingFn = new LuanFunction() { |
64 @Override public Object call(LuanState luan,Object[] args) { | 65 @Override public Object call(LuanState luan,Object[] args) { |
65 return LuanFunction.NOTHING; | 66 return LuanFunction.NOTHING; |
76 @Override public boolean acceptsDocsOutOfOrder() { | 77 @Override public boolean acceptsDocsOutOfOrder() { |
77 return true; | 78 return true; |
78 } | 79 } |
79 } | 80 } |
80 | 81 |
81 @LuanMethod public Object[] search( final LuanState luan, Object queryObj, Object nObj, Sort sort ) throws LuanException, IOException, QueryNodeException { | 82 @LuanMethod public Object[] search( final LuanState luan, String queryStr, Object nObj, String sortStr ) throws LuanException, IOException, ParseException { |
82 Query query; | 83 Utils.checkNotNull(luan,queryStr); |
83 if( queryObj instanceof Query ) { | 84 Query query = index.parseQuery(queryStr); |
84 query = (Query)queryObj; | |
85 } else if( queryObj instanceof String ) { | |
86 String s = (String)queryObj; | |
87 query = index.parse(s); | |
88 } else | |
89 throw luan.exception("bad argument #1 (string or Query expected, got "+Luan.type(queryObj)+")"); | |
90 if( nObj instanceof LuanFunction ) { | 85 if( nObj instanceof LuanFunction ) { |
91 final LuanFunction fn = (LuanFunction)nObj; | 86 final LuanFunction fn = (LuanFunction)nObj; |
92 Collector col = new MyCollector() { | 87 Collector col = new MyCollector() { |
93 @Override public void collect(int doc) { | 88 @Override public void collect(int doc) { |
94 try { | 89 try { |
117 if( n==0 ) { | 112 if( n==0 ) { |
118 TotalHitCountCollector thcc = new TotalHitCountCollector(); | 113 TotalHitCountCollector thcc = new TotalHitCountCollector(); |
119 searcher.search(query,thcc); | 114 searcher.search(query,thcc); |
120 return new Object[]{ nothingFn, 0, thcc.getTotalHits() }; | 115 return new Object[]{ nothingFn, 0, thcc.getTotalHits() }; |
121 } | 116 } |
117 Sort sort = sortStr==null ? null : index.parseSort(sortStr); | |
122 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort); | 118 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort); |
123 final ScoreDoc[] scoreDocs = td.scoreDocs; | 119 final ScoreDoc[] scoreDocs = td.scoreDocs; |
124 LuanFunction results = new LuanFunction() { | 120 LuanFunction results = new LuanFunction() { |
125 int i = 0; | 121 int i = 0; |
126 | 122 |
143 } | 139 } |
144 | 140 |
145 LuanTable table() { | 141 LuanTable table() { |
146 LuanTable tbl = new LuanTable(); | 142 LuanTable tbl = new LuanTable(); |
147 try { | 143 try { |
148 add( tbl, "search", LuanState.class, Object.class, Object.class, Sort.class ); | 144 add( tbl, "search", LuanState.class, String.class, Object.class, String.class ); |
149 } catch(NoSuchMethodException e) { | 145 } catch(NoSuchMethodException e) { |
150 throw new RuntimeException(e); | 146 throw new RuntimeException(e); |
151 } | 147 } |
152 return tbl; | 148 return tbl; |
153 } | 149 } |