建立索引,通过已经生成的索引文件,实现通过关键字检索。
写了一个类MySearchEngine,根据上述思想实现,把Lucene自带的递归建立索引的方法提取出来,加了一个搜索的方法:
package org.shirdrn.lucene;
import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.demo.FileDocument;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermDocs;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.TermQuery;import org.apache.lucene.store.LockObtainFailedException;
public class MySearchEngine {private File file;private String indexPath;public File getFile() { return file;}
public void setFile(File file) { this.file = file;}
public String getIndexPath() { return indexPath;}
public void setIndexPath(String indexPath) { this.indexPath = indexPath;}public void createIndex(IndexWriter writer, File file) throws IOException { // file可以读取 if (file.canRead()) { if (file.isDirectory()) { // 如果file是一个目录(该目录下面可能有文件、目录文件、空文件三种情况) String[] files = file.list(); // 获取file目录下的所有文件(包括目录文件)File对象,放到数组files里 // 如果files!=null if (files != null) { for (int i = 0; i < files.length; i++) { // 对files数组里面的File对象递归索引,通过广度遍历 createIndex(writer, new File(file, files[i])); } } } else { // 到达叶节点时,说明是一个File,而不是目录,则为该文件建立索引 try { writer.addDocument(FileDocument.Document(file)); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } } }}public void searchContent(String type,String keyword){ // 根据指定的检索内容类型type,以及检索关键字keyword进行检索操作 try { IndexSearcher searcher = new IndexSearcher(this.indexPath); // 根据指定路径,构造一个IndexSearcher检索器 Term term = new Term(type,keyword); // 创建词条 Query query = new TermQuery(term); // 创建查询 Date startTime = new Date(); TermDocs termDocs = searcher.getIndexReader().termDocs(term); // 执行检索操作 while(termDocs.next()){ // 遍历输出根据指定词条检索的结果信息 System.out.println("搜索的该关键字【"+keyword+"】在文件\n"+searcher.getIndexReader().document(termDocs.doc())); System.out.println("中,出现过 "+termDocs.freq()+" 次"); } Date finishTime = new Date(); long timeOfSearch = finishTime.getTime() - startTime.getTime(); // 计算检索花费时间 System.out.println("本次搜索所用的时间为 "+timeOfSearch+" ms"); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
}
这里引用了import org.apache.lucene.demo.FileDocument,在创建Field的时候,为每个Field都设置了三种属性:path、modified、contents。在检索的时候,只要指定其中的一个就可以从索引中检索出来。
public static void main(String[] args){ MySearchEngine mySearcher = new MySearchEngine(); String indexPath = "E:\\Lucene\\myindex"; File file = new File("E:\\Lucene\\txt"); mySearcher.setIndexPath(indexPath); mySearcher.setFile(file); IndexWriter writer; try { writer = new IndexWriter(mySearcher.getIndexPath(),new StandardAnalyzer(),true); mySearcher.createIndex(writer, mySearcher.getFile()); mySearcher.searchContent("contents","server"); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
在构造IndexWriter的时候,选择分词器不同,对检索的结果有很大的影响。
我感觉,这里自带的StandardAnalyzer和SimpleAnalyzer对中文的支持不是很好,因为它是对像英文这样的,以空格作为分隔符的,中文不同英文的习惯,可能有时候根本检索不出中文。
使用StandardAnalyzer和SimpleAnalyzer的时候,检索关键字“server“,结果是相同的,结果如下所示:
搜索的该关键字【server】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\license.txt> stored/uncompressed,indexed<modified:200106301125>>中,出现过 2 次搜索的该关键字【server】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Patch.txt> stored/uncompressed,indexed<modified:200112160745>>中,出现过 8 次搜索的该关键字【server】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\REDIST.TXT> stored/uncompressed,indexed<modified:200511120152>>中,出现过 1 次搜索的该关键字【server】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\新建 文本文档.txt> stored/uncompressed,indexed<modified:200710270258>>中,出现过 27 次本次搜索所用的时间为 0 ms
但是,如果使用StandardAnalyzer检索中文,mySearcher.searchContent("contents","的");,结果可以看出来:
搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\120E升级包安装说明.txt> stored/uncompressed,indexed<modified:200803101357>>中,出现过 2 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\1实验题目.txt> stored/uncompressed,indexed<modified:200710160733>>中,出现过 1 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\3实验题目.txt> stored/uncompressed,indexed<modified:200710300744>>中,出现过 2 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\CustomKeyInfo.txt> stored/uncompressed,indexed<modified:200406041814>>中,出现过 80 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\CustomKeysSample.txt> stored/uncompressed,indexed<modified:200610100451>>中,出现过 8 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\FAQ.txt> stored/uncompressed,indexed<modified:200604130754>>中,出现过 348 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\MyEclipse 注册码.txt> stored/uncompressed,indexed<modified:200712061059>>中,出现过 5 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\readme.txt> stored/uncompressed,indexed<modified:200803101314>>中,出现过 17 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Struts之AddressBooks学习笔记.txt> stored/uncompressed,indexed<modified:200710131711>>中,出现过 8 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Update.txt> stored/uncompressed,indexed<modified:200707050028>>中,出现过 177 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Visual Studio 2005注册升级.txt> stored/uncompressed,indexed<modified:200801300512>>中,出现过 3 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\书籍网站.txt> stored/uncompressed,indexed<modified:200708071255>>中,出现过 3 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\使用技巧集萃.txt> stored/uncompressed,indexed<modified:200511210413>>中,出现过 153 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\关系记录.txt> stored/uncompressed,indexed<modified:200802201145>>中,出现过 24 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\剑心补丁使用说明(readme).txt> stored/uncompressed,indexed<modified:200803101357>>中,出现过 17 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\史上最强天籁之声及欧美流行曲超级精选【 FLAC分轨】.txt> stored/uncompressed,indexed<modified:200712231241>>中,出现过 1 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\密码强度检验.txt> stored/uncompressed,indexed<modified:200712010901>>中,出现过 1 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\新1建 文本文档.txt> stored/uncompressed,indexed<modified:200710311142>>中,出现过 39 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\新建 文本文档.txt> stored/uncompressed,indexed<modified:200710270258>>中,出现过 4 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\汉化说明.txt> stored/uncompressed,indexed<modified:200708210247>>中,出现过 16 次本次搜索所用的时间为 16 ms
使用SimpleAnalyzer的时候,检索中文关键字,结果很不准确。mySearcher.searchContent("contents","的");,结果可以看出来:
搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Update.txt> stored/uncompressed,indexed<modified:200707050028>>中,出现过 1 次搜索的该关键字【的】在文件Document<stored/uncompressed,indexed<path:E:\Lucene\txt\使用技巧集萃.txt> stored/uncompressed,indexed<modified:200511210413>>中,出现过 1 次本次搜索所用的时间为 0 ms
现在感觉到了,分词在建立索引的时候是一件多么重要的事情啊。
