1、引入Lucene相关的jar, 本实验采用的是如下版本,现在最新的版本为3.0
lucene-core-2.4.0.jar、lucene-analyzers-2.4.0.jar、lucene-highlighter-2.4.0.jar、je-analysis-1.5.3.jar
下载地址:, Maven库:
2、Lucene3.0分为网页抓取、分词器、高亮器、索引库等20几个模块, 具体参考~\lucene-3.0.0\contrib\*
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.index.IndexWriter.MaxFieldLength;
- import org.apache.lucene.queryParser.MultiFieldQueryParser;
- import org.apache.lucene.queryParser.QueryParser;
- import org.apache.lucene.search.Filter;
- 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.junit.Test;
-
- import cn.itcast.lucene.utils.File2DocumentUtils;
-
- public class HelloWorld {
- String filePath = "E:\\workspace\\LuceneDemo\\luceneDatasource\\IndexWriter addDocument's a javadoc.txt";
-
- String indexPath = "E:\\workspace\\LuceneDemo\\luceneIndex";
-
- Analyzer analyzer = new StandardAnalyzer();
-
-
-
-
-
-
- @Test
- public void createIndex() throws Exception {
-
- Document doc = File2DocumentUtils.file2Document(filePath);
-
-
- IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true,
- MaxFieldLength.LIMITED);
- indexWriter.addDocument(doc);
- indexWriter.close();
- }
-
-
-
-
-
-
- @Test
- public void search() throws Exception {
-
- String queryString = "adddocument";
-
-
- String[] fields = { "name", "content" };
- QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
- Query query = queryParser.parse(queryString);
-
-
- IndexSearcher indexSearcher = new IndexSearcher(indexPath);
- Filter filter = null;
- TopDocs topDocs = indexSearcher.search(query, filter, 10000);
- System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果");
-
-
- for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
- int docSn = scoreDoc.doc;
- Document doc = indexSearcher.doc(docSn);
- File2DocumentUtils.printDocumentInfo(doc);
- }
- }
- }
3、更多实例待续......
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/396733,如需转载请自行联系原作者