版本: 4.6
参考了网上相上的相关内容!先谢过了
单独新建一个类,引用Lucence 4.6的相关JAR包:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.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.util.Version;
/**
* Lucence test class 20150412
*
* @author Wang
*
*/
public class Test {
public class Constants {
public final static String INDEX_STORE_PATH = "F:\\lucene\\index"; // 索引的存放位置
}
/**
* 创建索引
*
* @param analyzer
* @throws Exception
*/
public static void createIndex(Analyzer analyzer) throws Exception {
Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,
analyzer);
IndexWriter iw = new IndexWriter(dire, iwc);
Test.addDoc(iw);
iw.close();
}
/**
* 动态添加Document
*
* @param iw
* @throws Exception
*/
public static void addDoc(IndexWriter iw) throws Exception {
Document doc = new Document();
doc.add(new TextField("content", "我爱祖国", Store.YES));
iw.addDocument(doc);
doc = new Document();
doc.add(new TextField("content", "我也爱你", Store.YES));
iw.addDocument(doc);
iw.commit();
}
/**
* 搜索
*
* @param query
* @throws Exception
*/
private static void search(Query query) throws Exception {
Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexReader ir = DirectoryReader.open(dire);
IndexSearcher is = new IndexSearcher(ir);
TopDocs td = is.search(query, 1000);
System.out.println("共为您查找到" + td.totalHits + "条结果");
ScoreDoc[] sds = td.scoreDocs;
for (ScoreDoc sd : sds) {
Document d = is.doc(sd.doc);
System.out
.println(d.get("content") + ":[" + d.get("content") + "]");
}
}
public static void main(String[] args) throws Exception, Exception {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
Test.createIndex(analyzer);
QueryParser parser = new QueryParser(Version.LUCENE_46, "content",
analyzer);
Query query = parser.parse("我");
Test.search(query);
Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,
analyzer);
IndexWriter iw = new IndexWriter(dire, iwc);
iw.deleteDocuments(query);
iw.commit();
iw.close();
}
}
以下代码用来删除已有的索引:
Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,
analyzer);
IndexWriter iw = new IndexWriter(dire, iwc);
iw.deleteDocuments(query);
iw.commit();
iw.close();