標籤:view dir director baidu 代碼 gen 支援 建立 ima
從百度上知道的,Lucene是apache軟體基金會4 jakarta項目組的一個子項目,是一個開放原始碼的全文檢索索引引擎工具包,但它不是一個完整的全文檢索索引引擎,而是一個全文檢索索引引擎的架構,提供了完整的查詢引擎和索引引擎,部分文本分析引擎(英文與德文兩種西方語言)。Lucene的目的是為軟體開發人員提供一個簡單易用的工具包,以方便的在目標系統中實現全文檢索索引的功能,或者是以此為基礎建立起完整的全文檢索索引引擎。Lucene是一套用於全文檢索索引和搜尋的開源程式庫,由Apache軟體基金會支援和提供。Lucene提供了一個簡單卻強大的應用程式介面,能夠做全文索引和搜尋。在Java開發環境裡Lucene是一個成熟的免費開源工具。就其本身而言,Lucene是當前以及最近幾年最受歡迎的免費Java資訊檢索程式庫。人們經常提到資訊檢索程式庫,雖然與搜尋引擎有關,但不應該將資訊檢索程式庫與搜尋引擎相混淆。
用的是Lucene 5.5.0版本,http://archive.apache.org/dist/lucene/java/5.5.0/
一,下載後,需要找到Lucene需要的核心jar包
lucene-analyzers-common-5.5.0.jarlucene-core-5.5.0.jarlucene-queryparser-5.5.0.jar
找到核心jar包後,匯入到開發項目的lib下。
二、建立索引的步驟
1、建立Directory
2、建立IndexWriter
3、建立Document對象
4、往Document添加Field
5、通過IndexWriter添加文檔到索引中
三、執行個體開發
其中:FSDirectory.open(Paths.get("D://lucene//index")); 是將索引建立在自己電腦上硬碟上。
如果建立在記憶體中,就可以使用Directory directory = new RAMDirectory();//建立在記憶體中
/** * 建立索引 */ public void createIndex(){ //1,建立Directory Directory directory = null; //2,建立IndexWriter Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter indexWriter = null; try { directory = FSDirectory.open(Paths.get("D://lucene//index")); indexWriter = new IndexWriter(directory, config); //3,建立Document對象 Document doc = null; //4,位Document添加Field File f = new File("D://text"); for(File file:f.listFiles()){ doc = new Document(); @SuppressWarnings("deprecation") Field content = new Field("content", new FileReader(file)); @SuppressWarnings("deprecation") Field filename = new Field("filename", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED); @SuppressWarnings("deprecation") Field pathFile = new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED); doc.add(content); doc.add(filename); doc.add(pathFile); //5,通過IndexWriter添加文檔到索引中 indexWriter.addDocument(doc); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(indexWriter != null) try { indexWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
執行後,可以看到索引檔案:
OK,索引就建好了。
Lucene索引的初步建立