lucene 学习一
生活随笔
收集整理的這篇文章主要介紹了
lucene 学习一
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
索引工具的三部分
1.索引部分
2.分詞部分
3.搜索部分
?
查看索引的工具:luke ? java -jar fileName.jar
?
目標:為文件夾的所有的文檔生成索引并搜索它
package com.lucene;import java.io.File; import java.io.FileReader; import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.Version;// lucene 使用的版本為 3.5 public class HelloLucene {public static void main(String[] args) {HelloLucene hl = new HelloLucene();hl.index();hl.search();}/*** 建立文檔索引*/public void index() {IndexWriter writer = null;try {//1.創(chuàng)建Director(確定索引建立的位置) // Directory directory = new RAMDirectory(); //在內存中建立Directory directory = FSDirectory.open(new File("d:/index_01"));//2.通過 IndexWriter 寫索引IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));writer = new IndexWriter(directory, iwc);//3.創(chuàng)建 Document 對象Document doc = null;File f = new File("G:/lucene/");for(File file : f.listFiles()){//4.為文檔 添加Field (文檔的每個屬性比如名稱可以稱之為文檔的一個Field)doc = new Document();doc.add(new Field("content", new FileReader(file)));doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));//5.通過IndexWrite添加文檔到索引中 writer.addDocument(doc);// 在索引庫沒有建立并且沒有索引文件的時候首先要commit一下讓他建立一個 索引庫的版本信息 writer.commit();}} catch (CorruptIndexException e) {e.printStackTrace();} catch (LockObtainFailedException e) {e.printStackTrace();} catch (IOException e) {try {if(writer != null) writer.close();} catch (CorruptIndexException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();}e.printStackTrace();}}/*** 搜索*/public void search(){try {//1.創(chuàng)建DirectoryDirectory directory = FSDirectory.open(new File("d:/index_01"));//2.創(chuàng)建IndexReaderIndexReader reader = IndexReader.open(directory);//3.根據(jù)IndexReader創(chuàng)建IndexSearcherIndexSearcher searcher = new IndexSearcher(reader);//4.創(chuàng)建搜索的的QueryQueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));//要搜索的內容Query query = parser.parse("document");//5.根據(jù)searcher搜索并返回TopDocsTopDocs tds = searcher.search(query, 100);//6.根據(jù)TopDocs獲取scoreDocs對象ScoreDoc[] sds = tds.scoreDocs;for(ScoreDoc sd:sds){//7.根據(jù)Search和ScoreDoc對象獲取具體的Document對象Document d = searcher.doc(sd.doc);//8.根據(jù)document對象獲取需要的值 System.out.println(d.get("filename")+"|"+d.get("path"));}System.out.println(sds.length); } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (ParseException e) {// TODO Auto-generated catch block e.printStackTrace();}//2. }}?
總結
以上是生活随笔為你收集整理的lucene 学习一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视图、存储过程、函数、游标、触发器使用
- 下一篇: Android使用SAX解析XML(6)