package com.cngine.Index; import java.io.File; import java.io.IOException; import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; 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.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; import com.cngine.utils.File2Docuement; public class IndexDemo { private String sourceDir = "D://sourceDir//Baidu studies i'm.txt"; private String indexDir = "D://indexDir"; Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); /*** * 创建索引 */ @Test public void CreateIndex() throws Exception { // 操作索引库 (增删改) // MaxFieldLength.LIMITED = new MaxFieldLength(10000); // 索引前一万个10000 IndexWriter index = new IndexWriter(FSDirectory .open(new File(indexDir)), analyzer, true, MaxFieldLength.LIMITED); //把文件转换成一个Document文档 Document doc = File2Docuement.ToDocument(sourceDir); //在索引文件中添加Document文档 index.addDocument(doc); //刷新缓存里面的数据到索引库 //index.commit(); //优化 index.optimize(); // 释放资源 index.close(); } /** * 搜索索引 */ @Test public void SearchIndex() { String searchString = "baidu"; Date BeginDate = new Date(); try { // 1.根据索引的目录获取 搜索 索引器 IndexSearcher indexSearcher = new IndexSearcher(FSDirectory .open(new File(indexDir))); // 2.把要搜索的文本 解析成Query对象 //要搜索的字段 String[] fields = { "name", "content" }; //Query对象转换器 QueryParser parser = new MultiFieldQueryParser( Version.LUCENE_CURRENT, fields, analyzer); //根据Query转换器把要搜索的 字符串转换成 Query对象 Query query = parser.parse(searchString); // 3.过滤器,过滤掉不想要的词语 Filter filter = null; // 4.搜索 TopDocs topDocs = indexSearcher.search(query, filter, 10000); // 5.打印结果 System.out.println("---------------------------------------------"); System.out.println("总共有条"+topDocs.totalHits+"记录"); ScoreDoc[] scoreDoc = topDocs.scoreDocs; for(ScoreDoc score :scoreDoc) { int i = score.doc; Document doc = indexSearcher.doc(i); //获取值 //1. // Field field = doc.getField("name"); // field.stringValue(); //2. //doc.get("name"); System.out.println("name:"+doc.get("name")); System.out.println("content:"+doc.get("content")); System.out.println("size:"+doc.get("size")); System.out.println("path:"+doc.get("path")); } Date endDate = new Date(); System.out.println(String.valueOf(endDate.getTime()-BeginDate.getTime())+"ms"); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } } 工具类 package com.cngine.utils; 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.Field.Index; import org.apache.lucene.document.Field.Store; public class File2Docuement { public static Document ToDocument(String path) { File file = new File(path); Document doc = new Document(); doc.add(new Field("name",file.getName(),Store.YES,Index.ANALYZED)); doc.add(new Field("content",ReadFile(file),Store.YES,Index.ANALYZED)); doc.add(new Field("size",String.valueOf(file.length()),Store.YES,Index.NOT_ANALYZED)); doc.add(new Field("path",file.getAbsolutePath(),Store.YES,Index.NO)); return doc; } /** * * @param file * @return */ public static String ReadFile(File file) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); StringBuffer sb = new StringBuffer(); for(String line=null;(line = reader.readLine()) != null;) { sb.append(line).append("/n"); } return sb.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }