一、乱碰
去年5、6月份看了下lucene+hibernate简单碰了下,后来又想看但是就丢了。现在打算重新学下,东西感觉挺多的。不知道能不能坚持下。打算做个简单的小搜索。
二、lucene的简单用法
1.创建索引用到的核心类
Directory //描述索引存放的位置 Analyzer //分词器 IndexWriter //对索引的操作,通过分词器和索引目录完成的 Field //每个文档都包含有不同的域(属性),每个域也都有自己的名字 Document //文档的描述通过上面的域进行(好像一个类)
创建索引的一个例子:
String filePath;//文件路径 String indexPath;//索引路径 Directory dir=new FSDirectory.open(new File(indexPath); //创建一个索引存放位置 //声明一个分词器 Analyzer analyzer=new SmartChineseAnalyzer(Version.LUCENE_42,true); IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_42, analyzer); //通过索引指向+分词器 声明一个索引的操作 IndexWriter indexWriter=IndexWriter(dir, iwc); //文档包含的域 Document doc=new Document(); doc.add(new StringField("name",new File(filePath).getName(), Field.Store.YES); doc.add(new StringField("path",new File(filePath).getAbsolutePath(),Field.Store.YES); ..... //把文档添加到对应的分词中 indexWriter。addDocument(doc); indexWrtier.close(); //end
2.创建搜索的主要类
IndexSearcher //索引的搜索操作 Term //搜索的基本单元 也就是对准field里面 单元 关键字 Query //查询 TermQuery //查询query的子类 TopDocs //匹配前N个符合要求的文档
搜索的基本例子:
package com.stx.search; import java.io.File; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.junit.Test; public class Search { @Test public void search() throws Exception { String filePath="E:\\测试lucene\\index"; Directory dir=FSDirectory.open(new File(filePath)); IndexReader reader=DirectoryReader.open(dir); IndexSearcher searcher=new IndexSearcher(reader); Term term=new Term("content", "护照"); TermQuery query=new TermQuery(term); TopDocs hits=searcher.search(query,10); // TermDocs termDocs=searcher. System.out.println("发下数据:"+hits.totalHits); for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc=searcher.doc(scoreDoc.doc); System.out.println(doc.get("path")); } reader.close(); } }
文本的一个遍历添加索引例子
package com.stx.testlucene; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; public class MyLucene { // 测试搜索李敖的本地小说 private String path; // 文本路径 private String indexPath; // 索引存放路径 private int countTxt; // 文件总数统计 private Long startTime; // 开始的时间 private Long endTime; // 搜索结束时间 // private Directory dir; private IndexWriter indexWriter; public MyLucene() throws Exception { path = "E:\\测试lucene\\李敖大全集 TXT版"; indexPath = "E:\\测试lucene\\index"; countTxt = 0; startTime = System.currentTimeMillis(); // 声明索引存放的路径 dir = FSDirectory.open(new File(indexPath)); indexWriter = getWriter(); } // 得到此文件夹下的所有所有文件 @SuppressWarnings("deprecation") public void getAllFile(String nowPath) throws IOException { // 得到文件路径 File file = new File(nowPath); for (File files : file.listFiles()) { if (files.isDirectory()) { getAllFile(files.getCanonicalPath()); } else { if (files.getName().toLowerCase().endsWith(".txt")) { // 文件个数添加一 countTxt++; // 打印出所的的文件路径 System.out.println(files.getAbsolutePath()); // 写入索引了 Document doc = new Document(); doc.add(new StringField("path", files.getAbsolutePath(), Field.Store.YES)); // doc.add(new TextField("content", loadFile(files), // Field.Store.NO)); doc.add(new TextField("content", new FileReader(files))); indexWriter.addDocument(doc); } } } // indexWriter.close(); } // 测试方法遍历所有的文件 public void ceshi() throws IOException { getAllFile(path); endTime = System.currentTimeMillis(); } // 这是个结束输出的提升信息 public void end() throws IOException { indexWriter.close(); System.out.println("总文件数:" + countTxt); System.out.println("时间耗费:" + (endTime - startTime)); } // 返回索引的实例 public IndexWriter getWriter() throws Exception { Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_42, true); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_42, analyzer); return new IndexWriter(dir, iwc); } // 读取文件字符串 public String loadFile(File file) throws IOException { BufferedReader bufferReader = new BufferedReader(new FileReader(file)); StringBuffer buffer = new StringBuffer(); String line = bufferReader.readLine(); while (line != null) { buffer.append(line); line = bufferReader.readLine(); } bufferReader.close(); return buffer.toString(); } @Test public static void main(String[] args) throws Exception { MyLucene m = new MyLucene(); m.ceshi(); m.end(); } }