在具体实现之前,请根据自己的要求,建立对应的路径及文件。例如,我这边创建的路径及文件是: D:/tools/LearningByMyself/lucene/source/demo1.txt
D:/tools/LearningByMyself/lucene/source/demo2.txt
D:/tools/LearningByMyself/lucene/index
第一步,建立索引,代码如下:
/** * @param sourceFile 需要添加到索引中的路径 * @param indexFile 存放索引的路径 * @throws Exception */ public static void textFileIndexer(String sourceFile,String indexFile) throws Exception{ File sourceDir = new File(sourceFile), indexDir = new File(indexFile); Directory dir = FSDirectory.open(indexDir); Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,luceneAnalyzer); iwc.setOpenMode(OpenMode.CREATE); IndexWriter indexWriter = new IndexWriter(dir,iwc); File[] textFiles = sourceDir.listFiles(); long startTime = new Date().getTime(); for(int i=0;i<textFiles.length;i++){ if(textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt")){ System.out.println("File--->" + textFiles[i].getCanonicalPath() + " 正在被索引....."); String str_temp = fileReaderAll(textFiles[i].getCanonicalPath(),"UTF-8"); System.out.println("文件内容:" + str_temp); Document document = new Document(); Field field_path = new Field("path",textFiles[i].getCanonicalPath(), Field.Store.YES,Field.Index.NO); Field field_body = new Field("body",str_temp,Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS); document.add(field_path); document.add(field_body); indexWriter.addDocument(document); } } indexWriter.close(); long endTime = new Date().getTime(); System.out.println("一共花费了" + (endTime - startTime) + "毫秒将" + sourceDir.getPath() + "中的文件增加到索引里面去....."); }
private static String fileReaderAll(String filename,String charset) throws IOException{ BufferedReader buffer_read = new BufferedReader( new InputStreamReader(new FileInputStream(filename),charset)); String line = new String(); String temp = new String(); while((line = buffer_read.readLine()) != null){ temp += line ; } buffer_read.close(); return temp ; }
第二步,在索引中检索关键字
/** * @param indexFile 索引所在的路径 * @param keyWords 需要检索的关键字 * @throws IOException * @throws ParseException */ public static void queryKeyWords(String indexFile,String keyWords) throws IOException,ParseException{ IndexReader reader = IndexReader.open( FSDirectory.open(new File(indexFile))); IndexSearcher index_search = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); QueryParser query_parser = new QueryParser(Version.LUCENE_36,"body",analyzer); Query query = query_parser.parse(keyWords); if(index_search != null){ TopDocs result = index_search.search(query, 10); //返回最多为10条记录 ScoreDoc[] hits = result.scoreDocs; if(hits.length > 0){ System.out.println("关键字:" + keyWords + ",在 " + indexFile + "中,一共检索到" + hits.length + "个..."); } index_search.close(); } }
第三部,自己编写一个测试类,测试一下上面的两个方法,例如,我写的测试类如下:
public class LuceneTest { public static void main(String[] args) throws IOException,ParseException,Exception{ String sourcePath = "D:/tools/LearningByMyself/lucene/source" ; String indexPath = "D:/tools/LearningByMyself/lucene/index" ; String key_words = "服务器" ; LuceneIndex.textFileIndexer(sourcePath, indexPath); LuceneIndex.queryKeyWords(indexPath, key_words); } }
最后,在控制台上查看结果。例如,我这边的测试结果如下:
File--->D:\tools\LearningByMyself\lucene\source\demo1.txt 正在被索引.....
文件内容:为了保证机房的网络安全,IDC内所有服务器不被允许从办公网直接ssh登录,必须通过跳板机 进行间接登录。用户通过跳板机执行的所有命令(包括通过跳板机登录的其他机器后的命令)都会被保存并审计。
File--->D:\tools\LearningByMyself\lucene\source\demo2.txt 正在被索引.....
文件内容:Relay是我们登录IDC服务器的跳板机,在Relay上用户只能执行ssh、passwd等简单命令,Relay只做ssh跳板机儿不做日常工具机使用。
一共花费了235毫秒将D:\tools\LearningByMyself\lucene\source中的文件增加到索引里面去.....
关键字:服务器,在 D:/tools/LearningByMyself/lucene/index中,一共检索到2个...
相关推荐
这是lucene的使用案例,实现了对word文档中的关键字检索,并将检索出的内容高亮打印出来
在Eclipse环境中运用java,Lucene建索引及查询关键字
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859本文出自【我是干勾鱼的博客】
lucene3.6.1文件关键字搜索代码(附加核心包),就是一个入门级的JAVA全局文件夹搜索案例、对于初学lucene很有帮助。
Lucene从问世之后,引发了开放源代码社群的巨大反响,程序员们不仅使用它构建具体的全文检索应用,而且将之集成到各种系统软件中去,以及构建Web应用,甚至某些商业软件也采用了Lucene作为其内部全文检索子系统的...
lucene对某目录下txt文件做全文检索源码
NULL 博文链接:https://iamyida.iteye.com/blog/2205114
NULL 博文链接:https://iamyida.iteye.com/blog/2203575
使用lucene技术,实现android的本地文档资源的全文检索功能,并实现关键字高亮
lucene实现文件检索Demo,
此资源配合《android+lucene实现全文检索并高亮关键字》使用http://download.csdn.net/detail/reality_jie/6335113
Lucene全文检索案例Lucene全文检索案例Lucene全文检索案例Lucene全文检索案例
关于lucene的一些介绍。Lucene:基于Java的全文检索引擎简介
创建索引 一、创建词法分析器 二、创建索引存储目录 三、创建索引写入器 四、将内容存储到索引 关键字查询 一、创建索引存储目录读取器 二、创建索引搜索器 三、解析查询 四、获取结果
Lucene.Net 文件检索doc,xls,ppt,txt,pdf文件(实例),对需要文件检索的朋友很有帮助,10分很值的
使用zend Framework的lucene进行全文检索
Lucene检索数据库支持中文检索,还不错的说
本案例通过.Net MVC4基础上,针对Lucene.Net实现全文检索的应用。通过查询数据表中数据,创建索引,通过统一输入框进行全文检索。可以进行对索引的增删改查功能。
Lucene可以对任何的数据做索引和搜索. Lucene不管数据源是什么格式,只要它能被转化为文字的形式,就可以被Lucene所分析利用.也就是说不管是MS word, Html ,pdf还是其他什么...你就可以用Lucene对它们进行索引以及搜索.
NULL 博文链接:https://1151461406.iteye.com/blog/2388343