hits這個類相當於一個List<Document>,至少我是這麼認為的。 可能當中的Doc已經被處理過了。
在進階的lucene jar版本中,可能被刪除了這個類了。
Hits中的函數:
| Method Summary |
Document |
doc(int n)
Returns the stored fields of the nth document in this set. |
int |
id(int n)
Returns the id for the nth document in this set. |
int |
length()
Returns the total number of hits available in this set. |
float |
score(int n)
Returns the score for the nth document in this set. |
代碼:
<br />import org.apache.lucene.analysis.standard.StandardAnalyzer;<br />import org.apache.lucene.document.Document;<br />import org.apache.lucene.document.Field;<br />import org.apache.lucene.index.IndexWriter;<br />import org.apache.lucene.index.Term;<br />import org.apache.lucene.search.Hits;<br />import org.apache.lucene.search.IndexSearcher;<br />import org.apache.lucene.search.PrefixQuery;</p><p>/**<br /> * 此次測試使用的是Lucene1.4.3<br /> * @author 李晗<br /> *<br /> */<br />public class Test {<br />//http://lucene.apache.org/java/1_4_3/api/<br /> public static void main(String[] args) throws Exception {<br /> // 產生Document對象,下同<br /> Document doc1 = new Document();<br /> // 添加new Fieldnamenew Field欄位的內容,下同<br /> //Field(String name, String string, boolean store, boolean index, boolean token)<br /> doc1.add(new Field("name", "lihan",true,true,true));<br /> // 添加new Fieldtitlenew Field欄位的內容,下同<br /> doc1.add(new Field("title", "lihan", true,true,true));<br /> Document doc2 = new Document();<br /> doc2.add(new Field("name", "amen", true,true,true));<br /> doc2.add(new Field("title", "amen", true,true,true));<br /> Document doc3 = new Document();<br /> doc3.add(new Field("name", "Wanglihong", true,true,true));<br /> doc3.add(new Field("title", "shoushou", true,true,true));<br /> Document doc4 = new Document();<br /> doc4.add(new Field("name", "Zhoujielun", true,true,true));<br /> doc4.add(new Field("title", "gongshang", true,true,true));<br /> // 產生索引書寫器<br /> //IndexWriter(String path, Analyzer a, boolean create) Constructs an IndexWriter for the index in path.<br /> IndexWriter writer = new IndexWriter("d://index//testPrefix", new StandardAnalyzer(), true);<br /> // 設定為混合索引模式<br /> writer.setUseCompoundFile(true);<br /> // 依次將文檔添加到索引中<br /> writer.addDocument(doc1);<br /> writer.addDocument(doc2);<br /> writer.addDocument(doc3);<br /> writer.addDocument(doc4);<br /> // 關閉索引書寫器<br /> writer.close();<br /> // 產生索引搜尋器對象<br /> IndexSearcher searcher = new IndexSearcher("d://index//testPrefix");<br /> // 構造詞條<br /> Term pre1 = new Term("name", "l");<br /> Term pre2 = new Term("name", "zh");<br /> Term pre3 = new Term("name", "a");<br /> // 用於儲存檢索結果<br /> Hits hits = null;<br /> // 產生PrefixQuery類型的對象,初始化為null<br /> PrefixQuery query = null;<br /> query = new PrefixQuery(pre1);<br /> // 開始第一次檢索,並返回檢索結果<br /> hits = searcher.search(query);<br /> // 輸出相應的檢索結果<br /> printResult(hits, "首碼為'l'的文檔");<br /> query = new PrefixQuery(pre2);<br /> // 開始第二次檢索,並返回檢索結果<br /> hits = searcher.search(query);<br /> // 輸出相應的檢索結果<br /> printResult(hits, "首碼為'zh'的文檔");<br /> query = new PrefixQuery(pre3);<br /> // 開始第二次檢索,並返回檢索結果<br /> hits = searcher.search(query);<br /> // 輸出相應的檢索結果<br /> printResult(hits, "首碼為'a'的文檔");<br /> } </p><p> public static void printResult(Hits hits, String key) throws Exception {<br /> System.out.println("尋找 /"" + key + "/" :");<br /> if (hits != null) {<br /> if (hits.length() == 0) {<br /> System.out.println("沒有找到任何結果");<br /> System.out.println();<br /> } else {<br /> System.out.print("找到");<br /> for (int i = 0; i < hits.length(); i++) {<br /> // 取得文檔<br /> Document d = hits.doc(i);<br /> // 取得new Fieldtitlenew Field欄位的內容<br /> String dname = d.get("title");<br /> System.out.print(dname + " ");<br /> }<br /> System.out.println();<br /> System.out.println();<br /> }<br /> }<br /> }<br />}<br />
輸出::
尋找 "首碼為'l'的文檔" :
找到lihan
尋找 "首碼為'zh'的文檔" :
找到gongshang
尋找 "首碼為'a'的文檔" :
找到amen