Lucene-3.0.0配置 java
1、Lucene開發環境配置 apache step1.Lucene開發包下載 學習 step2.Java開發環境配置 spa step3.Tomcat安裝 .net step4.Lucene開發環境配置 對象 解壓下載的lucene-3.0.0.zip,能夠看到lucene-core-3.0.0.jar和lucene-demos-3.0.0.jar這兩個文件,將其解壓(建議放在安裝jdk的lib文件夾內),並把路徑添加到環境變量的classpath。 索引 |
package com.sun.lucene; ip
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.document.FieldSelectorResult;
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.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.RAMDirectory;
import org.apache.lucene.util.Version;
///咱們建立一個簡單的類進行學習
///前提是咱們在使用要導入lucene jar 包
public class hellolucene {
// C建立索引
public void index() {
// 建立Directory
IndexWriter writer = null;
// Directory directory = new RAMDirectory();
try {
Directory directory = FSDirectory.open(new File(
"E:/Lucene/oneday/index0"));
// 建立 IndexWriter
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35));
writer = new IndexWriter(directory, iwc);
// 建立Document 對象
Document doc = null;
File f = new File("E:/Lucene/oneday/example");
for (File file : f.listFiles()) {
doc = new Document();
doc.add(new Field("content", new FileReader(file)));
doc.add(new Field("filename", file.getName(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
writer.addDocument(doc);
}
// / 爲document 對象添加Field
// / 經過indexwriter添加文檔索引
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer!= null)
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// /建立索引搜索
public void Searcher() {
// 建立Directory
Directory directory;
try {
directory = FSDirectory.open(new File("E:/Lucene/oneday/index0"));
// 建立 IndexReader 讀取要搜索的文件或相關信息
IndexReader reader = IndexReader.open(directory);
// 建立 根據IndexReader 建立 Indexsearcher
IndexSearcher searcher = new IndexSearcher(reader);
// 建立 Query
// 建立Parser ;來肯定搜索文件的內容 和搜索的文件域
QueryParser parser = new QueryParser(Version.LUCENE_35, "content",
new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse("hosts");
// 根據seacher 返回topdoc
TopDocs tds = searcher.search(query, 10);
// 根據 topdoc 獲取scordDoc
ScoreDoc[] sds = tds.scoreDocs;
for (ScoreDoc sd : sds) {
// 根據 seacher 和scoredDoc 獲取document 對象信息
Document d = searcher.doc(sd.doc);
// 根據Document 獲取相應的對象值
System.out.println(d.get("path") + "" + d.get("filename"));
}
} catch (Exception e) {
e.getStackTrace();
}
// 關閉Reader
}
}
public class Test{
@Test
public void Text(){
hellolucene ge = new hellolucene();
ge.index();
}
@Test public void TextSearcher(){ hellolucene ge = new hellolucene(); ge.index(); ge.Searcher(); } }