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
下载地址:https://olex.openlogic.com/, 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();
- /**
- * 创建索引
- *
- * IndexWriter 是用来操作(增、删、改)索引库的
- */
- @Test
- public void createIndex() throws Exception {
- // file --> doc
- Document doc = File2DocumentUtils.file2Document(filePath);
- // 建立索引
- IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true,
- MaxFieldLength.LIMITED);
- indexWriter.addDocument(doc);
- indexWriter.close();
- }
- /**
- * 搜索
- *
- * IndexSearcher 是用来在索引库中进行查询的
- */
- @Test
- public void search() throws Exception {
- // String queryString = "document";
- String queryString = "adddocument";
- // 1,把要搜索的文本解析为 Query
- String[] fields = { "name", "content" };
- QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
- Query query = queryParser.parse(queryString);
- // 2,进行查询
- IndexSearcher indexSearcher = new IndexSearcher(indexPath);
- Filter filter = null;
- TopDocs topDocs = indexSearcher.search(query, filter, 10000);
- System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果");
- // 3,打印结果
- 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,如需转载请自行联系原作者