使用Lucene实现多个文档关键词检索demo(一)

来源:互联网 发布:知豆电动车好不好 编辑:程序博客网 时间:2024/06/05 16:18

在进行demo前先到http://www.ibm.com/developerworks/cn/java/j-lo-lucene1/了解关于lucene的一些基本概念,忽略其中的代码实例,因为年代久远,而我的这篇文档正是补充其中代码部分。
了解了基本概念后,接下来就可以开始完成demo了。
首先在http://www.apache.org/dyn/closer.cgi/lucene/java/4.10.0下载lucene包,这里我使用的是最新的4.10版,由于最新版与网上其他lucene使用demo在API上有很多差异,因此能够找到实例比较少,只能通过查看官方API来学习如何使用。
lucene4.10 API地址:http://lucene.apache.org/core/4_10_0/core/index.html
将下载后的压缩包解压后,将core和analysis目录下的jar包丢进项目path即可。

接下来就是编码
首先建立问指定目录下的所有文档建立索引,代码如下(具体解释见代码注释):

public static void buildIndex(String idir,String dDir)throws IOException{File indexDir = new File(idir);// 索引存放目录File dataDir = new File(dDir);// 需要建立索引的文件目录Analyzer luceneAnalyzer = new StandardAnalyzer();//分词工具File[] dataFiles = dataDir.listFiles();IndexWriterConfig indexConfig = new IndexWriterConfig(Version.LATEST, luceneAnalyzer);FSDirectory fsDirectory = null;IndexWriter indexWriter = null;try {fsDirectory = FSDirectory.open(indexDir);// 索引目录indexWriter = new IndexWriter(fsDirectory, indexConfig);// 用于创建索引的对象long startTime = new Date().getTime();for (int i = 0; i < dataFiles.length; i++) {if (dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")) {Document document = new Document();//代表一个文档Reader txtReader = new FileReader(dataFiles[i]);FieldType fieldType = new FieldType();fieldType.setIndexed(true);document.add(new TextField("path",dataFiles[i].getCanonicalPath(),Store.YES));//Field是用来描述文档的属性,比如这里文档设置了两个属性,路径和内容document.add(new Field("contents", txtReader, fieldType));indexWriter.addDocument(document);}}indexWriter.commit();//为文档建立索引long endTime = new Date().getTime();System.out.println("It takes " + (endTime - startTime) + " milliseconds to create index for the files in directory " + dataDir.getPath());} catch (IOException e) {e.printStackTrace();try {indexWriter.rollback();} catch (IOException e1) {e1.printStackTrace();}} finally {if(indexWriter!=null){indexWriter.close();}}}

接下来就是使用建立好的索引来检索关键字,代码如下:

public static void search(String queryStr) throws IOException {File indexDir = new File("/home/luzhen/lucenceIndex");FSDirectory fsDirectory = FSDirectory.open(indexDir);// 存放索引的目录IndexReader indexReader = DirectoryReader.open(fsDirectory);IndexSearcher searcher = new IndexSearcher(indexReader);if (!indexDir.exists()) {System.out.println("The Lucene index is not exist");return;}Term term = new Term("contents", queryStr);//搜索的基本单位TermQuery luceneQuery = new TermQuery(term);//基本的一个查询类TopDocs doc = searcher.search(luceneQuery, 10);//使用query进行查询,10为限制返回的结果for (int i = 0; i < doc.totalHits; i++) {//当有检索到结果时进入循环,打印出文档路径Document docHit = searcher.doc(doc.scoreDocs[i].doc);System.out.println(docHit.get("path"));}}

0 0
原创粉丝点击