lucene全文检索入门实例
基本概念:

前期准备:
?
lucene-2.4.0
junit4.9
?
实例代码:
?
package com.ln.ydc.lucene.test;import java.io.File;import java.io.IOException;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.queryParser.MultiFieldQueryParser;import org.apache.lucene.queryParser.ParseException;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 com.ln.ydc.lucene.util.LuceneUtil;/** * 1.根据 数据源文件 创建索引库 * 2.根据 数据源文件所在的目录 创建索引库 * 3.根据索引搜索关键字 * * @author ydc * */public class HelloWorld {// 数据源文件路径String filePath = "D:\\logs\\lucene\\datasource\\eng_article.txt";// 索引库目录String indexPath = "D:\\logs\\lucene\\luceneIndex";// Directory directory = Directory.createOutput(indexPath);// 数据源目录String dirPath = "D:\\logs\\lucene\\datasource";// 词库分词器Analyzer analyzer = new StandardAnalyzer();// Analyzer analyzer = new MMAnalyzer();// 中文分词器/** * 根据数据源文件创建索引库 * * IndexWriter 是用来操作索引库的(增、删、改) */@Testpublic void createIndexByFile() throws Exception {Document doc = LuceneUtil.file2Document(filePath);// file-->docIndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);indexWriter.addDocument(doc);// optimize()方法是对索引进行优化indexWriter.optimize();indexWriter.close();}/** * 根据数据源文件目录创建索引库 * * @throws Exception */@Testpublic void createIndexByDir() throws Exception {File filesDir = new File(dirPath);IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);for (String filePath : filesDir.list()) {Document doc = LuceneUtil.file2Document(dirPath + "//" + filePath);indexWriter.addDocument(doc);}// optimize()方法是对索引进行优化indexWriter.optimize();indexWriter.close();}/** * 搜索 * * @throws ParseException * @throws IOException */@Testpublic void search() throws ParseException, IOException {String queryString = "村上春树";// 1.把要搜索的文本解析为QueryString[] 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 + "】条匹配结果");for (ScoreDoc scoreDoc : topDocs.scoreDocs) {int docSn = scoreDoc.doc; // 文档内部编号Document doc = indexSearcher.doc(docSn); // 根据编号取出相应的文档LuceneUtil.printDocumentInfo(doc); // 打印出文档信息}}}?
package com.ln.ydc.lucene.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.NumberTools;public class LuceneUtil {/** * 将一个具体的文件转换成document * 保存文件的如下信息: * name 文件名 * content 文件内容 * size 文件大小 * path文件路径 * * @param filePath * @return */public static Document file2Document(String filePath) {File file = new File(filePath);Document doc = new Document();doc.add(new Field("name", file.getName().trim(), Field.Store.YES, Field.Index.ANALYZED));doc.add(new Field("content", readFileContent(file), Field.Store.YES, Field.Index.ANALYZED));doc.add(new Field("size", NumberTools.longToString(file.length()), Field.Store.YES, Field.Index.ANALYZED));doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NO));return doc;}/** * 读取文件内容 * * @param file * @return */public static String readFileContent(File file) {try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));StringBuffer content = new StringBuffer();for (String line = null; (line = reader.readLine()) != null;) {if(line!=null && !"".equals(line=line.trim()))content.append(line).append("\n");}return content.toString();} catch (Exception e) {throw new RuntimeException(e);}}/** * <pre> * 获取 name 属性的值的两种方法: * 1.Field field = doc.getField("name"); * field.stringValue(); * 该方法已过时 * 2.doc.get("name"); * </pre> * * @param doc */public static void printDocumentInfo(Document doc) {System.out.println("--------------------------------------------");System.out.println("【name】 \t " + doc.get("name"));System.out.println("【content】 \t " + doc.get("content"));System.out.println("【size】 \t " + doc.get("size"));System.out.println("【path】 \t " + doc.get("path"));}/*public static void printDocumentInfo(QueryResult<Document> qr) {System.out.println("总共有【" + qr.getRecordCount() + "】条匹配结果");for(Document doc : qr.getRecordList()) {printDocumentInfo(doc);}}*/?}