Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/LuceneSearcher.java @ 230:4438cb2e04d0
start lucene
git-svn-id: https://luan-java.googlecode.com/svn/trunk@231 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 30 Sep 2014 20:03:56 +0000 |
parents | |
children | 9ce18106f95a |
comparison
equal
deleted
inserted
replaced
229:2a54cb7d1cf4 | 230:4438cb2e04d0 |
---|---|
1 package luan.modules.lucene; | |
2 | |
3 import java.io.IOException; | |
4 import java.util.Iterator; | |
5 import java.util.NoSuchElementException; | |
6 import org.apache.lucene.index.IndexReader; | |
7 import org.apache.lucene.document.Document; | |
8 import org.apache.lucene.search.IndexSearcher; | |
9 import org.apache.lucene.search.Query; | |
10 import org.apache.lucene.search.TopDocs; | |
11 import org.apache.lucene.search.TopFieldDocs; | |
12 import org.apache.lucene.search.Sort; | |
13 import org.apache.lucene.search.ScoreDoc; | |
14 import luan.LuanTable; | |
15 | |
16 | |
17 public final class LuceneSearcher { | |
18 private final IndexSearcher searcher; | |
19 | |
20 LuceneSearcher(IndexReader reader) { | |
21 this.searcher = new IndexSearcher(reader); | |
22 } | |
23 | |
24 // call in finally block | |
25 public void close() { | |
26 try { | |
27 searcher.getIndexReader().decRef(); | |
28 } catch(IOException e) { | |
29 throw new RuntimeException(e); | |
30 } | |
31 } | |
32 | |
33 private Document rawDoc(int docID) { | |
34 try { | |
35 return searcher.doc(docID); | |
36 } catch(IOException e) { | |
37 throw new RuntimeException(e); | |
38 } | |
39 } | |
40 | |
41 public LuanTable doc(int docID) { | |
42 return LuceneDocument.toTable(rawDoc(docID)); | |
43 } | |
44 | |
45 public TopDocs search(Query query,int n) { | |
46 try { | |
47 return searcher.search(query,n); | |
48 } catch(IOException e) { | |
49 throw new RuntimeException(e); | |
50 } | |
51 } | |
52 | |
53 public TopFieldDocs search(Query query,int n,Sort sort) { | |
54 try { | |
55 return searcher.search(query,n,sort); | |
56 } catch(IOException e) { | |
57 throw new RuntimeException(e); | |
58 } | |
59 } | |
60 | |
61 public Iterable<LuanTable> docs(TopDocs td) { | |
62 final ScoreDoc[] scoreDocs = td.scoreDocs; | |
63 return new Iterable<LuanTable>() { | |
64 public Iterator<LuanTable> iterator() { | |
65 return new Iterator<LuanTable>() { | |
66 private int i = 0; | |
67 | |
68 public boolean hasNext() { | |
69 return i < scoreDocs.length; | |
70 } | |
71 | |
72 public LuanTable next() { | |
73 if( !hasNext() ) | |
74 throw new NoSuchElementException(); | |
75 return doc(scoreDocs[i++].doc); | |
76 } | |
77 | |
78 public void remove() { | |
79 throw new UnsupportedOperationException(); | |
80 } | |
81 }; | |
82 } | |
83 }; | |
84 } | |
85 } |